Go Back   CodingForums.com > :: Server side development > PHP > Post a PHP snippet

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-09-2013, 06:29 PM   PM User | #1
stevenmw
Regular Coder

 
stevenmw's Avatar
 
Join Date: Jun 2007
Location: OK
Posts: 446
Thanks: 26
Thanked 30 Times in 30 Posts
stevenmw is an unknown quantity at this point
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.
__________________
--
Thanks!

Last edited by stevenmw; 03-09-2013 at 06:32 PM..
stevenmw is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:59 PM.


Advertisement
Log in to turn off these ads.