Created
January 14, 2018 03:19
-
-
Save eimg/9bf6f530793d1948c24ffaa8553bb05c to your computer and use it in GitHub Desktop.
PHP paging - sample code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$conn = mysqli_connect("localhost", "root", ""); | |
mysqli_select_db($conn, "test"); | |
$start = isset($_GET['start']) ? $_GET['start'] : 0; | |
$result = mysqli_query($conn, "SELECT * FROM countries LIMIT $start, 10"); | |
$total = mysqli_query($conn, "SELECT * FROM countries"); | |
$total = mysqli_num_rows($total); | |
$total = floor($total / 10); | |
?> | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<title></title> | |
</head> | |
<body> | |
<table border="1"> | |
<tr> | |
<th>Name</th> | |
<th>ISO 2</th> | |
<th>Phone Code</th> | |
</tr> | |
<?php while($row = mysqli_fetch_assoc($result)): ?> | |
<tr> | |
<td><?= $row['name'] ?></td> | |
<td><?= $row['iso2'] ?></td> | |
<td><?= $row['phone_code'] ?></td> | |
</tr> | |
<?php endwhile; ?> | |
</table> | |
<?php $prev = $start - 10; if($prev < 0) $prev = 0; ?> | |
<a href="test.php?start=<?= $prev ?>">« Previous</a> | |
<?php for($i = 0; $i < $total; $i++): ?> | |
<?php $page = $i * 10; ?> | |
<a href="test.php?start=<?= $page ?>"><?= $i + 1 ?></a> - | |
<?php endfor; ?> | |
<?php $next = $start + 10; ?> | |
<a href="test.php?start=<?= $next ?>">Next »</a> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment