Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

string comparison

Status
Not open for further replies.

richclever

IS-IT--Management
Oct 5, 2000
127
FR
I am really confused.

I have a set of database fields that have a list of keywords in them that are comma delimited.

I have read them into an array and exploded them so each keyword is in a seperate array element; thus I have the follwing list in my array from one record:
$arr1[1]="potato"
$arr1[2]="potatoes"
$arr1[3]="garden"

The second record is read into a different array $arr2 and contains the following elements:
$arr2[1]="gardening"
$arr2[2]="plants"
$arr2[3]="new potatoes"
$arr2[4]="varieties of pôtatoes"

What I need to do, is have the first array $arr1 look through the second array $arr2 and if there are 2 or more similar entries, display the second array. So, $arr1 compared to $arr2 will find 5 similarities or exact matches and so output the entries in $arr2.

Does any one have anyideas. I have tried to use strist to compare them but for some reason it is not working.

I enclose my php if anyone is interested (you will see that it actually constructs a menu that is defined elsewhere)

<?php
include ("authormenu.inc");
if ($pagekeywords != ''){

//relevant menu
$relevantmenu=$relevant_menu_header;
$relevantcount=0;
//picks keywords from article and places them in an array. compares other articles keywords with current article and flags any relevances.
$arr1=explode(',', $pagekeywords);
$arr1length = count ($arr1);
for ($l1 = 0; $l1 < $arr1length; $l1++){
//echo "$arr1[$l1] ,";

$relevantquery="SELECT title, author, pid, uid, keywords FROM pages WHERE keywords LIKE '$arr1[$l1]'";
$relevantresult=mysql_query($relevantquery);
while (list($title, $author,$pid, $uid, $keywords) = mysql_fetch_row($relevantresult)){
$arr2=explode (',', $keywords);
$arrayLength = count($arr2);

if ($pid != $sectionuid){
if ($author != $pageauthor){

for ($i = 0; $i < $arrayLength; $i++){
for ($i1 = 0; $i1 < $arr1length; $i1++){
if (stristr($arr1[$i1], $arr2[$i])){
$relevantmenu=$relevantmenu . $relevant_menu_link1 . $uid . $relevant_menu_link2 . $title . $relevant_menu_link3;
$relevantcount++;
if ($relevantcount =1){

$menucount++;

//$relevantcount = 0;
};

};
};

};
};
};



if ($menucount>1){
$relevantmenu=$relevantmenu . $relevant_menu_link1 . $uid . $relevant_menu_link2 . $title . $relevant_menu_link3;
//$menucount=0;
};
};
};
};
$relevantmenu=$relevantmenu . $relevant_menu_footer;
?>


Thanks Rich
 
Have you considered using the percent wildcard in your LIKE statement in the query?
SELECT * WHERE whatevercolumn LIKE "%keyword%"
I'm not sure if your first query returns any rows to begin with.
 
Thansk a lot,

I totally forgot to use the %. I had it in on a test variable but forgot to include it when I reset the variable.

Rich
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top