Log in

View Full Version : split pages?



Dennis_Gull
04-13-2007, 01:20 AM
Hello,

I need to split my pages up if the row result is higher then x numbers but I have no idea on how to do this.. Is there any tutorial on how to do something like this? And also, is it possible to echo how many rows the browser found?

thetestingsite
04-13-2007, 01:23 AM
So, you basically want a pagination script? There are several floating around the internet as well as some here on DD. Take a look at the following script to see if that suits your needs:



<?php
include('dbconnect.php');

// If page is set
if($_GET['page'] && is_numeric($_GET['page'])) {
$page = $_GET['page'];
}

// Set default page
else {
$page = 1;
}

// Max results per page
$max = 7;

// Current page
$cur = (($page * $max) - $max);

// Get data
$qry = mysql_query("SELECT * FROM `test`");
$total = mysql_num_rows($qry); // count results

// Get amount of pages
$total_pages = ($total / $max);

// Is page # more than one
if($page > 1) {
$prev = ($page - 1);
$prevLink = '<a href="?p='.$p.'&page='.$prev.'">&lt;&lt;Previous</a>';
}

// For each page less than total pages
for($i=1;$i<$total_pages+1;$i++) {
// If page = current page
if($page == $i) {
$nav .= ' <b>'.$i.'</b> ';
} else {
// Echo link to previous page
$nav .= ' <a href="?p='.$p.'&page='.$i.'">'.$i.'</a> ';
}
}

// If page is less than total pages, show next link
if($page < $total_pages) {
$next = ($page + 1);
$nextLink = '<a href="?p='.$p.'&page='.$next.'">Next&gt;&gt;</a>';
}

########## End Pagination Script ##########

$info = mysql_query("SELECT * FROM `test` LIMIT $cur, $max");

while($row = mysql_fetch_array($info)) {
echo $row['val'].'
<br />';
}

if ($total_pages > 1) {
echo 'Pages: '.$prevLink . $nav . $nextLink;
}

?>


If not, then you may want to look at http://www.php-mysql-tutorial.com/php-mysql-paging.php and view their tutorial.

Hope this helps.

Dennis_Gull
04-13-2007, 01:45 AM
That code is perfect, big thanks for pointing it out :)

thetestingsite
04-13-2007, 01:48 AM
No problem, let us know if you need any more help.