PDA

View Full Version : Search engine: Unexpected results


jianneng
11-02-2002, 08:10 AM
Hi, I am making a simple search engine (for images) which user can enter a search string, and specify which category of image they wish to search. I started with one category first, and I have two piece of PHP code to accomplish this:

<?PHP
//First script: search.php
if (isset ($Submit)) {
header ("Location: search_result.php?search_text=$search_text&category=$category");
}
?>

------------------------------------------------------------------------------------


<?PHP
//Second script: search_result.php
$display_number = 8;

$db_connection = mysql_connect ('localhost', 'username', 'password') or die (mysql_error());
$db_select = mysql_select_db('Pictures') or die (mysql_error());

if (!isset($num_pages)) {

// Determine the query.
if ($category == "Bird") {
$query1 = "";
$query1 = "SELECT * FROM bird WHERE bird_name LIKE '%$search_text%'";
}

// Query the database.
$query_result1 = @mysql_query ($query1) or die (mysql_error());

// Calculate the number of pages required.
$num_results = @mysql_num_rows ($query_result1);

if ($num_results > $display_number) {
$num_pages = ceil ($num_results/$display_number);
} elseif ($num_results > 0) {
$num_pages = 1;
} else {
echo 'There are no pictures in this category.';
}
$start = 0;
}

$query2 = "SELECT * FROM bird WHERE bird_name LIKE '%$search_text%' LIMIT $start, $display_number";

// Print a table.
echo '<table align="center" cellpadding="2" cellspacing="2" border="1"><tr align="center">
<td align="center"><strong>Pic ID</strong></FONT></td>
<td align="center"><strong>Bird</strong></td>
<td align="center"><strong>Name</strong></td>
</tr>';

// Print each item.
$query_result2 = @mysql_query ($query2);
while ($row = @mysql_fetch_array ($query_result2)) {
echo " <tr align=\"center\">
<td align=\"center\">$row[pic_id]</td>
<td align=\"center\"><img src=\"$row[pic_url]\"></td>
<td align=\"center\">$row[bird_name]</td>
</tr>";
}

// Make the links to other pages, if necessary.
if ($num_pages > 1) {
echo ' <tr align="center"><td align="center" colspan="3">';

// Determine what page the script is on.
if ($start == 0) {
$current_page = 1;
} else {
$current_page = ($start/$display_number) + 1;
}

// If it is not the first page, make a Previous button.
if ($start != 0) {
echo '<a href="search_result.php?start=' . ($start - $display_number) . '&num_pages=' . $num_pages . '">Previous</a> ';
}

// Make all the numbered pages.
for ($i = 1; $i <= $num_pages; $i++) {
$next_start = $start + $display_number;
if ($i != $current_page) {

// Don't link the current page.
echo '<a href="search_result.php?start=' . (($display_number * ($i - 1))) . '&num_pages=' . $num_pages . '">' . $i . '</a> ';
} else {
echo $i . ' ';
}
}

// If it is not the last page, make a Next button.
if ($current_page != $num_pages) {
echo '<a href="search_result.php?start=' . ($start + $display_number) . '&num_pages=' . $num_pages . '">Next</a> ';
}
echo '</td> </tr>';
}
?>
------------------------------------------------------------------------------------


Ok, the problem here is if say I search for "hornbill", and I got 14 hornbill pictures in my database, so I will get 2 pages of results. The first page will display 8 hornbill records, and the second page should display the other 6 records. The 'headache' is my database somehow add irrelevant records (e.g. 2 seabird pictures) to my second page, making it altogether 8 records. Those 2 extra records are not what I searched for.

Any idea of what could be wrong in my coding? Thank you!


Lim

mordred
11-02-2002, 09:21 AM
I suspect that the $search_text variable is no longer set when you click on the link to search_result.php, because it seems you only pass the 'start' and 'numPages' variables in the query string.

Try to echo your sql string so you can see what query you actually send to the MySQL server. Also, using a more strict error reporting level helps a lot at tracking down lost variables. Add this at the top of your script:


error_reporting(E_ALL);

jianneng
11-02-2002, 12:17 PM
Thanks...now I see the error. I will try again.

jianneng
11-02-2002, 01:32 PM
Hi, PHP is telling me that I have an undefined variable "$search_text" in the following line of code, which is in my second script...

$query2 = "SELECT * FROM bird WHERE bird_name LIKE '%$search_text%' LIMIT $start, $display_number";


I thought I have passed the variable from my first script to this second script...How could I amend this "undefined variable" problem?


Lim

mordred
11-02-2002, 03:11 PM
There are some techniques how to pass variables from pages to pages, or how to make them accessible from many pages. Possible techniques include GET/POST variables, sessions, cookies, files, database entries. You see, there are quite a few ways to deal with your problem.

For a start I would recommend that you pass the variable as an additional $_GET parameter. Something like


echo '<a href="search_result.php?start=' . (($display_number * ($i - 1))) . '&search_text=' . $search_text . '&num_pages=' . $num_pages . '">' . $i . '</a> ';


Then you only have to import this variable on the next page, like


$search_text = "";

if (isset($_GET['search_text'])) {
$search_text = $_GET['search_text'];
}


I think this illustrates the method clearly enough. Ask if something still is unclear.

jianneng
11-02-2002, 04:08 PM
Hi, that really works! Thanks!