I'm working on a script where users can search for text in a database. So far it is working okay, but I want the results to be displayed on multiple pages.
I can't really get it to work though. The search works fine, and the next/previous page links get built fine, but I can't figure out how, when you click the links, to get it to go to the next page.
Here's some of my script.. (after the user has submitted their query)
if ($_POST[op] == "search") {
if(!isset($_GET['page'])){
$page = 1;
} else {
$page = $_GET['page'];
}
// Define the number of results per page
$max_results = 25;
// Figure out the limit for the query based
// on the current page number.
$from = (($page * $max_results) - $max_results);
// Perform MySQL query on only the current page number's results
$get_results = "SELECT *,date_format(date,'%m/%d/%Y') AS frm_date FROM news WHERE body LIKE '%".$_POST[newssearch]."%' ORDER BY date DESC LIMIT $from, $max_results";
$get_results_res = mysql_query($get_results) or die(mysql_error());
$display_block = "<table>";
if (mysql_num_rows($get_results_res) == "0") {
$display_block = "<tr><td>There is no news about "<b>".$_POST[newssearch]."</b>".</td></tr>";
}
if (mysql_num_rows($get_results_res) > "0") {
$display_block = "<tr><td>Results for "<b>".$_POST[newssearch]."</b>"</td></tr><tr><td>";
while($row = mysql_fetch_array($get_results_res)){
$id = $row["id"];
$date = $row["frm_date"];
$title = $row["title"];
$body = $row["body"];
// Build your formatted results here.
$display_block .= "<a href=\"news.php?id=$id\">$title</a> ($date)<br>";
}
$display_block .= "</td></tr>";
// Figure out the total number of results in DB:
$total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM news WHERE body LIKE '%".$_POST[newssearch]."%'"),0);
// Figure out the total number of pages. Always round up using ceil()
$total_pages = ceil($total_results / $max_results);
// Build Page Number Hyperlinks
$display_block .= "<tr><td>Select a Page<br />";
// Build Previous Link
if($page > 1){
$prev = ($page - 1);
$display_block .= "<a href=\"".$_SERVER['PHP_SELF']."?page=$prev\"><<Previous</a> ";
}
for($i = 1; $i <= $total_pages; $i++){
if(($page) == $i){
$display_block .= "$i ";
} else {
$display_block .= "<a href=\"".$_SERVER['PHP_SELF']."?page=$i\">$i</a> ";
}
}
// Build Next Link
if($page < $total_pages){
$next = ($page + 1);
$display_block .= "<a href=\"".$_SERVER['PHP_SELF']."?page=$next\">Next>></a>";
}
$display_block .= "</td></tr>";
}
}
how can i get the scriptname.php?page=2 to actually function and still include the results of the search? Or is there some way to do something like this:
scriptname.php?search=searchtext&page=2