Pagination Function

Discussion in 'PHP' started by FishSword, Oct 3, 2010.

  1. #1
    Hiya! ;)

    How do I create a pagination function that can be used multiple times, with any MySQL query?

    Many Thanks,
     
    Last edited: Oct 3, 2010
    FishSword, Oct 3, 2010 IP
  2. axelay

    axelay Peon

    Messages:
    54
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    axelay, Oct 3, 2010 IP
  3. FishSword

    FishSword Active Member

    Messages:
    131
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #3
    Excellent, thanks for that axelay. But, how do I turn that into a function that can be used with any MySQL query. Can you give me an example?
     
    FishSword, Oct 3, 2010 IP
  4. axelay

    axelay Peon

    Messages:
    54
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    It really is not that difficult if you know how to PHP even a little :)
    What I gave you should be enough for you to start customising it

    aXe
     
    axelay, Oct 4, 2010 IP
  5. imperialDirectory

    imperialDirectory Peon

    Messages:
    395
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #5
    imperialDirectory, Oct 4, 2010 IP
  6. FishSword

    FishSword Active Member

    Messages:
    131
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #6
    I have looked at the code you provided, but I can't work out how you would add the limit to the SQL query. If you could provide a function example, it would be much appreciated ;)
    Also, how do I adapt the code, so it can be used to display the results of any query?

    I have had a go! ;) Can you point me in the right direction?

    
    $getusers= "SELECT firstname, lastnameFROM users";
    paginate($getusers, 0);
    
    function paginate($query, $start)
    {
    	
    	$query .= " LIMIT 0, 5";
    	$query = mysql_query($query);
    	echo "sss".mysql_num_rows($query);
    	
    	while($row = mysql_fetch_array($query))
    	{
    		echo $row['firstname'] . " " . $row['lastnameFROM '];
    		echo "<br />";
    	}
    }
    
    
    Code (PHP):
     
    Last edited: Oct 4, 2010
    FishSword, Oct 4, 2010 IP
  7. FishSword

    FishSword Active Member

    Messages:
    131
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #7
    I don't know what else I need to do, in order to make the function work for any query :(
     
    FishSword, Oct 5, 2010 IP
  8. axelay

    axelay Peon

    Messages:
    54
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #8
    $getusers= "SELECT firstname, lastname FROM users";
    paginate($getusers, 0, 5);
    
    function paginate($query, $start, $count)
    {
        $query .= " LIMIT $start, $count";
        $query = mysql_query($query);
        echo "sss".mysql_num_rows($query);
        
        while($row = mysql_fetch_array($query))
        {
            echo $row['firstname'] . " " . $row['lastnameFROM '];
            echo "<br />";
        }
    }
    PHP:
    That is not working? :)

    aXe
     
    axelay, Oct 5, 2010 IP
  9. FishSword

    FishSword Active Member

    Messages:
    131
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #9
    Well, it is working, but what about the data (Firstname & Lastname)? That won't necessarily be the same for each query. What do I need to do in order to make it work with any query, and display the associated query results.
     
    FishSword, Oct 5, 2010 IP
  10. axelay

    axelay Peon

    Messages:
    54
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #10
    function paginate($query, $start, $count)
    {
        $query .= " LIMIT $start, $count";
        $query = mysql_query($query);
        return $query;
    }
    $getusers= "SELECT firstname, lastname FROM users";
    $data = paginate($getusers, 0, 5);
    
    echo "sss".mysql_num_rows($data);
        
    while($row = mysql_fetch_array($data))
    {
        echo $row['firstname'] . " " . $row['lastnameFROM '];
        echo "<br />";
    }
    
    PHP:
    Would work but is kinda pointless :)

    aXe
     
    axelay, Oct 5, 2010 IP
  11. FishSword

    FishSword Active Member

    Messages:
    131
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #11
    So is it not possible to make a pagination function that can be used multiple times, with any MySQL query?
     
    FishSword, Oct 6, 2010 IP
  12. ilyakar

    ilyakar Peon

    Messages:
    14
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #12
    whats a pagination function?
     
    ilyakar, Oct 7, 2010 IP
  13. FishSword

    FishSword Active Member

    Messages:
    131
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #13
    A "Pagination Function" is basically a function that can be used multiple times, in order to paginate results. The problem I have, is would it be possible to make such a function to be used for any query, and if so, how?
     
    FishSword, Oct 16, 2010 IP
  14. sinha.sidhi

    sinha.sidhi Peon

    Messages:
    50
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #14
    sinha.sidhi, Aug 29, 2011 IP