Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Regular Coder stevenmw's Avatar
    Join Date
    Jun 2007
    Location
    OK
    Posts
    497
    Thanks
    27
    Thanked 31 Times in 31 Posts

    Alphabetical Pagination That Uses Mysqli API

    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_SELFchr($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.
    Last edited by stevenmw; 03-09-2013 at 06:32 PM.
    Thanks!

  • #2
    Regular Coder stevenmw's Avatar
    Join Date
    Jun 2007
    Location
    OK
    Posts
    497
    Thanks
    27
    Thanked 31 Times in 31 Posts
    Here is a modified version of the script above. This one implements an active state for the selected letter. I've also moved the <li></li> tags so that each individual link is wrapped in them. Making each link an individual list item.

    PHP Code:
    $mysqli = new mysqli("host""user""pass""db");

    $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 
    "<ul>" ;
    for (
    $i 65$i 91$i++) {
        if(
    ord($sort) == $i) {
          
    $class 'active';
        } else {
          
    $class '';
        }

        
    printf('<li><a href="%s?letter=%s" class="%s">%s</a></li>'$PHP_SELFchr($i), $classchr($i));
    }
    echo 
    "</ul>" ;

    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>" ;

    Last edited by stevenmw; 06-08-2013 at 03:09 AM.
    Thanks!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •