Pagination, a topic that's arose a lot at this website this past week, lol, now I'm adding to it
With all the topics that have arose this past week, it got me thinking that pagination is something I haven't yet put into parts of my project that are finished, so I've decided to get to work on it. Previously when I was working on a project, I was lazy and used someone else's code, but no longer will I do that, with anything, how else will I learn? I've found a really easy tutorial at Pixel2Life that I'm using as a guideline, and I also plan on using some code that was posted here to shorten the numbered links.
Here's the code I've got, which works great!
php Code:
<?phpinclude('configs/functions.php');
// Check if page is set$page =
isset($_GET[
'page']
) ?
$_GET[
'page'] :
1;
// Set maximum number of results to return$max =
2;
// Calculate what page number their on$res =
ceil(($page *
$max) -
$max);
// Get the data you require from the database$query =
mysql_query(sprintf("
SELECT * FROM poll_options
ORDER BY poll_options_id DESC
LIMIT %d, %d",
$res,
$max)) or
die(mysql_error());
// Fetch the datawhile($row =
mysql_fetch_array($query)) { echo $row[
'choice'].
'<br />';
}// Count how many results are in the table$count_query =
mysql_query("
SELECT * FROM poll_options") or
die(mysql_error());
// Count the total$total =
mysql_num_rows($count_query);
// How many pages will there be?$total_pages =
ceil($total /
$max);
/* Work out the number of links needed */// If page number is more than 1...if($page >
1) { // Take one away from the current page number $prev =
($page -
1);
// Print a 'previous' link $links =
'<a href="?page='.
$prev.
'">« Previous</a> ';
}// Page numbersfor($i =
1;
$i <=
$total_pages;
$i++
) { // If page number is same as current number if($page ==
$i) { $links .=
'<strong>'.
$i.
'</strong> ';
} else { $links .=
'<a href="?page='.
$i.
'">'.
$i.
'</a> ';
}}// Work out if theirs a next pageif($page <
$total_pages) { // Increment from the page number $next =
($page +
1);
// Print a 'next' link $links .=
'<a href="?page='.
$next.
'">Next »</a>';
}echo $links;
?>
This will go in my 'functions.php' file, and I want to use the same functions for different queries to paginate. I thought of splitting that code into two functions, and keeping the query seperate, like so...
php Code:
<?phpinclude('configs/functions.php');
function results
($max_num) { global $max;
// Check if page is set $page =
isset($_GET[
'page']
) ?
$_GET[
'page'] :
1;
// Set maximum number of results to return $max =
$max_num;
// Calculate what page number their on $res =
ceil(($page *
$max) -
$max);
return $res;
}function pagiLinks
() { global $max;
// Count how many results are in the table $count_query =
mysql_query("
SELECT * FROM poll_options" ) or
die(mysql_error());
// Count the total $total =
mysql_num_rows($count_query);
// How many pages will there be? $total_pages =
ceil($total /
$max);
/* Work out the number of links needed */ // If page number is more than 1... if($page >
1) { // Take one away from the current page number $prev =
($page -
1);
// Print a 'previous' link $links =
'<a href="?page='.
$prev.
'">« Previous</a> ';
} // Page numbers for($i =
1;
$i <=
$total_pages;
$i++
) { // If page number is same as current number if($page ==
$i) { $links .=
'<strong>'.
$i.
'</strong> ';
} else { $links .=
'<a href="?page='.
$i.
'">'.
$i.
'</a> ';
} } // Work out if theirs a next page if($page <
$total_pages) { // Increment from the page number $next =
($page +
1);
// Print a 'next' link $links .=
'<a href="?page='.
$next.
'">Next »</a>';
} return $links;
}// Call function to get resultsresults
(2);
// Get the data you require from the database$query =
mysql_query(sprintf("
SELECT * FROM poll_options
ORDER BY poll_options_id DESC
LIMIT %d, %d",
$res,
$max)) or
die(mysql_error());
// Fetch the datawhile($row =
mysql_fetch_array($query)) { echo $row[
'choice'].
'<br />';
}echo pagiLinks
();
?>
This only displays two results and shows numbered links and a next link (but no previous link) and when you change the page number, it doesn't show the other rows. I know this is because with some of the important code been in functions, that
$res and
$max equal
0. Can you even use a variable stored inside a function, outside of the function, by just say
echo'ing it out?
I thought that putting the code in those two functions like I have would save me time in re-writing the code over and over when I wanted to paginate some queries.
Hope I make some sense to you, and any help I get, like always would be appreciated!
P.S. I don't use classes in this project, so if you could avoid any solutions using classes, that'd be great