I'm creating a news page where visitors to my site can search previous news posts based on criteria they enter in a text box..
i can get the search function to work fine, but sometimes, there might be a lot of results (depending on what they search for) and i'd like to span that data over as many pages as it needs.
I can't figure out how to keep the original search query going and build the next pages..
Here's is a piece of my script (when the user clicks search). and i also included the section that displays the individual post..
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=\"newnews.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>";
}
}
if ($_GET[id]) {
$get_news = "SELECT *,date_format(date,'%W, %M %D, %Y | %l:%i%p') AS frm_date FROM news WHERE id = '".$_GET[id]."'";
$get_news_res = mysql_query($get_news) or die(mysql_error());
while($row = mysql_fetch_array($get_news_res)) {
$id = $row["id"];
$date = $row["frm_date"];
$image = $row["image"];
$title = $row["title"];
$body = $row["body"];
$display_block = "<tr><a name=$id></a>
<td valign=top width=200 align=center><img src=images/news/$image width=200 border=1></td>
<td valign=top><b>$title</b><br>
<span class=date>$date</span><br>
<br>
$body
</td>
</tr>
<tr><td valign=top colspan=2 align=right><a href=javascript:; onClick=MM_openBrWindow('sendtoafriend.php?id=$id','','width=410,height=220,resizable=yes')><img src=images/news/stafspacer.gif border=0></a></td></tr>";
}
}