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?
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.
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.
Didn't seem to fix it, thanks though. It just limited the news display to 1 and didn't show any links.
<? 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 = " "; } $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