Forum Moderators: open

Message Too Old, No Replies

Alphabetic Pagination PHP

         

micro123

4:57 am on Oct 30, 2009 (gmt 0)

10+ Year Member



Hi,

I am impressed by the answer I got to my last post.

I have a new query this time:

I am able to pull up student names from database by order and paginate it. What I want is to be able to page it by alphabetic.

For eg: if there are 40 results to be displayed per page, and on page one I have student with A-C displayed and on page 2, I have students C-E displayed. and so on.

What I want is to be able to display A-C as a link to page 1, C-E link to page 2,...

Final result:

<a href-"student.php?pg=1">A-C</a> <a href-"student.php?pg=2">C-E</a> and so on...

Please help

Thanks

rocknbil

6:33 pm on Oct 31, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This is going to all be in your programming, and how you build select statements to pass to mySQL. For the alphabetic organization, easy:

select * from table where field like '%a';

Or for a range, a regexp:

select * from table where field regexp '^[a-cA-C]';

Meaning,

^ starts with. Don't confuse with the "not" character in a class. When done like this, [^A-Z], it means any character not A through Z.

[] defines a character class, so the pattern is anything that contains this range of characters, a through C. Note that "like" is case insensitive, a class is not.

So you could have a function that builds a list of links A through Z by looping through the alphabet.

The "links per page" issue is a little more complex, but it's also going to be in your programming.

- Set a per page variable somewhere - do this first, don't hard code it in.

- write a function that outputs the "foot links" for any given search. That is, **something** like this.

Warning: not working code, sample only


function foot_links () {
global $settings,$qs,$total_records_found;
$per = ($settings['per_page'])?$settings['per_page']:25;
if ($per >= $total_records_found) { return; } // no need for links
for ($i=0; $i<=$total_records_found; $i+=$per){
$start_num=($i==0)?1:$i;
$end_num=$i+$per;
$links .= '<li><a href="' . $your_script . '?start=' . $i;
$links .= '&amp;limit=' . $per . '"
>' . $start_num . '-' . $end_num . '</a></li>';
}
}

There are several things wrong with the above, it's just to give you the logic of creating the links - for example, when you get to the end of the count, as in your example, it's going to set a limit that displays a number exceeding the number of actual records, and since everything starts with 0 (limit 0,40) you have to resolve the math so that it "appears" to start at record 1 and ends on record 40 in the links, then the next page starts on record 41, etc.

Then, when this link is clicked, you would add to the above select statement based on the input.


$select = select * from table where field regexp '^[a-cA-C]';
if (isset($_GET['start])) and ($_GET['start'] > 0)) {
if (isset($_GET['limit])) and ($_GET['limit'] > 0)) {
$select .= ' limit ' . $_GET['start'] . ',' . $_GET['limit'];
}
// for a zero start, the limit number is just limit.
// synonymous with limit 0,40
else { $select .= ' limit ' . $settings[per_page]; }
}

So in your case it should echo something like

select * from table where field regexp '^[a-cA-C]' limit 40;

(or, synonymous,
select * from table where field regexp '^[a-cA-C]' limit 0,40;

Then when "page two" is clicked,

select * from table where field regexp '^[a-cA-C]' limit 40,40;

Meaning start at record 40 of A-C and limit it to 40 records. In mySQL, it doesn't matter that the limit exceeds the records found. But it matters when you display the links on the pages. Should be

1-40 ¦ 41-41

not

1-40 ¦ 41-80

It gets even more complex when you are displaying millions of records and links to them on a single page - the above method will choke most browsers. Beyond the scope of this post, you'd need to define a links range and modify the programming to parse it so you get something like

<< last 40 ¦ 1041-1080 ¦ 1081-2000 ¦ 2001-2040 ¦ next 40 >>

(etc.) The idea is to limit the number of links your system outputs so it doesn't choke the browser.

There is a good thread on pagination with PHP on this site, in the subscription forum, and others that deeply explore this issue. Try pagination links PHP site:webmasterworld.com in Google.