i want to display the results of a sql statement in several pages
i have 6 rows .and i set the page limit to 2 rows
so i have 3 pages
i followed some tutorial online :
http://php.about.com/od/phpwithmysql/ss/php_pagination_3.htm
but when i click next i have no results to display !!
the while loop just selects the 1st 2 rows only ???

if (!(isset($pagenum))) 
     { 
       $pagenum = 1; 
     }
   
   $data=mysql_query("select title from news ");
   $rows = mysql_num_rows($data);  
   $page_rows = 2; //assuming 2 titles per page 
   $last = ceil($rows/$page_rows); //or last page
 //this makes sure the page number isn't below one, or more than our maximum pages 
    if ($pagenum < 1) 
    { 
      $pagenum = 1; 
     } 
    elseif ($pagenum > $last) 
     { 
    $pagenum = $last; 
     } 
     
     //This sets the range to display in our query 
    $max = 'limit ' .($pagenum - 1) * $page_rows .',' .$page_rows;  

  $data_p = mysql_query("SELECT * from news $max");
  echo "<table border='2' align='center' height='80%' width='80%' >";

    while($row = mysql_fetch_array( $data_p )) 
    	{ 
               $id=$row["id"];
	           echo "<tr>";
	          echo "<td>";
              echo "<LI>".$row["title"];
                echo "</td>";
                     echo"<td> <a href='edit_news.php?id=$id'> Edit"; echo "</td>";
                 echo"<td><a href='#' onClick='confirm_delete($id)'> Delete</LI>"; echo "</td>";
               echo"</tr>";
             }
             
            // This shows the user what page they are on, and the total number of pages
          echo " --Page $pagenum of $last-- <p>";
            /* First we check if we are on page one. If we are then we don't need a link to the previous page or the first page
            	 so we do nothing. If we aren't then we generate links to the first page, and to the previous page.*/
       if ($pagenum == 1) 
         {
          } 
       else 
         {
           echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=1'> <<-First</a> ";
           echo " ";
            $previous = $pagenum-1;
           echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$previous'> <-Previous</a> ";       
          }
      //just a spacer
      echo " ---- ";
       //This does the same as above, only checking if we are on the last page, and then generating the Next and Last links
        if ($pagenum == $last) 
          {
           } 
         else {
            $next = $pagenum+1;
             echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$next'>Next -></a> ";
             echo " ";
             echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$last'>Last ->></a> ";
              }

hello:
do work on this code:

// how many rows to show per page
$rowsPerPage = 20;

// by default we show first page
$pageNum = 1;

// if $_GET['page'] defined, use it as page number
if(isset($_GET['page']))
{
	$pageNum = $_GET['page'];
}

// counting the offset
$offset = ($pageNum - 1) * $rowsPerPage;

$query  = "SELECT * FROM sometableLIMIT $offset, $rowsPerPage";
$result = mysql_query($query) or die('Error, query failed');

// print the random numbers
while($row = mysql_fetch_array($result))
{
	echo $row['val'] . '<br>';
}
echo '<br>';

// how many rows we have in database
$query   = "SELECT COUNT(val) AS numrows FROM randoms";
$result  = mysql_query($query) or die('Error, query failed');
$row     = mysql_fetch_array($result, MYSQL_ASSOC);
$numrows = $row['numrows'];

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

// print the link to access each page
$self = $_SERVER['PHP_SELF'];
$nav = '';
for($page = 1; $page <= $maxPage; $page++)
{
	if ($page == $pageNum)
	{
		$nav .= " $page ";   // no need to create a link to current page
	}
	else
	{
		$nav .= " <a href=\"$self?page=$page\">$page</a> ";
	}		
}

// creating previous and next link
// plus the link to go straight to
// the first and last page

if ($pageNum > 1)
{
	$page = $pageNum - 1;
	$prev = " <a href=\"$self?page=$page\">[Prev]</a> ";
	
	$first = " <a href=\"$self?page=1\">[First Page]</a> ";
} 
else
{
	$prev  = '&nbsp;'; // we're on page one, don't print previous link
	$first = '&nbsp;'; // nor the first page link
}

if ($pageNum < $maxPage)
{
	$page = $pageNum + 1;
	$next = " <a href=\"$self?page=$page\">[Next]</a> ";
	
	$last = " <a href=\"$self?page=$maxPage\">[Last Page]</a> ";
} 
else
{
	$next = '&nbsp;'; // we're on the last page, don't print next link
	$last = '&nbsp;'; // nor the last page link
}

// print the navigation link
echo $first . $prev . $nav . $next . $last;

// and close the database connection
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.