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
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>';
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.