Table Pagination example

The basics

The following examples demonstrate the usage of the pagination function
The examples are used in conjunction with the dataBase object to generate information for the array
The dataBase object can be downloaded and information found here Step one is to instaniate the dataBase object in the conventional way

$dataBase = new DB();

but however any multi-dimensional (associative) array can be used.
Each example builds upon the previous code, as to not cause any confusion and area`s of intrest are highlighted

Example 1

Make a basic database request and paginate the result

$array = $dataBase->getQuery("SELECT * FROM testtable");
paginate($array);

---- Or you could simply say ----

paginate($dataBase->getQuery("SELECT * FROM testtable"));

output:

Displaying 1-10 of 29

123

mem_idmem_usermem_passmem_addressmem_tel
1test_username_1test_password_11 test street, testville, test county1111 1111111
2test_username_2test_password_22 test street, testville, test county2222 2222222
3test_username_3test_password_33 test street, testville, test county3333 3333333
4test_username_4test_password_44 test street, testville, test county4444 4444444
5test_username_5test_password_55 test street, testville, test county5555 5555555
6test_username_6test_password_64 test street, testville, test county6666 6666666
7test_username_7test_password_77 test street, testville, test county7777 7777777
8test_username_8test_password_88 test street, testville, test county8888 8888888
9test_username_9test_password_99 test street, testville, test county9999 9999999
10test_username_atest_password_aa test street, testville, test countyaaaa aaaaaaa
123


Example 2

Giving the fields a title, this is done within the MySql using 'AS'

$SQL = "SELECT mem_id AS Id, mem_user AS Name, mem_pass AS Password, mem_address AS Address, mem_tel AS 'Tel No' FROM testtable";
$array = $dataBase->getQuery($SQL);
paginate($array);

---- Or you could simply say ----

paginate($dataBase->getQuery("SELECT mem_id AS Id, mem_user AS Name, mem_pass AS Password, mem_address AS Address, mem_tel AS 'Tel No' FROM testtable"));

output:

Displaying 1-10 of 29

123

IdNamePasswordAddressTel No
1test_username_1test_password_11 test street, testville, test county1111 1111111
2test_username_2test_password_22 test street, testville, test county2222 2222222
3test_username_3test_password_33 test street, testville, test county3333 3333333
4test_username_4test_password_44 test street, testville, test county4444 4444444
5test_username_5test_password_55 test street, testville, test county5555 5555555
6test_username_6test_password_64 test street, testville, test county6666 6666666
7test_username_7test_password_77 test street, testville, test county7777 7777777
8test_username_8test_password_88 test street, testville, test county8888 8888888
9test_username_9test_password_99 test street, testville, test county9999 9999999
10test_username_atest_password_aa test street, testville, test countyaaaa aaaaaaa
123


Example 3

Special formatting or functions, can be achieved using callback functions.
The callback function is declared by hooking the field name to the function in the argument.
For example, firstly declare the function or functions.

function removeUnderscoreAndMakeRed($val){
echo '<font color="red">'.str_replace ("_", " ", $val).'</font>';
}

function delId($id){
echo '<a href="delete.php?id='.$id.'">Delete<a>';
}

And secondly, the argument or arguments are passed in an associative array that then hooks the field values to the functions

$SQL = "SELECT mem_id AS 'Option', mem_user AS Name, mem_pass AS Password, mem_address AS Address, mem_tel AS 'Tel No' FROM testtable";
$array = $dataBase->getQuery($SQL);
$args = array('Option'=>'delId', 'Name'=>'removeUnderscoreAndMakeRed');
paginate($array,$args);

---- Or you could simply say ----

paginate($dataBase->getQuery("SELECT mem_id AS 'Option', mem_user AS Name, mem_pass AS Password, mem_address AS Address, mem_tel AS 'Tel No' FROM testtable"), array('Option'=>'delId','Name'=>'removeUnderscoreAndMakeRed'));

output:

Displaying 1-10 of 29

123

OptionNamePasswordAddressTel No
Deletetest username 1test_password_11 test street, testville, test county1111 1111111
Deletetest username 2test_password_22 test street, testville, test county2222 2222222
Deletetest username 3test_password_33 test street, testville, test county3333 3333333
Deletetest username 4test_password_44 test street, testville, test county4444 4444444
Deletetest username 5test_password_55 test street, testville, test county5555 5555555
Deletetest username 6test_password_64 test street, testville, test county6666 6666666
Deletetest username 7test_password_77 test street, testville, test county7777 7777777
Deletetest username 8test_password_88 test street, testville, test county8888 8888888
Deletetest username 9test_password_99 test street, testville, test county9999 9999999
Deletetest username atest_password_aa test street, testville, test countyaaaa aaaaaaa
123


Example 4

However you will notice in the above examples when you click to change the page, all the tables move to that page.
This is because they all share the same link.
This can be fixed by passing a link argument, which is then a unique link to this pagination.

function removeUnderscoreAndMakeRed($val){
echo '<font color="red">'.str_replace ("_", " ", $val).'</font>';
}

function delId($id){
echo '<a href="delete.php?id='.$id.'">Delete<a>';
}


$SQL = "SELECT mem_id AS 'Option', mem_user AS Name, mem_pass AS Password, mem_address AS Address, mem_tel AS 'Tel No' FROM testtable";
$array = $dataBase->getQuery($SQL);
$args = array('Option'=>'delId', 'Name'=>'removeUnderscoreAndMakeRed');
paginate($array,$args,'uniqueLink');

---- Or you could simply say ----

function removeUnderscoreAndMakeRed($val){echo '<font color="red">'.str_replace ("_", " ", $val).'</font>';}
function delId($id){echo '<a href="delete.php?id='.$id.'">Delete<a>';}

paginate($dataBase->getQuery("SELECT mem_id AS 'Option', mem_user AS Name, mem_pass AS Password, mem_address AS Address, mem_tel AS 'Tel No' FROM testtable"),array('Option'=>'delId','Name'=>'removeUnderscoreAndMakeRed'),'uniqueLink');

output:

Displaying 1-10 of 29

123

OptionNamePasswordAddressTel No
Deletetest username 1test_password_11 test street, testville, test county1111 1111111
Deletetest username 2test_password_22 test street, testville, test county2222 2222222
Deletetest username 3test_password_33 test street, testville, test county3333 3333333
Deletetest username 4test_password_44 test street, testville, test county4444 4444444
Deletetest username 5test_password_55 test street, testville, test county5555 5555555
Deletetest username 6test_password_64 test street, testville, test county6666 6666666
Deletetest username 7test_password_77 test street, testville, test county7777 7777777
Deletetest username 8test_password_88 test street, testville, test county8888 8888888
Deletetest username 9test_password_99 test street, testville, test county9999 9999999
Deletetest username atest_password_aa test street, testville, test countyaaaa aaaaaaa
123


Example 5

By default the pagination will display 10 results per page, but this can implicitly set
For example

function removeUnderscoreAndMakeRed($val){
echo '<font color="red">'.str_replace ("_", " ", $val).'</font>';
}

function delId($id){
echo '<a href="delete.php?id='.$id.'">Delete<a>';
}


$SQL = "SELECT mem_id AS 'Option', mem_user AS Name, mem_pass AS Password, mem_address AS Address, mem_tel AS 'Tel No' FROM testtable";
$array = $dataBase->getQuery($SQL);
$args = array('Option'=>'delId', 'Name'=>'removeUnderscoreAndMakeRed');
paginate($array,$args,'uniqueLink1',3);

---- Or you could simply say ----

function removeUnderscoreAndMakeRed($val){echo '<font color="red">'.str_replace ("_", " ", $val).'</font>';}
function delId($id){echo '<a href="delete.php?id='.$id.'">Delete<a>';}

paginate($dataBase->getQuery("SELECT mem_id AS 'Option', mem_user AS Name, mem_pass AS Password, mem_address AS Address, mem_tel AS 'Tel No' FROM testtable"),array('Option'=>'delId','Name'=>'removeUnderscoreAndMakeRed'),'uniqueLink1',3);

output:

Displaying 1-3 of 29

123 ... Last

OptionNamePasswordAddressTel No
Deletetest username 1test_password_11 test street, testville, test county1111 1111111
Deletetest username 2test_password_22 test street, testville, test county2222 2222222
Deletetest username 3test_password_33 test street, testville, test county3333 3333333
123 ... Last


Example 6

Finally the scope of how much pagination can be set
For example

function removeUnderscoreAndMakeRed($val){
echo '<font color="red">'.str_replace ("_", " ", $val).'</font>';
}

function delId($id){
echo '<a href="delete.php?id='.$id.'">Delete<a>';
}


$SQL = "SELECT mem_id AS 'Option', mem_user AS Name, mem_pass AS Password, mem_address AS Address, mem_tel AS 'Tel No' FROM testtable";
$array = $dataBase->getQuery($SQL);
$args = array('Option'=>'delId', 'Name'=>'removeUnderscoreAndMakeRed');
paginate($array,$args,'uniqueLink2',3,1);

---- Or you could simply say ----

function removeUnderscoreAndMakeRed($val){echo '<font color="red">'.str_replace ("_", " ", $val).'</font>';}
function delId($id){echo '<a href="delete.php?id='.$id.'">Delete<a>';}

paginate($dataBase->getQuery("SELECT mem_id AS 'Option', mem_user AS Name, mem_pass AS Password, mem_address AS Address, mem_tel AS 'Tel No' FROM testtable"),array('Option'=>'delId','Name'=>'removeUnderscoreAndMakeRed'),'uniqueLink2',3,1);

output:

Displaying 1-3 of 29

12 ... Last

OptionNamePasswordAddressTel No
Deletetest username 1test_password_11 test street, testville, test county1111 1111111
Deletetest username 2test_password_22 test street, testville, test county2222 2222222
Deletetest username 3test_password_33 test street, testville, test county3333 3333333
12 ... Last

At this point the code may look pretty large but bare in mind, that each example has built upon the previous, and at this point this example has a large amount of arguments associated with its output

Questions / Comments

I will be happy to answer any questions
But, If you have no questions instead, please take a min to leave a comment, it shows that this guide was or was`nt some use to you, this in turn shapes the internet overall for the future, cause at the end of the day you will end up with more things that ya want!
Please leave any questions or comments here