Home

Sunday, August 14, 2011

Very simple PHP pagination

$per_page = 1;
$page = isset($_GET['page']) ? (int) $_GET['page'] : 1;
$pages = implode(mysql_fetch_assoc(mysql_query("SELECT COUNT(*) FROM register")));
$pages = ceil($pages / $per_page);
$querystring = "";
foreach ($_GET as $key => $value) {
    if ($key != "page") $querystring .= "$key=$value&";
}
echo "Pages: ";
for ($i = 1; $i <= $pages; $i++) {
    echo "<a " . ($i == $page ? "class=\"selected\" " : "");
    echo "href=\"?{$querystring}page=$i";
    echo "\">$i</a> ";
}
$result = mysql_query("SELECT * FROM register LIMIT " . (($page - 1) * $per_page) . ", $per_page");


echo "<table border='1'>
<tr>
<th>Username</th>
<th>Address</th>
<th>Area</th>
<th>Email</th>
<th>City</th>
</tr>";

while($row = mysql_fetch_array($result))
  {
  echo "<tr>";
  echo "<td>" . $row['uname'] . "</td>";
  echo "<td>" . $row['address'] . "</td>";
  echo "<td>" . $row['area'] . "</td>";
  echo "<td>" . $row['email'] . "</td>";
  echo "<td>" . $row['city'] . "</td>";
  echo "</tr>";
  }
echo "</table>";

mysql_close($con);

No comments:

Post a Comment

rathoddhirendra.blogspot.com-Google pagerank and Worth