WEB Displaying content with PHP and MySQL

Discussion in 'OT Technology' started by Flaxer, Dec 18, 2007.

  1. Flaxer

    Flaxer New Member

    Joined:
    Dec 7, 2006
    Messages:
    43
    Likes Received:
    0
    So I have made a form to add content and I have a page to display the content I have made... I was wondering if there was a simple way to only display like 10 at a time and have pages for the next 10... I have seen it done, but don't know how much work is needed to make this work...

    http://lawldesigns.com/design03/myspace_contact_tables.php
    ^- Link to the displayed content...

    As you can see it will continue to take up a lot of page space once I add more content... So the having multiple pages would be nice... Let me know all you PHP wizards :bowdown:
     
  2. projectalpha

    projectalpha 二號 Stunna

    Joined:
    Aug 4, 2001
    Messages:
    17,671
    Likes Received:
    0
    Location:
    Bay Area, California
    make use of START and LIMIT in your sql statement.
     
  3. noon

    noon get high and teach me how to listen

    Joined:
    May 4, 2002
    Messages:
    3,384
    Likes Received:
    0
    Location:
    Lawrence, KS
    What you do is add a flag to the URL for the page

    so, say that you want to show 10 results per page
    Code:
    $page = ((int)$_GET['page'] != 0) ? (int)$_GET['page'] : 1;
    $start_row = ($page - 1)*10;
    $end_row = $start_row + 10;
    
    Now you need to know how many rows are in the table
    Code:
    SELECT COUNT(1) FROM table WHERE ...
    
    Store that result to a variable, say $num_rows

    Now add this to the end of your original select statement
    Code:
    . "LIMIT $start_row, $end_row"
    So, now you have the page number,
    total number of results, and the 10 results that were pulled.

    Now you need the number of pages

    Code:
     $num_pages = ($num_rows / 10);
    if ($num_rows % 10)
     $num_pages++; // add extra page
    
    Put something like this where you want the page numbers to be..

    Code:
    for(int $i=1; $i<=$num_pages;$i++) {
     echo ($i==$page) ? "<b>".$page."</b> " : "<a href='site.php?page=".$i."'>".$i."</a> ";
    
    }
    
    
    This will link the pages as such (for page 2):
    1 2 3 4 5 6 7 8 9
     
    Last edited: Dec 18, 2007
  4. Flaxer

    Flaxer New Member

    Joined:
    Dec 7, 2006
    Messages:
    43
    Likes Received:
    0
    So this is my original code
    Code:
    <?
    include("dbinfo.inc.php");
    mysql_connect($dbhost,$username,$password);
    @mysql_select_db($database) or die( "Unable to select database");
    $query="SELECT * FROM contact_table ORDER BY id DESC";
    $result=mysql_query($query);
    
    $num=mysql_numrows($result); 
    
    mysql_close();
    ?>
    Now I am gonna add your code...

    Code:
    <?
    include("dbinfo.inc.php");
    mysql_connect($dbhost,$username,$password);
    @mysql_select_db($database) or die( "Unable to select database");
    $query="SELECT * FROM contact_table ORDER BY id DESC LIMIT $start_row, $end_row" ;
    $result=mysql_query($query);
    
    $num=mysql_numrows($result); 
    
    $page = ((int)$_GET['page'] != 0) ? (int)$_GET['page'] : 1;
    $start_row = ($page - 1)*10;
    $end_row = $start_row + 10;
    
     $num_pages = ($num_rows / 10);
    if ($num_rows % 10)
     $num_pages++; // add extra page
    
    mysql_close();
    ?>
    is this right? and where would I add
    Code:
    SELECT COUNT(1) FROM table WHERE ...
     
  5. Flaxer

    Flaxer New Member

    Joined:
    Dec 7, 2006
    Messages:
    43
    Likes Received:
    0
    also I was wondering how I might go about just displaying one? Like as I add new content the newest one would be displayed on the home page?
     
  6. Cyborg

    Cyborg New Member

    Joined:
    Feb 25, 2007
    Messages:
    186
    Likes Received:
    0
    I am no expert in the php/mySQL field, but ive been messing with my own (cyborg.3host.biz - example on home page):

    Basically, just dont put the "content" code in a loop:
    Code:
         //THIS SHOWS THE MOST RECENT RANT
         $result = mysql_query("SELECT `id`, 'subcat', `rant`, `title`, `username` FROM `maaking_rant` ORDER BY 'id' DESC") or die(mysql_error());   
         // Show Most Recent Rant 
         $row = mysql_fetch_array($result);
         echo "".$row['title']." <br><br> ".$row['rant']." <br> ".$row['username']."<br />";
    
     
  7. noon

    noon get high and teach me how to listen

    Joined:
    May 4, 2002
    Messages:
    3,384
    Likes Received:
    0
    Location:
    Lawrence, KS
    just do ORDER BY post_date DESC LIMIT 0,10
     
  8. Flaxer

    Flaxer New Member

    Joined:
    Dec 7, 2006
    Messages:
    43
    Likes Received:
    0
    I got the only displaying one... just deleted the grabbing of all of them.

    Now I just need to do the pages thingy
     

Share This Page