Hello, i am relatively new to PHP and i am struggling with printing multiple search results on to different pages. The code below works ok but when you click on next page button, it brings up a blank screen. I think it might have something to do with resetting the $found variable. Can you have a quick look? any suggestions would be greatly appreciated.
<?
if ($_GET) $vars=$_GET;
else $vars=$_POST;
$found = $vars['search'];
if (!($limit)){
$limit = 10;} // Default results per-page.
if (!($page)){
$page = 0;} // Default page value.
if ($found == "")
{
echo "<p>You forgot to enter a search term<br>";
exit;
}
$username="****";
$password="******";
$database="******";
$host="*****************";
mysql_connect($host,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
//convert to uppercase to allow for case sensisivities
$found = strtoupper($found);
//strip code from search term
$found = strip_tags($found);
//strip spaces from search term
$found = trim ($found);
$check = mysql_query("SELECT * FROM song WHERE upper(description) LIKE '%$found%'");
$anymatches=mysql_num_rows($check);
if ($anymatches == 0)
{
echo "Sorry, no matching results were found<br><br><br>";
}
$pages = intval($anymatches/$limit); // Number of results pages.
// $pages now contains int of pages, unless there is a remainder from division.
if ($anymatches % $limit) {
$pages++;} // has remainder so add one page
$current = ($page/$limit) + 1; // Current page number.
if (($pages < 1) || ($pages == 0)) {
$total = 1;} // If $pages is less than one or equal to 0, total pages is 1.
else {
$total = $pages;} // Else total pages is $pages value.
$first = $page + 1; // The first result.
if (!((($page + $limit) / $limit) >= $pages) && $pages != 1) {
$last = $page + $limit;} //If not last results page, last result equals $page plus $limit.
else{
$last = $anymatches;} // If last results page, last result equals total number of results
echo "Results <b>$first</b> - <b>$last</b> of <b>$anymatches</b><br><br>";
$search = mysql_query("SELECT * FROM song WHERE upper(description) LIKE '%$found%' LIMIT $page, $limit");
while($result = mysql_fetch_array($search))
{
PRINT RESULTS!!!
}
if ($page != 0) { // Don't show back link if current page is first page.
$back_page = $page - $limit;
echo("<a href=\"$PHP_SELF?query=$found&page=$back_page&limi t=$limit\">back</a> \n");}
for ($i=1; $i <= $pages; $i++) // loop through each page and give link to it.
{
$ppage = $limit*($i - 1);
if ($ppage == $page){
echo("<b>$i</b> \n");} // If current page don't give link, just text.
else{
echo("<a href=\"$PHP_SELFquery=$found&page=$ppage&limit=$li mit\"> $i</a> \n");}
}
if (!((($page+$limit) / $limit) >= $pages) && $pages != 1) { // If last page don't give next link.
$next_page = $page + $limit;
echo(" <a href=\"$PHP_SELF?query=$found&page=$next_page&limi t=$limit\">next</a>\n");}
?>
Thanks!
Bigalan