Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4

Thread: pagination

  1. #1
    New Coder
    Join Date
    May 2011
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    pagination

    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.

    Code:
    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);
    }

  • #2
    Banned
    Join Date
    Feb 2011
    Posts
    2,699
    Thanks
    13
    Thanked 395 Times in 395 Posts
    I posted a working pagination demo (including sql to create the test table) in this thread.

  • #3
    Regular Coder
    Join Date
    Dec 2010
    Location
    London
    Posts
    333
    Thanks
    63
    Thanked 11 Times in 11 Posts
    big thumbs up to bullant.. much better than any 'googled' tutorials i've seen.

  • #4
    Banned
    Join Date
    Feb 2011
    Posts
    2,699
    Thanks
    13
    Thanked 395 Times in 395 Posts
    you're welcome


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •