Jump to content

Splitting search results


apophis

Recommended Posts

I have a search page and try to split the result, as it is nicer to see, insted of 100 result to get it by 25 each.

The code is doing everything it have to do, but every time i go to the next page, it starts indexing them again from 1 insted of 26,27,etcetera...

 

I am a little tired of looking and at this moment i don't see the problem. Any suggestions.

 

Here is the code i am using:

 

<?php
include('config.php');
error_reporting(E_ALL);

function Print_Single ($SQL_search_Row) {

  $Id = $SQL_search_Row['Id'];
  $DOB1 = $SQL_search_Row['DOB'];
  $DOB = date('d M Y', strtotime($DOB1));

  $Image_folder = "<img src=\"../pictures/andere/new.gif\" width=\"35\" height=\"15\" />";
      $new = $Image_folder;
  $bewerk_datum = $SQL_search_Row['DOC'];
  $aantal_dagen = floor(abs((strtotime($bewerk_datum)-time())/(60*60*24)));

  $Image_folder2 = "<img src=\"../pictures/andere/update.gif\" width=\"47\" height=\"10\" />";
      $update = $Image_folder2;
  $bewerk_datum2 = $SQL_search_Row['DOU'];
  $aantal_dagen2 = floor(abs((strtotime($bewerk_datum2)-time())/(60*60*24)));


  print "<span class=\"style2\">".$SQL_search_Row['Ch']."</span>\n";
  print "</a>&nbsp";
  print "<span class=\"style3\">".$SQL_search_Row['DogName']."</span";
  print "</a>&nbsp";
  print "<span class=\"style4\">(".$SQL_search_Row['Color'].")</span>";
  print "</a>&nbsp";
  print "<span class=\"style4\">".$DOB."</span>";
  print "</a>  &nbsp&nbsp";
  if($aantal_dagen <60){echo $new;}
      else{echo'';}
      if($aantal_dagen2 <60){echo $update;}
      else{echo'';}
  print "<br><font color='black' size='1' face='Arial'>";
  print "[<a href=\"pedigree.php?Id=$Id\">Pedigree]</a>  ";
  print "[<a href=\"breeding.php?Id=$Id\">Breeding]</a>  ";
  print "[<a href=\"sibling.php?Id=$Id\">Sibling]</a></font>";
   }

// Added for splitting the pages
$RowPerPages = 25 ;
$PageNumber = 1;
// if $_GET['page'] defined, use it as page number
if(isset($_GET['page']))
{
    $PageNumber = $_GET['page'];
}
$offset = ($PageNumber - 1) * $RowPerPages;


$var = $_GET['q']; // Get the search variable from URL
$trimmed = trim($var); //trim whitespace from the stored variable

// check for an empty string and display a message.

if ($trimmed == "")

{

		print "<p>Please enter a search...</p>";
		exit;

}

print "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">";
print "<html xmlns=\"http://www.w3.org/1999/xhtml\">";
print "<head>";
print "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\" />";
print "<title>Searching the database</title>";
print "</head>";

print "<body>";

print  "<p <span align=\"center\" class=\"style1\">Searching database - here are the entries that match: ".$trimmed."</span></p>\n";
print "<br>";	

$SQL_search = "SELECT * FROM $table WHERE DogName Like \"%$trimmed%\" ORDER BY DogName LIMIT $offset, $RowPerPages";
$SQL_search_Result = mysql_query($SQL_search) or die(mysql_error());



// keeps getting the next row until there are no more to get
if (mysql_num_rows($SQL_search_Result) >0){

$a = 1;

while($SQL_search_Row = mysql_fetch_array( $SQL_search_Result)) {

            // Print out the contents of each row into a table


print "<table width=\"60%\" border=\"1\" bgcolor=\"#00FFFF\">";
print   "<tr>";
print     "<td width=\"10%\"><div align=\"center\"><strong>".$a."</strong></div></td>";
print     "<td width=\"90%\"><div align=\"left\">";
echo Print_Single($SQL_search_Row);
print  "</div></td></tr>";
print "</table>";	
$a++;
}
} 

echo "</table>";
print "<body>";
$query   = "SELECT * FROM $table WHERE DogName Like \"%$trimmed%\"";
$result  = mysql_query($query) or die('Error, query failed');
$numrows      =mysql_num_rows($result);

//print "number of rows = $numrows";

// how many pages we have when using paging?
$maxPage = ceil($numrows/$RowPerPages);

$self = $_SERVER['PHP_SELF'];

// creating 'previous' and 'next' link
// plus 'first page' and 'last page' link

// print 'previous' link only if we're not
// on page one
if ($PageNumber > 1)
{
    $page = $PageNumber - 1;
    $prev = " <a href=\"$self?q=$trimmed&page=$page\">[Prev]</a> ";

    $first = " <a href=\"$self?q=$trimmed&page=1\">[First Page]</a> ";
}
else
{
    $prev  = ' [Prev] ';       // we're on page one, don't enable 'previous' link
    $first = ' [First Page] '; // nor 'first page' link
}

// print 'next' link only if we're not
// on the last page
if ($PageNumber < $maxPage)
{
    $page = $PageNumber + 1;
    $next = " <a href=\"$self?q=$trimmed&page=$page\">[Next]</a> ";

    $last = " <a href=\"$self?q=$trimmed&page=$maxPage\">[Last Page]</a> ";
}
else
{
    $next = ' [Next] ';      // we're on the last page, don't enable 'next' link
    $last = ' [Last Page] '; // nor 'last page' link
}

// print the page navigation link
echo $first . $prev . " Showing page <strong>$PageNumber</strong> of <strong>$maxPage</strong> pages " . $next . $last;

print "</body>";
print "</html>";

?>


Link to comment
https://forums.phpfreaks.com/topic/42210-splitting-search-results/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.