Jump to content

search 2 fields in a database and sort results


flash gordon

Recommended Posts

hxxp://localhost/test.php?search=foo,sad

hxxp://localhost/test.php?search=sad,foo

 

the results are different, but they should be the same?????

 

/*
* SEARCH WORDS into SQL STATEMENT
*/
//$search  = "foo";
//$search2 = "sad";
$search = explode(",", $_GET['search']);

$sql2 = "";
for ($i=0; $i<count($search)-1; $i++)
{
$sql2 .= " SELECT id, url, name, composer, time, description FROM cues WHERE keywords REGEXP '([,;. ]|^)+$search[$i]([,;. ]|$)+'";
$sql2 .= " UNION";
}
$last = count($search)-1;
$sql2 .= " SELECT id, url, name, composer, time, description FROM cues WHERE keywords REGEXP '([,;. ]|^)+$search[$last]([,;. ]|$)+'";



/*
* SQL QUERIES
*/

// put data into temporary table
$sql = "CREATE TEMPORARY TABLE tmp_cues";
$sql .= $sql2;
//$sql .= " SELECT id, url, name, composer, time, description FROM cues WHERE keywords REGEXP '([,;. ]|^)+$search([,;. ]|$)+'";
//$sql .= " UNION";
//$sql .= " SELECT id, url, name, composer, time, description FROM cues WHERE keywords REGEXP '([,;. ]|^)+$search2([,;. ]|$)+'";
$result = mysql_query($sql) or die (mysql_error());

// get date from temporary table and sort
$sql = " SELECT id, url, name, composer, time, description, COUNT(*) as t FROM tmp_cues GROUP BY id ORDER BY t ASC";
$result = mysql_query($sql) or die (mysql_error());




/*
* ECHO DATA OUT TO SCREEN
*/
header("Content-Type: application/xml; charset=ISO-8859-1");
echo '<?xml version="1.0" encoding="iso-8859-1"?>' . "\n";
echo '<matches>' . "\n";
while($row = mysql_fetch_array($result))
{
echo "\t" .   '<cue id="' . $row['id'] . '">' .  "\n";
echo "\t\t" . '<url>' . $row['url'] . '</url>' . "\n";
echo "\t\t" . '<name>' . $row['name'] . '</name>' . "\n";
echo "\t\t" . '<composer>' . $row['composer'] . '</composer>' . "\n";
echo "\t\t" . '<time>' . $row['time'] . '</time>' . "\n";
echo "\t\t" . '<description>' . $row['description'] . '</description>' . "\n";
echo "\t" .   '</cue>' . "\n\n"; 
}

echo '</matches>';

Link to comment
Share on other sites

I wish I could just attach this to the other threads, but I can't (mod feel free to clean it up)...Attached is the php file and database info.

 

"sad" appears in all rows, and the script does not work when sad appears first e.g. sad,foo However it works fine as foo, sad.

 

Cheers

:)

 

[attachment deleted by admin]

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.