Showing a link when needed

Discussion in 'PHP' started by crazyryan, Oct 17, 2006.

  1. #1
    I have a code for news articles, probably isn't great but it's good enough for me lol.

    Anyway, the easiest way to explain would be:

    I have 4 news articles and I limit each page to 3 articles. So, the first page shows 3 articles and because there is one more article in the database it shows next at the bottom of the page. Then when you click next it shows the other article that exists but just that article .. no next button because there are no more articles to see.

    
    <?
    include("news/dbconnect.php");//include the file to connect to the database
    
     if(is_numeric($_GET['page'])){
       $page = $_GET['page'];
       
    }else{
    $page = 1;
    }
    
    $limit = 3;  // How many to show per page
    $next = $page + 1;
    $next = "http://www.habboname.com/index/page/" . $next . "/";
    $x = ($page * $limit) - $limit;
    
    $getnews = mysql_query("SELECT * FROM `mynews` ORDER BY `id` DESC LIMIT $x, $limit ");
    //query the database for all of the news
    
    while($r=mysql_fetch_array($getnews)){//while there are rows in the table
    extract($r);//remove the $r so its just $variable
    
    if (strlen($title) < 1){
        //title is empty
        die(hello);
    }
    echo("
    <b>$title</b><br />
    Posted by $user on the $date!<br /><br />
    $message
    <br /><hr /><br />
    ");
    
    }
    
    ?>
    PHP:
    Can anyone help me accomplish it?
     
    crazyryan, Oct 17, 2006 IP
  2. streety

    streety Peon

    Messages:
    321
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #2
    You can use the 'mysql_num_rows' function to find out how many rows there are in your result set. If it is less than the limit then you don't display the link, otherwise you do.
     
    streety, Oct 17, 2006 IP
  3. SoKickIt

    SoKickIt Active Member

    Messages:
    305
    Likes Received:
    30
    Best Answers:
    0
    Trophy Points:
    70
    #3
    You could set $limit to 4. If you get 4 rows, display $next.
     
    SoKickIt, Oct 17, 2006 IP
    crazyryan likes this.
  4. streety

    streety Peon

    Messages:
    321
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Good point, just counting the rows returned could leave you with a page and nothing on it.

    You would also need to modify your while statement to include a count so that after three news stories it stopped printing.
     
    streety, Oct 17, 2006 IP
  5. crazyryan

    crazyryan Well-Known Member

    Messages:
    3,087
    Likes Received:
    165
    Best Answers:
    0
    Trophy Points:
    175
    #5
    I kinda suck at PHP, would anyone be able to do it for me? +rep to all who have helped.
     
    crazyryan, Oct 24, 2006 IP
  6. crazyryan

    crazyryan Well-Known Member

    Messages:
    3,087
    Likes Received:
    165
    Best Answers:
    0
    Trophy Points:
    175
    #6
    Didn't seem to fix it, thanks though.

    It just limited the news display to 1 and didn't show any links.
     
    crazyryan, Oct 24, 2006 IP
  7. crazyryan

    crazyryan Well-Known Member

    Messages:
    3,087
    Likes Received:
    165
    Best Answers:
    0
    Trophy Points:
    175
    #7
    <?
    include("/home/spiral/public_html/habbo/news/dbconnect.php");//include the file to connect to the database
    if(is_numeric($_GET['page'])){
    $page = $_GET['page'];
    }else{
    $page = 1;
    }
    
    $limit = 3; // How many to show per page
    $next = $page + 1;
    $x = ($page * $limit) - $limit;
    $query = ("SELECT * FROM mynews");
    //first find out the total record you are going to display
    $result= mysql_query($query)or die (mysql_error());
    $total_row = mysql_num_rows($result);
    
    $max_page = ceil($total_row/$limit);
    if($page != $max_page){
    $next = "http://www.habboname.com/index/page/" . $next . "/";
    }else{
    $next = "&nbsp;";
    }
    
    $getnews = mysql_query("SELECT * FROM mynews ORDER BY id DESC LIMIT $x, $limit");
    
    while($r=mysql_fetch_array($getnews)){//while there are rows in the able
    
    extract($r);//remove the $r so its just $variable
    
    if (strlen($title) < 1){ //title is empty
    die(hello);
    
    }
    
    }
    echo("
    <b>$title</b><br />
    Posted by $user on $date!<br /><br />
    $message
    <br /><hr /><br />
    ");
    
    
    ?>
    PHP:
    That's my full code, and this is what happens:
    www.habboname.com/labs/

    I need it so it displays how many I limit it too, at the moment it only displays 1 lol and also I need it to show the links.

    Thanks :)
     
    crazyryan, Oct 24, 2006 IP