05, 09 2010, 07:36:51 PM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
 
   Home   Help Search Login Register Weztec home  
Pages: [1]   Go Down
  Print  
Author Topic: pagination  (Read 3086 times)
0 Members and 1 Guest are viewing this topic.
ed_br
Newbie
*
Offline Offline

Posts: 17


View Profile
« on: 15, 08 2007, 07:51:53 AM »

I have been trying to use paging for a record set.
the results are passed to the page from a url link which is fine but when i click on the next links the link is not being passed and i get no  results. can anyone help with my code? ineed to pass $var onto following pages I guess but i cant see how


Code:
<?php

  
// Get the search variable from URL

  
$var = @$_GET['recordID'] ;
  
$trimmed trim($var); //trim whitespace from the stored variable

// rows to return
$limit=10

// check for a search parameter
if (!isset($var))
  {
  echo 
"<p>We dont seem to have a search parameter!</p>";
  exit;
  }

//connect to your database ** EDIT REQUIRED HERE **
mysql_connect("localhost","####","@@@@@"); //(host, username, password)

//specify database ** EDIT REQUIRED HERE **
mysql_select_db("Database") or die("Unable to select database"); //select which database we're using

// Build SQL Query  
$query "select * from TABLE NAME where field1 like \"%$trimmed%\"  
  order by created DESC"
// EDIT HERE and specify your table and field names for the SQL query

 
$numresults=mysql_query($query);
 
$numrows=mysql_num_rows($numresults);



if (
$numrows == 0)
  {
  echo 
"<h4>Results</h4>";
  echo 
"<p>Sorry, your search returned zero results</p>";


  }

// next determine if s has been passed to script, if not use 0
  
if (empty($s)) {
  
$s=0;
  }

// get results
  
$query .= " limit $s,$limit";
  
$result mysql_query($query) or die("Couldn't execute query");


// begin to show results set
 
print "<div class= \"header\">";
 
 print 
"<div class= \"cellh\">";
echo 
"Date<br />";
print 
"</div>";

 print 
"<div class= \"cellhtitle\">";
echo 
"</b>Title</b>";
print 
"</div>";

 print 
"<div class= \"cellh\">";
echo 
"Location<br />";
print 
"</div>";



 print 
"</div>";


$count $s ;

// now you can display the results returned
  
while ($rowmysql_fetch_array($result)) {
  
$title $row["created"]  ;
 
$title date("d/m/y");
  
 print 
"<div class= \"job\">";
 print 
"<div class= \"cell\">";

 echo 
"&nbsp;$title "
       
 print 
"</div>";
 print "<div class= \"celltitle\">";
 echo  $row["company"] ;
         print "</div>";
   print "<div class= \"cell\">";
echo  $row["city"];
    print "</div>";

 print "</div>";



 print 
"</div>";


  
$count++ ;
  }

$currPage = (($s/$limit) + 1);

//break before paging
  
echo "<br />";

  
// next we need to do the links to other results
  
if ($s>=1) { // bypass PREV link if s is 0
  
$prevs=($s-$limit);
  print 
"&nbsp;<a href=\"$PHP_SELF?s=$prevs&q=$var\">&lt;&lt; 
  Prev</a>&nbsp&nbsp;"
;
  }

// calculate number of pages needing links
  
$pages=intval($numrows/$limit);

// $pages now contains int of pages needed unless there is a remainder from division

  
if ($numrows%$limit) {
  
// has remainder so add one page
  
$pages++;
  }

// check to see if last page
  
if (!((($s+$limit)/$limit)==$pages) && $pages!=1) {

  
// not last page so give NEXT link
  
$news=$s+$limit;

  echo 
"&nbsp;<a href=\"$PHP_SELF?s=$news&q=$var\">Next &gt;&gt;</a>";
  }

$a $s + ($limit) ;
  if (
$a $numrows) { $a $numrows ; }
  
$b $s ;
  echo 
"<p>Showing results $b to $a of $numrows</p>";
  
?>
Logged
Weztec Support Team
Administrator
Newbie
*****
Offline Offline

Posts: 38



View Profile
« Reply #1 on: 15, 08 2007, 01:52:22 PM »

Hi Ed_Br

try this...

Code:
<?php

  
// Get the search variable from URL
  // Added by Weztec
  
$s = @$_GET['s'];

  
$var = @$_GET['recordID'] ;
  
$trimmed trim($var); //trim whitespace from the stored variable

// rows to return
$limit=10;

// check for a search parameter
if (!isset($var))
  {
  echo 
"<p>We dont seem to have a search parameter!</p>";
  exit;
  }

//connect to your database ** EDIT REQUIRED HERE **
mysql_connect("localhost","####","@@@@@"); //(host, username, password)

//specify database ** EDIT REQUIRED HERE **
mysql_select_db("Database") or die("Unable to select database"); //select which database we're using

// Build SQL Query
// Edited by Weztec
$query "select * from TABLE NAME where field1 like '%$trimmed%' order by created DESC"// EDIT HERE and specify your table and field names for the SQL query

//$query = "select * from TABLE NAME where field1 like \"%$trimmed%\"
//  order by created DESC"; // EDIT HERE and specify your table and field names for the SQL query

 
$numresults=mysql_query($query);
 
$numrows=mysql_num_rows($numresults);



if (
$numrows == 0)
  {
  echo 
"<h4>Results</h4>";
  echo 
"<p>Sorry, your search returned zero results</p>";


  }

// next determine if s has been passed to script, if not use 0
  
if (empty($s)) {
  
$s=0;
  }

// get results
  
$query .= " limit $s,$limit";
  
$result mysql_query($query) or die("Couldn't execute query");


// begin to show results set
 
print "<div class= \"header\">";

 print 
"<div class= \"cellh\">";
echo 
"Date<br />";
print 
"</div>";

 print 
"<div class= \"cellhtitle\">";
echo 
"</b>Title</b>";
print 
"</div>";

 print 
"<div class= \"cellh\">";
echo 
"Location<br />";
print 
"</div>";



 print 
"</div>";


$count $s ;

// now you can display the results returned
  
while ($rowmysql_fetch_array($result)) {
  
$title $row["created"]  ;
 
$title date("d/m/y");

 print 
"<div class= \"job\">";
 print 
"<div class= \"cell\">";

 echo 
"&nbsp;$title ";

 print 
"</div>";
 print "<div class= \"celltitle\">";
 echo  $row["company"] ;
         print "</div>";
   print "<div class= \"cell\">";
echo  $row["city"];
    print "</div>";

 print "</div>";



 print 
"</div>";


  
$count++ ;
  }

$currPage = (($s/$limit) + 1);

//break before paging
  
echo "<br />";

  
// next we need to do the links to other results
  
if ($s>=1) { // bypass PREV link if s is 0
  
$prevs=($s-$limit);
  
//Edited by Weztec
  
echo "&nbsp;<a href=" '"' $_SERVER["PHP_SELF"] . "?s=$prevs&q=$var" '"' ">&lt;&lt; Prev</a>&nbsp&nbsp;";
//  print "&nbsp;<a href=\"$PHP_SELF?s=$prevs&q=$var\">&lt;&lt;Prev</a>&nbsp&nbsp;";
  
}

// calculate number of pages needing links
  
$pages=intval($numrows/$limit);

// $pages now contains int of pages needed unless there is a remainder from division

  
if ($numrows%$limit) {
  
// has remainder so add one page
  
$pages++;
  }

// check to see if last page
  
if (!((($s+$limit)/$limit)==$pages) && $pages!=1) {

  
// not last page so give NEXT link
  
$news=$s+$limit;
  
//Edited by Weztec
  
echo "&nbsp;<a href=" '"' $_SERVER["PHP_SELF"] . "?s=$news&q=$var" '"' ">Next &gt;&gt;</a>";
  
//echo "&nbsp;<a href=\"$PHP_SELF?s=$news&q=$var\">Next &gt;&gt;</a>";
  
}

$a $s + ($limit) ;
  if (
$a $numrows) { $a $numrows ; }
  
$b $s ;
  echo 
"<p>Showing results $b to $a of $numrows</p>";

?>


Regards,
Weztec.

Logged
ed_br
Newbie
*
Offline Offline

Posts: 17


View Profile
« Reply #2 on: 16, 08 2007, 02:55:49 AM »

thanks but it is the same, i get the first page telling me 1-10 of 20 results but clicking next link i get warning

Quote
We dont seem to have a search parameter!
Logged
Weztec Support Team
Administrator
Newbie
*****
Offline Offline

Posts: 38



View Profile
« Reply #3 on: 16, 08 2007, 11:52:53 AM »

Hi ed_br,

The calling page needs to have something like search.php?recordID=thingtosearchfor

find the calling code and check or simply hover over the link and see what the url is.

To test the code I fixed simply enter a line after $var = @$_GET['recordID']; reading something like $var = 'thingtosearchfor'; substituting thingtosearchfor with a known field value.

And change the following lines:

echo "&nbsp;<a href=" . '"' . $_SERVER["PHP_SELF"] . "?s=$prevs&q=$var" . '"' . ">&lt;&lt; Prev[/url]&nbsp&nbsp;";
echo "&nbsp;<a href=" . '"' . $_SERVER["PHP_SELF"] . "?s=$news&q=$var" . '"' . ">Next &gt;&gt;[/url]";

to

echo "&nbsp;<a href=" . '"' . $_SERVER["PHP_SELF"] . "?s=$prevs&recordID=$var" . '"' . ">&lt;&lt; Prev[/url]&nbsp&nbsp;";
echo "&nbsp;<a href=" . '"' . $_SERVER["PHP_SELF"] . "?s=$news&recordID=$var" . '"' . ">Next &gt;&gt;[/url]";


Regards,
Weztec.

Logged
ed_br
Newbie
*
Offline Offline

Posts: 17


View Profile
« Reply #4 on: 20, 08 2007, 06:35:18 AM »

Outstanding!! I struggled with this at the weekend and just couldn't get it this works perfectly. Finally I can move on to find the next php minefield.
I am redesigning a site of mine an will  post a link if / when I get it right.
Thank you again for your help and especially your explanation
Logged
Pages: [1]   Go Up
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.8 | SMF © 2006-2008, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Page created in 0.21 seconds with 18 queries.