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
<?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