Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,153,015 members, 7,817,990 topics. Date: Sunday, 05 May 2024 at 02:15 AM

Pagination - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Pagination (1395 Views)

Nairaland : Please Add 1 To The Pagination Counter Not Everyone Is A Programmer / Urgent Help Need! Pagination With Php / Pagination Vs Infinite Scroll (2) (3) (4)

(1) (Reply) (Go Down)

Pagination by gateluv: 1:30am On Jun 20, 2012
Hi programmers in the house. I want to display results out of database using pagination because it's is showing all the 100 results at once. I would be happy if a source code can be given out. I want PHP language. Thanks
Re: Pagination by Chimanet(m): 9:58am On Jun 20, 2012
U didnt even bother 2 say d language u want d code in or even paste ur cold let c hw fa u have gone, i now see y d programming section is dieing slowly. Are u sure u wrote or even configured d first code urself at all?
Re: Pagination by instinctg(m): 9:11pm On Jun 20, 2012
Re: Pagination by djynnius(m): 3:57am On Jun 28, 2012
My contribution to you. take a look at the function below and hopefully it should help

/*function taking arguments for databse table, 
number of results you wish to display set to a default of 10 and
the current page you are viewing*/
function pagination($table, $num_of_results = 10, $current_page = 1){
/*connection string.*/
/*dont forget to enter the appropriate login credentials and database name.*/
$connect = mysql_connect(localhost, dba_username, dba_password);
$db = mysql_select_db(db_name, $connect);

/*check to see if the page id is set in the url*/
$current_page = (isset($_GET['pid'])) ? $_GET['pid'] : 1;

/*setting url for controls so you dont have to bother about the current url in using the function*/
$page_url = $_SERVER['REQUEST_URI'];
$page_url = explode("?", $page_url);
$page_url = $page_url[0];

/*querry to count all results*/
$sql = "select id from ".$table;
$query = mysql_query($sql);
$total_result_count = mysql_num_rows($query);

/*offset being what number the querry results should begin from to count the set number of results*/
$offset = ($current_page - 1)*$num_of_results;

/*querry to return desired results*/
$sql = "select * from $table limit $offset, $num_of_results";
$query = mysql_query($sql);

/*page controls*/
$p = 1;
$page_num = ceil($total_result_count/$num_of_results); //returns a rounded up value for number of expected pages
if(isset($_GET['pid']) and $_GET['pid'] != 1){
$page_ctrl = "<a href='".$page_url."?pid=".($_GET['pid'] - 1)."'>Previous</a> | ";
} else {
$page_ctrl = "Previous | ";
}
while($p <= $page_num){
$page_ctrl .= "<a href='".$page_url."?pid=".$p."'>".$p."</a>| ";
$p++;
}
if(isset($_GET['pid']) and $_GET['pid'] != $page_num){
$page_ctrl .= "<a href='".$page_url."?pid=".($_GET['pid'] + 1)."'>Next</a> ";
} else {
$page_ctrl .= "Next ";
}
echo $page_ctrl;


/*begin results table*/
echo "<table id='paginated'>"; //usually smart to give the table an id so you can style it later
echo "<tr><thead><tr><th>SN</th><th>ID</th><th>Other Columns</th></tr></thead></tr>";

$i = ($offset + 1);
while($result = mysql_fetch_assoc($query)){
/*dont forget to use the appropriate table names*/
echo "<tr><td>".$i."</td><td>{$result['id']}</td><td>{$result['subject']}</td></tr>";
$i++;
}
echo "</table>";

echo $page_ctrl;

mysql_close();
}



/*to test simply run the function as below replacing the argument with a valid table name*/
pagination("table_name"wink;
Re: Pagination by pmatrixr2: 12:09am On Jun 29, 2012
Can be solved here. use the ask question link
http:
http://trimer247.org

(1) (Reply)

Facebook Launches Techprep: ‘there Will Be Over 1M Programming Jobs By 2020’ / Wants To Deploy A Codeigniter Project Live / Simple Java Challenge

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 19
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.