Hiya! How do I create a pagination function that can be used multiple times, with any MySQL query? Many Thanks,
Hi, I have posted an article on my blog on how to do so http://blog.alexhanin.info/2009/09/21/php-pagination-made-easy/ aXe
Excellent, thanks for that axelay. But, how do I turn that into a function that can be used with any MySQL query. Can you give me an example?
It really is not that difficult if you know how to PHP even a little What I gave you should be enough for you to start customising it aXe
I been using Zend's Paginator function. It works for database, array etc. http://framework.zend.com/manual/en/zend.paginator.html
I have looked at the code you provided, but I can't work out how you would add the limit to the SQL query. If you could provide a function example, it would be much appreciated Also, how do I adapt the code, so it can be used to display the results of any query? I have had a go! Can you point me in the right direction? $getusers= "SELECT firstname, lastnameFROM users"; paginate($getusers, 0); function paginate($query, $start) { $query .= " LIMIT 0, 5"; $query = mysql_query($query); echo "sss".mysql_num_rows($query); while($row = mysql_fetch_array($query)) { echo $row['firstname'] . " " . $row['lastnameFROM ']; echo "<br />"; } } Code (PHP):
$getusers= "SELECT firstname, lastname FROM users"; paginate($getusers, 0, 5); function paginate($query, $start, $count) { $query .= " LIMIT $start, $count"; $query = mysql_query($query); echo "sss".mysql_num_rows($query); while($row = mysql_fetch_array($query)) { echo $row['firstname'] . " " . $row['lastnameFROM ']; echo "<br />"; } } PHP: That is not working? aXe
Well, it is working, but what about the data (Firstname & Lastname)? That won't necessarily be the same for each query. What do I need to do in order to make it work with any query, and display the associated query results.
function paginate($query, $start, $count) { $query .= " LIMIT $start, $count"; $query = mysql_query($query); return $query; } $getusers= "SELECT firstname, lastname FROM users"; $data = paginate($getusers, 0, 5); echo "sss".mysql_num_rows($data); while($row = mysql_fetch_array($data)) { echo $row['firstname'] . " " . $row['lastnameFROM ']; echo "<br />"; } PHP: Would work but is kinda pointless aXe
So is it not possible to make a pagination function that can be used multiple times, with any MySQL query?
A "Pagination Function" is basically a function that can be used multiple times, in order to paginate results. The problem I have, is would it be possible to make such a function to be used for any query, and if so, how?