...

View Full Version : pagination



jokerboy123
05-19-2011, 04:57 AM
Hey ladies n germans, just wondering if i can get some help, im getting a mysql syntax error but cant understand why, what im trying to do with this code is paginate, iv gotten pagination working befor but this is a different code and im just unsure how to work it out, this code is used to display the 5 most recent users or an entire list or a search, depending on what the user has clicked.



function get_users($name="",$gender="",$age_range="",$offset,$entries_per_page) {
$name = clean($name,80);

$connection = mysql_open();

$query =
"select SQL_CALC_FOUND_ROWS id, username, realname, gender, birthdate, joindate, email, " .
"(year(curdate()) - year(birthdate)) " .
" - (right(curdate(),5) < right(birthdate,5)) as age " .
"from yusers where 1 ";
if ($name) {
$query .= "and (username like '%$name%' or realname like '%$name%') ";
}
if ($gender) {
$query .= "and gender = '$gender' ";
}
$query .= "order by id";
$query .= "LIMIT $offset, $entries_per_page";
//print "$query<br>\n";

// Find total number of rows, ignoring pagination, using SQL_CALC_FOUND_ROWS
$r = mysql_query("SELECT FOUND_ROWS()", $connection)
or show_error();
$r = mysql_fetch_array($r);
$num_entries = $r[0];

$result = mysql_query($query,$connection) or show_error();

if ($age_range) {
list($lower,$upper) = preg_split("/-/", $age_range);
}
$users = array();
while ($user = mysql_fetch_array($result)) {
if ($age_range == "" ||
($lower <= $user['age'] && $user['age'] <= $upper)) {
$users[] = $user;
}
}

mysql_close($connection) or show_error();
return array($users,$num_entries);
}

bullant
05-19-2011, 07:27 AM
I posted a working pagination demo (including sql to create the test table) in this thread (http://www.codingforums.com/showthread.php?p=1083513&highlight=paginator#post1083513).

paddyfields
05-19-2011, 10:03 AM
big thumbs up to bullant.. much better than any 'googled' tutorials i've seen.

bullant
05-19-2011, 11:48 PM
you're welcome :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum