Page 1 of 1

Pagination page

Posted: Thu Nov 17, 2011 5:21 pm
by louiegiezer
Hey Guys!

I'm planning to implement the pagination tutorial in my website made by Mr.Jacek so in this tutorial instead of the next page is a number i want to change it in Next and Previous is this possible?

Re: Pagination page

Posted: Thu Nov 17, 2011 10:16 pm
by jacek
Yes.

Re: Pagination page

Posted: Fri Nov 18, 2011 7:52 am
by Temor
Yes. Very easily even.

If you're having troubles figuring it out, let me know :)

Re: Pagination page

Posted: Fri Nov 18, 2011 9:09 am
by louiegiezer
thanks. Lets begin :lol:

Re: Pagination page

Posted: Fri Nov 18, 2011 3:21 pm
by Temor
show us what you have so far :)

Re: Pagination page

Posted: Fri Nov 18, 2011 4:42 pm
by louiegiezer
here's the code


user_list.php
[syntax=php]
<?php

include('core/init.inc.php');

$page = (isset($_GET['page'])) ? (int)$_GET['page'] : 1;
?>

<html>
<head>
<title>Pagination Test</title>
</head>
<body>
<div>
<?php
foreach (fetch_users($page, 5) as $user){
echo "<p>{$user}</p>";
}

$total_pages = ceil(fetch_total_users() / 5);

for ($i = 1; $i <= $total_pages; ++$i){
echo " <a href=\"?page={$i}\">{$i}</a> ";
}

?>
</div>
</body>
</html>

[/syntax]


user.inc.php
[syntax=php]
<?php

function fetch_users($page, $per_page){
$start = (int)($page -1) * $per_page;
$per_page = (int)$per_page;

$query = mysql_query("SELECT `user_name` FROM `tester` LIMIT {$start}, {$per_page}");

while (($row = mysql_fetch_assoc($query)) !== false){
$users[] = $row['user_name'];
}
return $users;
}

function fetch_total_users(){
$result = mysql_query('SELECT COUNT(`user_id`)FROM `tester`');

return mysql_result($result, 0);
}
?>
[/syntax]

so how to make this prev and next

Re: Pagination page

Posted: Sun Nov 20, 2011 12:21 am
by jacek
Well the next page is current_page + 1 so

[syntax=php]echo '<a href="?page=', ($page + 1), '">Next</a>';[/syntax]
should be a good place to start.

Re: Pagination page

Posted: Sun Nov 20, 2011 6:31 pm
by louiegiezer
:cry: well its better if you give the exact code in prev and next..

Re: Pagination page

Posted: Sun Nov 20, 2011 7:18 pm
by bowersbros
Jacek has given you exactly what you need to figure it out for yourself.

You wont learn if he provides code for you, and frankly. Why should he do it for you? Its your website; Not his.

The code that Jacek gave will work, however you will need to adapt it slightly and enhance it abit more. Just give it a try.