Jump to content

Searching two arrays fully and comparing


ChadNomad

Recommended Posts

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.