lovephp Posted November 16, 2012 Share Posted November 16, 2012 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>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/270789-help-add-a-paginate-please/ Share on other sites More sharing options...
Beeeeney Posted November 16, 2012 Share Posted November 16, 2012 http://www.phpfreaks.com/tutorial/basic-pagination Quote Link to comment https://forums.phpfreaks.com/topic/270789-help-add-a-paginate-please/#findComment-1392959 Share on other sites More sharing options...
lovephp Posted November 16, 2012 Author Share Posted November 16, 2012 still not getting how to implant it to my script Quote Link to comment https://forums.phpfreaks.com/topic/270789-help-add-a-paginate-please/#findComment-1393038 Share on other sites More sharing options...
PFMaBiSmAd Posted November 16, 2012 Share Posted November 16, 2012 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>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/270789-help-add-a-paginate-please/#findComment-1393046 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.