Members Page Pagination
Okay everyone! this was requested on the forums and such so i thought i would throw it together ^_^
So first and really only thing you have to do is open your members.php and add the things i have added below.
PHP Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<?php
session_start(); //allows session
include "config.php";
if(!$_GET[page]){ //check if there is a page number
$page = 1; //if there isn't the page is 1
}else{
$page = $_GET[page]; //if there is a page then it is what the page is
} //end page check
$max_results = 10; //members per page
$result_math = (($page * $max_results) - $max_results); //work out results to show
echo "<center>";
if(isset($_GET['user'])){ //if there trying to view a profile
//gets the user name and makes it safe
$username = addslashes($_GET[user]);
//querys the db to find the username
$getuser = mysql_query("SELECT * FROM `members` WHERE `username` = '$username'");
//checks see if the username exists in the db
$usernum = mysql_num_rows($getuser);
//if it don't exist
if ($usernum == 0)
{
//don't exist
echo ("User Not Found");
}
//if it does exist then show there profile
else
{
$user = mysql_fetch_array($getuser);
echo "<fieldset style='width: 350'>
<b>$user[username]'s Profile</b><br><br>
Email: $user[email]<br>
Location: $user[location]<br>
Sex: $user[sex]<br>
Age: $user[age]
</fieldset>";
}
}
else
{
//gets all the members from the database
$getusers = mysql_query("SELECT * FROM `members` ORDER BY `id` DESC LIMIT $result_math, $max_results") or die(mysql_error()); //get users limiting by result math and max results
$total_members = mysql_num_rows($getusers); //count members
$total_pages = ceil($total_members / $max_results); //make total pages
//loops there name out
while ($user = mysql_fetch_array($getusers))
{
echo "<a href='members.php?user=$user[username]'>$user[username]</a><br>";
}
if($page > 1){ //if the page number is bigger than 1
$prev = ($page - 1); //previous is page minus 1
echo '<a href="members.php?page=$prev" title="Go To Page: $prev">« Previous</a>'; //give the previous link
} //end
die('Copy And Paster!'); //you gotta remove this..
for($i = 1;$i<=$total_pages;$i++){ //start making more pages :)
if($page == $i){ //if the current page is the same as the number than
echo '<b>$i</b> '; //dont give it as a link
}else{ //or...
echo '<a href="members.php?page=$i" title="Go To Page: $i">$i</a> '; //give as link :)
}
}
if($page < $total_pages){ //if the page is less than total
$next = ($page + 1); //add one to page
echo '<a href="members.php?page=$next" title="Go Ro Page: $next">Next »</a>'; //give next link
} //end
}
echo "<center>";
?>
Read through it carefully :)