I was recently looking for a pagination tutorial that used letters of the alphabet instead of numbers. To my surprise all I found was more people looking for the same thing.
Finally I stumbled upon.
http://www.emirplicanic.com/php/php-...orting-script#
But the code found on that page uses the mysql API. After playing around with the mysqli API and getting some help on the forums I came up with the code below. A 100% working alphabetical pagination snippet. If anyone sees any major flaws, or ways to improve this please share.
PHP Code:
<?php
$mysqli = new mysqli("x", "x", "x", "x");
$sort = $_REQUEST['letter'];
if($sort == ""){
$qry= "SELECT * FROM table ORDER BY title ASC " ;
}else{
$qry = "SELECT * FROM table WHERE title LIKE '$sort%' ORDER BY title ASC" ;
}
$execute = $mysqli->query($qry) or die(mysqli_error());
$row_cnt = mysqli_num_rows($execute);
echo "<p>" ;
for ($i = 65; $i < 91; $i++) {
printf('<a href="%s?letter=%s">%s</a> | ',
$PHP_SELF, chr($i), chr($i));
}
echo "</p>" ;
if ($row_cnt > 0) {
do{
while ($row = $execute->fetch_assoc()) {
printf ("%s %s <br />", $row["id"], $row["title"]);
}
}
while ($row = $execute->fetch_assoc());
}else{
echo "<p>No customer found.</p>" ;
}
?>
Please note that both SELECT statements use the row title. This is the row that the code filters by. So if I click the letter 'A' all entries in the title row that start with the letter 'A' will be displayed. If i click on the letter 'P' all entries in the title row starting with P will be displayed. So make this whichever row you'd like to filter by.