PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
Ghoz
Forum Commoner
Posts: 38 Joined: Thu Jul 12, 2012 2:32 am
Post
by Ghoz » Wed Sep 26, 2012 1:15 am
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>
Christopher
Site Administrator
Posts: 13596 Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US
Post
by Christopher » Wed Sep 26, 2012 9:23 am
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.
(#10850)
Ghoz
Forum Commoner
Posts: 38 Joined: Thu Jul 12, 2012 2:32 am
Post
by Ghoz » Wed Sep 26, 2012 9:55 am
Hi, I do not know how to insert it into the script to make it working. Pls help. TQ
Christopher
Site Administrator
Posts: 13596 Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US
Post
by Christopher » Wed Sep 26, 2012 2:31 pm
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);
(#10850)
Mordred
DevNet Resident
Posts: 1579 Joined: Sun Sep 03, 2006 5:19 am
Location: Sofia, Bulgaria
Post
by Mordred » Thu Sep 27, 2012 3:29 am
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.
Christopher
Site Administrator
Posts: 13596 Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US
Post
by Christopher » Thu Sep 27, 2012 1:15 pm
Mordred wrote: I'd also take time to remove the dodgy stuff with %extend% --
Agreed. All the SQL building code above is horrible.
(#10850)
Ghoz
Forum Commoner
Posts: 38 Joined: Thu Jul 12, 2012 2:32 am
Post
by Ghoz » Thu Sep 27, 2012 7:58 pm
Whatever it is, i'm still blur.... any clear guide?
Thanks
Ghoz
Forum Commoner
Posts: 38 Joined: Thu Jul 12, 2012 2:32 am
Post
by Ghoz » Thu Sep 27, 2012 9:27 pm
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.
Benjamin
Site Administrator
Posts: 6935 Joined: Sun May 19, 2002 10:24 pm
Post
by Benjamin » Thu Sep 27, 2012 9:31 pm
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.
Christopher
Site Administrator
Posts: 13596 Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US
Post
by Christopher » Thu Sep 27, 2012 9:32 pm
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.
(#10850)