rofl90 Posted March 8, 2008 Share Posted March 8, 2008 I need to do some pagination for news on my site, how would I limit a while command if it gets more than however many I set per page? Quote Link to comment Share on other sites More sharing options...
deadonarrival Posted March 8, 2008 Share Posted March 8, 2008 <?php $query = mysql_query($sql); $result = mysql_fetch_array($query); $total = mysql_num_rows($query); $number = 0; while ($number <= $total) { //Print whatever it is print $result[$number]; ++$number } ?> Quote Link to comment Share on other sites More sharing options...
rofl90 Posted March 8, 2008 Author Share Posted March 8, 2008 <?php $sql = "SELECT * FROM NEWS ORDER BY id ASC"; $query = mysql_query($sql); $total = mysql_num_rows($query); $maxnews = '3'; if($total > $maxnews) { //what do i put here?????? } else{ while($result = mysql_fetch_array($query)) { // echo news articles if total is no greater than 3 } } ?> I have this so far, anyone care to fill in the "What do I put here"? Quote Link to comment Share on other sites More sharing options...
corbin Posted March 9, 2008 Share Posted March 9, 2008 You should never pull more data from mysql than you need.... It's just silly.... What if you had 10k rows and pulled all of them just for like 3 rows? You should limit it with sql LIMIT. Example: $page = (isset($_GET['page']) && is_numeric($_GET['page']) && $_GET['page'] > = 1) ? $_GET['page'] : 1; $per_page = 5; $count_q = mysql_query("SELECT COUNT(id) FROM news"); $count_r = mysql_fetch_row($count_q); $count = $count_r[0]; if($page > ceil($count/per_page)) $page = 1; $limit_min = ($page-1)*$per_page; $query = mysql_query("SELECT * FROM news ORDER BY id ASC LIMIT {$limit_min}, {$per_page}"); while($r = mysql_fetch_assoc($query)) { //echo out the contents } The count isn't exactly the best thing in the world, but you need it so you can know whether or not to have a next button. (I guess you could do another limit and see if there were a result instead of a count.... hmmmm.... I don't paginate much lol) Quote Link to comment Share on other sites More sharing options...
rofl90 Posted March 9, 2008 Author Share Posted March 9, 2008 oh wait lemme try Quote Link to comment Share on other sites More sharing options...
rofl90 Posted March 9, 2008 Author Share Posted March 9, 2008 works Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.