www.webdeveloper.com
Results 1 to 4 of 4

Thread: PHP split results over pages

  1. #1
    Join Date
    Oct 2006
    Posts
    364

    PHP split results over pages

    I have the following php code;

    PHP Code:
    <?php

        mysql_connect
    ("*""*""*") or die("Could not connect to MySQL");
        
    mysql_select_db("*") or die("Could not connect to MySQL");

        
    $query mysql_query("SELECT uid, username FROM fusers_users ORDER BY regdate DESC LIMIT 0,30");
        
    $user mysql_fetch_array($query);
        
    $members "<b><a href=\member.php?action=profile&uid={$user['uid']}\">{$user['username']}</a></b>,";
        echo 
    $members;
    ?>
    Can someone help me make it so that it selects 30, and then at the bottom of the page there is a link saying more >> then it gets the next 31-60 and so on.

    Also right now it is ordered by regdate, how can i use a hyperlink so when it is clicked it orders by username, and change from ASC or DESC by changing a dropdown box??

    Thanks.
    Last edited by Tabo; 04-08-2007 at 05:14 AM.

  2. #2
    Join Date
    Jun 2005
    Posts
    33
    Here is the answer to the first question.

    Code:
    <?php
    
        mysql_connect("*", "*", "*") or die("Could not connect to MySQL");
        mysql_select_db("*") or die("Could not connect to MySQL");
    
        $query = mysql_query("SELECT uid, username FROM fusers_users ORDER BY regdate DESC LIMIT 0,30");
        $user = mysql_fetch_array($query);
        $members = "<b><a href=\member.php?action=profile&uid={$user['uid']}\">{$user['username']}</a></b>,";
        echo $members;
    ?>
    goes to

    Code:
    <?php
    
        mysql_connect("*", "*", "*") or die("Could not connect to MySQL");
        mysql_select_db("*") or die("Could not connect to MySQL");
    	$startnumber = $_GET['page'] * 30;
    	
        $query = mysql_query("SELECT uid, username FROM fusers_users ORDER BY regdate DESC LIMIT $startnumber ,30");
        $user = mysql_fetch_array($query);
        $members = "<b><a href=\member.php?action=profile&uid={$user['uid']}\">{$user['username']}</a></b>,";
        echo $members;
    ?>
    and make the next link equal to resultspage?page=$_GET['page'] + 1;

  3. #3
    Join Date
    Nov 2004
    Location
    Canada
    Posts
    379
    Here's a wonderful tutorial on pagination:

    http://www.phpfreaks.com/print.php?c...rial&tut_id=43
    The beauty of code is in its simplicity.

  4. #4
    Join Date
    Oct 2006
    Posts
    7
    $maxrec = $_GET['maxrec'] * 1;
    $startrec = $_GET['startrec'] * 1;


    $l1 = $startrec;
    $l2 = $l1 + $maxrec;
    //# - if both startrec and maxrec is 0 then set l2 as 100
    if($l2 == 0)
    $l2 = 100;

    //# - set limit to display
    if($l2)
    $limit = " LIMIT $l1,$l2";
    else
    $limit = " LIMIT 0,100";//# - default


    if($l1 >= $totrows)
    {
    if($maxrec < $totrows)
    $l1 = $totrows - $maxrec;
    else
    $l1 = 0;
    }
    //# - assign start and end for a loop
    $vStart = $l1?$l1:0;
    $vEnd = $l2?($l2<$totrows?$l2:$totrows):$totrows;


    //$vEnd = $l2?($l2):$totrows;
    //# - fill the XML tags
    for($vIndex=$vStart;$vIndex<$vEnd;$vIndex++)
    {
    ----------------------

    }

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles