MySQL results displaying on 3 separate pages or use switch statement?

Discussion in 'PHP' started by yoyo99, Jan 25, 2009.

  1. #1
    Hello,

    I was wondering what would be the best way to display my query results for 3 separate pages. Should I use a switch statement and have the different results all on 1 page? Or use 3 different pages?

    Basically I want to the page to return a limited amount of info at first. Then if the user wants to see more info from the results, they can hit a button and the same info would be displayed, but with more info from the database. Then have an option to show all the info from the database.

    Please explain details, i am new to php.

    Thanks
     
    yoyo99, Jan 25, 2009 IP
  2. crivion

    crivion Notable Member

    Messages:
    1,669
    Likes Received:
    45
    Best Answers:
    0
    Trophy Points:
    210
    Digital Goods:
    3
    #2
    Search for pagination, its exactly what you need
     
    crivion, Jan 25, 2009 IP
  3. Dennis M.

    Dennis M. Active Member

    Messages:
    119
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    58
    #3
    I have just recently (last night) written up a small simple pagination script hopefully to describe how the script works. (Refer to post (in this section): "how to split mysql in pages in php").

    A lot has to do with your MySQL query. Basically, you want (on the search page or whatever) to use a query similar to this:
    SELECT * from db_table ORDER BY id DESC LIMIT 0, 2
    Code (markup):
    That query right there will start from the very first item in the database and show 2 results from there. To do this on a paginated level try something similar to this:

    <?php
    // Provided that you're already connected to MySQL
    $num = $_GET['num'];
    if(!isset($num) OR !is_numeric($num)){
         $num = 1;
    }
    $perPage = 2; // 2 Results per page
    $limit = $perPage+($num-1); // Properly offset pages
    $linkno = 1; // Proper link offset
    $query = mysql_query("SELECT * from db_table ORDER BY id DESC ".mysql_escape_string($limit)." , ".mysql_escape_string($perPage).";");
    $pageNum = round(mysql_num_rows($query)); // Static Number for link settings
    while($row == mysql_fetch_array($query)){
         while($link <= $perPage){
              print stripslashes($row['title'])."<br />".stripslashes($row['content']."<br />";
              print "<a href=\"?num=".$linkno."\">$linkno</a> ";
              $link++;
              $linkno++;
         }
    }
    ?>
    PHP:
    Alright so just wrote this example just now haven't really had a chance to test but should work fine. The only thing I don't have time to include (because I'm leaving) is the "summary" part. But it's the same principle only make the title a link (or whatever) going to id. Define something like $page = $_GET['page']; and create separate pages for certain articles and different page for the results. E.g. page=article&id=ART-ID that way the DB can pull the information from the database using a query which searches for the id. (Depends on database structure of course)

    Regards,
    Dennis M.
     
    Dennis M., Jan 25, 2009 IP
  4. yoyo99

    yoyo99 Peon

    Messages:
    17
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Thank you both for the quick replies! I am sorry but I guess I wasn't too clear in my original post. I don't need to make pages to continue the query results. I will try to explain it more thorough.

    For example, one record in my database would contain the following info:

    Fields : name, number, address, bday, favColor, pet
    And the record would have : Joe, 555-1234, 584 1st Ave, June 10, Green, Spot

    then in first result page would be something like:
    
    <table>
    <tr>
    <td>Name</td>
    <td>Number</td>
    <td>Address</td>
    </tr>
    <tr>
    <td><? echo $name; ?></td>
    <td><? echo $number; ?></td>
    <td><? echo $address; ?></td>
    </tr>
    </table>
    
    Code (markup):
    then i want to have a button, i guess, to switch to display more info. So it would look something like:
    
    <table>
    <tr>
    <td>Name</td>
    <td>Number</td>
    <td>Address</td>
    <td>Birthday</td>
    </tr>
    <tr>
    <td><? echo $name; ?></td>
    <td><? echo $number; ?></td>
    <td><? echo $address; ?></td>
    <td><? echo $bday; ?></td>
    </tr>
    </table>
    
    Code (markup):
    And another button to switch to a view with even more info like:
    
    <table>
    <tr>
    <td>Name</td>
    <td>Number</td>
    <td>Address</td>
    <td>Birthday</td>
    <td>Favorite Color</td>
    <td>Pet Name</td>
    </tr>
    <tr>
    <td><? echo $name; ?></td>
    <td><? echo $number; ?></td>
    <td><? echo $address; ?></td>
    <td><? echo $bday; ?></td>
    <td><? echo $favColor; ?></td>
    <td><? echo $pet; ?></td>
    </tr>
    </table>
    
    Code (markup):
    So I hope that clear up my other post and show you more or less what I need. Any ideas?

    Thanks
     
    yoyo99, Jan 26, 2009 IP