Page 1 of 1

Pagination

Posted: Wed Sep 26, 2012 1:15 am
by Ghoz
Hi, I'm a newbie. I have searched online & read few tutorials on pagination. Unfortunately I do not know how to insert the pagination code into my script. Please help. Thanks!

Code: Select all

<?php 
// This will allow to use the css inside includes if editing this file directly... probably
$visitingfiledirectly = false;
if($visitingfiledirectly)
{
	?>
    <link rel="stylesheet" type="text/css" media="screen" href="../stylus.css">
    <?php
}
?>
<?php
//To check default
$gender = "Female";
$age1 = 18;
$age2 = 35;
$country = "All";
$status = "";
$orderby = "BirthYear";

if(isset($_POST["gender"]) && ($_POST["gender"] == "Male" || $_POST["gender"] == "Female" || $_POST["gender"] == ""))
{
	$gender = $_POST["gender"];
}
if(isset($_POST["age1"]) && is_numeric($_POST["age1"]))
{
	$age1 = $_POST["age1"];
}
if(isset($_POST["age2"]) && is_numeric($_POST["age2"]))
{
	$age2= $_POST["age2"];
}
if(isset($_POST["country"]) && $_POST["country"] != "All")
{
	$country = PHP_slashes($_POST["country"]);
}
if(isset($_POST["status"]) && ($_POST["status"] == "Single" || $_POST["status"] == "Married"))
{
	$status = $_POST["status"];
}
if(isset($_POST["orderby"]) && ($_POST["orderby"] == "BirthYear" || $_POST["orderby"] == "Name"))
{
	$orderby = $_POST["orderby"];
}

$query = "";

$checkuser = "SELECT BirthYear, Id, Avatar, Name, Email FROM users WHERE %extend%"; // FIND THE DESIRED USER BY ID
//Name - to display the name
$extend = "";
$and = "";
if($gender != "")
{
	$extend = $extend.$and."Gender='".$gender."'";
	$and = " AND ";
}
	$extend = $extend.$and."BirthYear<='".(date("Y")-$age1)."'";
	$and = " AND ";
	$extend = $extend.$and."BirthYear>='".(date("Y")-$age2)."'";

if($country != "All")
{
	$extend = $extend.$and."Country='".$country."'";
	$and = " AND ";
}
if($status != "")
{
	$extend = $extend.$and."Status='".$status."'";
	$and = " AND ";
}
if($orderby == "BirthYear")
{
	$extend = $extend." ORDER BY BirthYear DESC";
}
if($orderby == "Name")
{
	$extend = $extend." ORDER BY Name ASC";
}
$checkuser = str_replace("%extend%", $extend, $checkuser);
$checkuser = mysql_query($checkuser);
//$user = mysql_fetch_array($checkuser); // COLLECT HIS DATA INTO AN ARRAY

$admin = getMyAdminLevel();
?>
<form method="POST" id="smform">
<b>Browse for Users</b>
<font color="#FF0000"><br>You must signed up & log in to view the full profile.</font><br /><br>
Gender: <input type="radio" name="gender" value="Male" <?php if($gender == 'Male')echo'checked';?> /> Male
<input type="radio" name="gender" value="Female" <?php if($gender == 'Female')echo'checked';?> /> Female
<input type="radio" name="gender" value="" <?php if(strlen($gender) == 0)echo'checked';?> /> Both<br>
Age:<input type="text" size="1" name="age1" <?php echo'value="'.$age1.'"'?>/>~<input type="text" size="1" name="age2" <?php echo'value="'.$age2.'"'?>/>,
 Location:<?php printCountries($country,TRUE);?>
<br />
Status:<select name="status">
<?php if($status != "")
{
echo'<option value="'.$status.'">'.$status.'</option>';
}?>
<option value="Single">Single</option>
<option value="Married">Married</option>
<option value="">Any</option>
</select><br />
Order by:<input type="radio" name="orderby" value="BirthYear" <?php if($orderby == 'BirthYear')echo'checked';?> /> Age
<input type="radio" name="orderby" value="Name" <?php if($orderby == 'Name')echo'checked';?>/> Name<br>
 <input type="submit" value="Browse" id="sm">
</form> 
<div class="browselist">
	<table border="0">
	<tr>
    <?php
	$counter = 0;
	while($user = mysql_fetch_array($checkuser))
	{
		?>
<td>
		<p align="center">
		<a target="_blank" href="index.php?section=profile&uid=<?php echo $user["Id"];?>"><?php echo $user["Name"];?><br>
<!--["Name"] - to display the name-->
		<img src="<?php echo $user["Avatar"];?>" width="100" height="100"/></a></td></p>
        <?php
		$counter++;
		if($counter >= 5)
		{
			?></tr><tr><?php
			$counter = 0;
		}
	}
	?>
</table>
</div>

Re: Pagination

Posted: Wed Sep 26, 2012 9:23 am
by Christopher
You need to add LIMIT to your SQL, like: " LIMIT $offset, $items_per_page". Pass a parameter to your script that specifies the page number, then calculate the offset from that.

Re: Pagination

Posted: Wed Sep 26, 2012 9:55 am
by Ghoz
Hi, I do not know how to insert it into the script to make it working. Pls help. TQ

Re: Pagination

Posted: Wed Sep 26, 2012 2:31 pm
by Christopher
Well, start with something like this:

Code: Select all

$page_size = 10;
$page = isset($_POST['page']) ? intval($_POST['page']) : 1;     // get value from request
$offset = $page > 1 ? ($page - 1) * $page_size : 0;     // check for negative numbers and make zero based value for LIMIT
$extend .= " LIMIT $offset, $page_size";

$checkuser = str_replace("%extend%", $extend, $checkuser);
$checkuser = mysql_query($checkuser);

Re: Pagination

Posted: Thu Sep 27, 2012 3:29 am
by Mordred
I'd also take time to remove the dodgy stuff with %extend% -- you want to append something to a query - append it. Everything is fine now, but when you get back to this code after two months, will you be able to remember what is the correct way to deal with the query? What if you add a condition based on user input right in the query, and the user enters %extend% as a value? Aim to write clean and robust code: no hidden assumptions and not easily breakable when circumstances change.

Re: Pagination

Posted: Thu Sep 27, 2012 1:15 pm
by Christopher
Mordred wrote:I'd also take time to remove the dodgy stuff with %extend% --
Agreed. All the SQL building code above is horrible.

Re: Pagination

Posted: Thu Sep 27, 2012 7:58 pm
by Ghoz
Whatever it is, i'm still blur.... any clear guide?
Thanks

Re: Pagination

Posted: Thu Sep 27, 2012 9:27 pm
by Ghoz
Finally I got it!! Sometimes I feel that this forum doesn't help me much...
How I did it? I studied how the 'LIMIT' works and finally I got it. Anyway thanks for your post.

Re: Pagination

Posted: Thu Sep 27, 2012 9:31 pm
by Benjamin
Ghoz wrote:Finally I got it!! Sometimes I feel that this forum doesn't help me much...
How I did it? I studied how the 'LIMIT' works and finally I got it. Anyway thanks for your post.
Well, to be fair, it does appear that Christopher gave you the code you needed, or at least very close to what you needed.

Re: Pagination

Posted: Thu Sep 27, 2012 9:32 pm
by Christopher
Ghoz wrote:Finally I got it!! Sometimes I feel that this forum doesn't help me much...
How I did it? I studied how the 'LIMIT' works and finally I got it. Anyway thanks for your post.
We tend not to do your work for you, but give you ideas and code examples. Discovering the solution yourself gives you a better understanding of how it works.