ChadNomad Posted July 29, 2007 Share Posted July 29, 2007 I'm trying to find matching pages by searching two arrays against each other. The arrays get the keywords from the current page and others stored in a database. I'm not very good with PHP but what I'm doing at the moment is actually working, except it's only printing one found keyword when there are more. Heres what I have: $q1 = mysql_query("SELECT * FROM $table WHERE category='$section' ORDER BY time DESC LIMIT 5") or die (mysql_error()); $table = "the_table"; $qX = mysql_query("SELECT keywords FROM $table WHERE id='$id'") or die (mysql_error()); $rX = mysql_fetch_array($qX) or die (mysql_error()); $keywordThis = ereg_replace(" ","",$rX[keywords]); $keywordThis = explode(",",$keywordThis); $a = 0; $i = 0; $x = 0; $y = 0; $z = 0; $qY = mysql_query("SELECT * FROM $table WHERE id!='$id'") or die (mysql_error()); $rows = mysql_num_rows($qY); while ($rY = mysql_fetch_array($qY)) { $getKeyword = $getKeyword.$rY[keywords].","; $getID = $getID.$rY[id].","; $getTitle = $getTitle.$rY[title].","; $getCategory = $getCategory.$rY[category].","; $x++; } $getKeyword = ereg_replace(" ","",$getKeyword); $getKeyword = explode(",",$getKeyword); $getID = explode(",",$getID); $getTitle = explode(",",$getTitle); $getCategory = explode(",",$getCategory); while ($getKeyword[$y] != "") { if ($getKeyword[$y] == $keywordThis[$y]) { $result = $result + 1; } $y = $y + 1; } if($result < 1) { echo ' No similar pages found...'; } else { while ($getKeyword[$z] != "") { if ($getKeyword[$z] == $keywordThis[$z]) { echo '<div id="result_line"><a href="index.php?go='.$getCategory[$z].'&id='.$getID[$z].'" class="result" title="'.$getTitle[$z].'">'.substr($getTitle[$z],0,30).'...</a></div>'; $result = $result + 1; } $z = $z + 1; } } Hope someone can help, sorry if the code is messy. Link to comment https://forums.phpfreaks.com/topic/62273-searching-two-arrays-fully-and-comparing/ Share on other sites More sharing options...
zq29 Posted July 29, 2007 Share Posted July 29, 2007 Have you checked out the array_intersect() function? Look it up in the manual, it does what I think you're trying to achieve! Link to comment https://forums.phpfreaks.com/topic/62273-searching-two-arrays-fully-and-comparing/#findComment-309949 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.