Jump to content

Help Add A Paginate Please.


lovephp

Recommended Posts

gould someone help me on how to add pagination on my script? i have no idea on how to get it done

 

 

here is my code

<?php
$user = $login->username;
if($user =='admin'){

$result = mysql_query("SELECT id, aname, cname, hnumber, address, lamount, status, time FROM db_table ORDER BY id DESC");
           $rowNo = 1; //Increment Row Number
           while($res = mysql_fetch_array($result))
           {
						    $id = $res['id'];
               $date = $res['time'];
  						 $newdate = date("D, j M, Y", $date);    
						    if($res['status'] == "Approved"){
               echo "<tr align='center' bgcolor='#CDE861'>";
						    }else{
						    echo "<tr align='center' bgcolor='#EDA553'>";
						    }
						    echo"<td><font color='black'>" .$rowNo++."</font></td>";
               echo"<td><font color='black'>" .$res['aname']."</font></td>";
						    echo"<td><font color='black'>" .strtoupper($res['cname'])."</font></td>";
               echo"<td><font color='black'>" .$res['hnumber']."</font></td>";
               echo"<td><font color='black'>". $res['address']. "</font></td>";
               echo"<td><font color='black'>". $res['lamount']. "</font></td>";
               echo"<td><font color='black'>". $newdate. "</font></td>";
						    if($res['status'] == "Approved"){
						    echo"<td>Approved</td>";
						    }else{
						    echo"<td>Pending</td>";
						    }
echo"<td> <a href ='view.php?id=$id'><center>Click to View </center></a></td>";
if($res['status'] !== "Approved"){
               echo"<td><a href ='accepted.php?id=$id'>Approve</a>";
}else{
               echo"<td><a href ='cancel.php?id=$id'>Cancel</a>";
}
               echo"<td> <a href ='del.php?id=$id'><center>Delete</center></a>";            
               echo "</tr>";
           }
}else{
$result = mysql_query("SELECT id, aname, cname, hnumber, address, lamount, status, time FROM db_table WHERE aname='".$user."' ORDER BY id DESC");
           $rowNo = 1; //Increment Row Number
           while($res = mysql_fetch_array($result))
           {
						    $id = $res['id'];
               $date = $res['time'];
  						 $newdate = date("D, j M, Y", $date);    
						    if($res['status'] == "Approved"){
               echo "<tr align='center' bgcolor='#CDE861'>";
						    }else{
						    echo "<tr align='center' bgcolor='#EDA553'>";
						    }
						    echo"<td><font color='black'>" .$rowNo++."</font></td>";
               echo"<td><font color='black'>" .$res['aname']."</font></td>";
						    echo"<td><font color='black'>" .strtoupper($res['cname'])."</font></td>";
               echo"<td><font color='black'>" .$res['hnumber']."</font></td>";
               echo"<td><font color='black'>". $res['address']. "</font></td>";
               echo"<td><font color='black'>". $res['lamount']. "</font></td>";
               echo"<td><font color='black'>". $newdate. "</font></td>";
						    if($res['status'] == "Approved"){
						    echo"<td>Approved</td>";
						    }else{
						    echo"<td>Pending</td>";
						    }
echo"<td> <a href ='view.php?id=$id'><center>Click to View </center></a></td>";

               echo "</tr>";
           }
}
echo '</tbody>
</table>';
?>

Link to comment
https://forums.phpfreaks.com/topic/270789-help-add-a-paginate-please/
Share on other sites

Your first step would be to form the existing query string in a php variable (this is required for the pagination logic to produce the actual query that gets the rows for the requested logical page.) Once you have the query in a php variable, it would be possible for you to then copy this logic to form the query that gets a count of all the matching rows (used to calculate and limit the highest logical page.)

 

Also, you should eliminate the duplication in your existing code (Don't Repeat Yourself - DRY) so that you don't have a wall of code. Here's your code reworked to serve as a starting place (untested) -

 

<?php
$user = $login->username;

if($user =='admin'){
   $where_clause = '';
} else {
   //not the admin
   $where_clause = "WHERE aname='$user'";
}

$query = "SELECT id, aname, cname, hnumber, address, lamount, status, time FROM db_table $where_clause ORDER BY id DESC";

$result = mysql_query($query);
$rowNo = 1; //Increment Row Number
while($res = mysql_fetch_array($result)){
   $id = $res['id'];
   $date = $res['time'];
   $newdate = date("D, j M, Y", $date);
   if($res['status'] == "Approved"){
       echo "<tr align='center' bgcolor='#CDE861'>";
   }else{
       echo "<tr align='center' bgcolor='#EDA553'>";
   }
   echo"<td><font color='black'>" .$rowNo++."</font></td>";
   echo"<td><font color='black'>" .$res['aname']."</font></td>";
   echo"<td><font color='black'>" .strtoupper($res['cname'])."</font></td>";
   echo"<td><font color='black'>" .$res['hnumber']."</font></td>";
   echo"<td><font color='black'>". $res['address']. "</font></td>";
   echo"<td><font color='black'>". $res['lamount']. "</font></td>";
   echo"<td><font color='black'>". $newdate. "</font></td>";
   if($res['status'] == "Approved"){
       echo"<td>Approved</td>";
   }else{
       echo"<td>Pending</td>";
   }
   echo"<td><a href ='view.php?id=$id'><center>Click to View </center></a></td>";
   if($user =='admin'){
       if($res['status'] !== "Approved"){
           echo"<td><a href ='accepted.php?id=$id'>Approve</a>";
       }else{
           echo"<td><a href ='cancel.php?id=$id'>Cancel</a>";
       }
       echo"<td> <a href ='del.php?id=$id'><center>Delete</center></a>";
   }
   echo "</tr>";
}
echo '</tbody>
</table>';
?>

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.