John
John

Reputation: 4944

Using a count ++ function with pagination

The HTML table below is paginated. On page one, $count++ starts at one and then goes up, which is what I want.

The problem is that when I click on page two, $count++ starts at one again.

How can I make it start at 101 on page two?

$presult = mysql_query("SELECT COUNT(*) FROM login") or die(mysql_error());

$rr = mysql_fetch_row($presult);  
$numrows = $rr[0]; 
$rowsperpage = 100; 
$totalpages = ceil($numrows / $rowsperpage);

// get the current page or set a default  
if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) {  
   // cast var as int  
   $currentpage = (int) $_GET['currentpage'];  
} else {  
   // default page num  
   $currentpage = 1;  
} // end if  

// if current page is greater than total pages...  
if ($currentpage > $totalpages) {  
   // set current page to last page  
   $currentpage = $totalpages;  
} // end if  
// if current page is less than first page...  
if ($currentpage < 1) {  
   // set current page to first page  
   $currentpage = 1;  
} // end if  

// the offset of the list, based on current page   
$offset = ($currentpage - 1) * $rowsperpage; 

    $tzFrom3 = new DateTimeZone('America/New_York'); 
    $tzTo3 = new DateTimeZone('America/Phoenix'); 


    $sqlStr3 = "SELECT 
        loginid, 
        username, 
        created,
        activated

    FROM login  


    WHERE activated = 1


    ORDER BY created ASC 
    LIMIT $offset, $rowsperpage";




    $result = mysql_query($sqlStr3);

    $arr = array();
    echo "<table>";
        while ($row = mysql_fetch_array($result)) { 
                $dt3 = new DateTime($row["created"], $tzFrom3); 
                $dt3->setTimezone($tzTo3);

                echo '<tr class="backgroundnonttsu">';
                echo '<td>'.$count++.'</td>';
                echo '<td >'.stripslashes($row["username"]).'</a></td>';
                echo '<td >'.$dt3->format('F j, Y').'</td>';

                echo '</tr>';

            }
    echo "</table>";

$range = 3;  

/******  build the pagination links ******/  
// range of num links to show    

// if not on page 1, don't show back links  
if ($currentpage > 1) {  
   // show << link to go back to page 1  
   echo " <div class='pages'><a href='http://www.domain.com/directory/file.php?currentpage=1' class='links'><<</a></div> ";  
   // get previous page num  
   $prevpage = $currentpage - 1;  
   // show < link to go back to 1 page  
   echo " <div class='pages'><a href='http://www.domain.com/directory/file.php?currentpage=$prevpage' class='links'><</a></div> ";  
} // end if   

// loop to show links to range of pages around current page  
for ($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++) {  
   // if it's a valid page number...  
   if (($x > 0) && ($x <= $totalpages)) {  
      // if we're on current page...  
      if ($x == $currentpage) {  
         // 'highlight' it but don't make a link  
         echo " <div class='pages'>[<b>$x</b>] </div>";  
      // if not current page...  
      } else {  
         // make it a link  
     echo " <div class='pages'><a href='http://www.domain.com/directory/file.php?currentpage=$x' class='links'>$x</a></div> ";  
      } // end else  
   } // end if   
} // end for  

// if not on last page, show forward and last page links      
if ($currentpage != $totalpages) {   
   // get next page  
   $nextpage = $currentpage + 1;  
    // echo forward link for next page   
   echo " <div class='pages'><a href='http://www.domain.com/directory/file.php?currentpage=$nextpage' class='links'>></a></div> ";  
   // echo forward link for lastpage  
   //echo " <div class='pages'><a href='http://www.domain.com/directory/file.php?currentpage=$totalpages' class='links'>>></a></div> ";  
} // end if  
/****** end build pagination links ******/

Upvotes: 1

Views: 232

Answers (2)

joshuahedlund
joshuahedlund

Reputation: 1782

Just do

$count = $offset + 1 

right after you set offset. You only need one line of code.

Upvotes: 0

user557846
user557846

Reputation:

if($currentpage==1){
$count=1;
}else{
$count =1+($currentpage*$rowsperpage);
}

Upvotes: 1

Related Questions