almystersv Posted January 28, 2008 Share Posted January 28, 2008 Hi Guys, I am displaying a large number of records onto one of my webpages. At the moment I am limiting the number i display to the page to 10 and have created a number of pages displaying a group of 10 at a time. Is there anyway of doing it so that a page is automatically created when more records are added or something along those lines?! Thanks Quote Link to comment https://forums.phpfreaks.com/topic/88206-displaying-records-across-a-number-of-pages/ Share on other sites More sharing options...
GingerRobot Posted January 28, 2008 Share Posted January 28, 2008 You're creating these pages manually?! Ouch. I suggest you look into pagination. There's a tutorial here on the subject. Quote Link to comment https://forums.phpfreaks.com/topic/88206-displaying-records-across-a-number-of-pages/#findComment-451350 Share on other sites More sharing options...
phpSensei Posted January 28, 2008 Share Posted January 28, 2008 You can just do "SELECT * FROM table ORDER BY field DESC LIMIT 10", and fetch that onto a query. The data is looped whenever you refresh the page with a while statement. Also this would be called a pagination. Quote Link to comment https://forums.phpfreaks.com/topic/88206-displaying-records-across-a-number-of-pages/#findComment-451352 Share on other sites More sharing options...
almystersv Posted January 28, 2008 Author Share Posted January 28, 2008 Hi, I went through that tutorial and it explained a lot of things well but I am having problems with the code. this is what i have... <?php session_start(); if (isset($_SESSION['username']) == false){ header("Location: login.php"); exit(); } require "connect.php"; $limit = 10; $query_count = "SELECT count(*) FROM product"; $result_count = mysql_query($query_count); $totalrows = mysql_num_rows($result_count); if(empty($page)){ $page = 1; } $limitvalue = $page * $limit - ($limit); $query = "SELECT * FROM product LIMIT $limitvalue, $limit"; $result = mysql_query($query) or die("Error: " . mysql_error()); if(mysql_num_rows($result) == 0){ echo("Nothing to Display!"); } $bgcolor = "#E0E0E0"; // light gray ?> <table width ="100%" border="1"> <?php while($row = mysql_fetch_array($result)){ if ($bgcolor == "#E0E0E0"){ $bgcolor = "#FFFFFF"; }else{ $bgcolor = "#E0E0E0"; } echo("<tr bgcolor=".$bgcolor.">n<td>"); echo($row["URN"]); echo("</td>n<td>"); echo($row["productName"]); echo("</td>n</tr>"); } echo("</table>"); if($page != 1){ $pageprev = $page--; ?> <a href=\"StationaryOrdersTEST.php&page=$pageprev\">PREV".$limit."</a> <?php }else{ echo("PREV".$limit." "); } $numofpages = $totalrows / $limit; for($i = 1; $i <= $numofpages; $i++){ if($i == $page){ echo($i." "); }else{ ?><a href=\"StationaryOrdersTEST.php?page=$i\">$i</a> <?php } } if(($totalrows % $limit) != 0){ if($i == $page) { echo($i." "); } else{ ?> <a href="StationaryOrdersTEST.php?page=$i">$i</a> <?php } } if(($totalrows - ($limit * $page)) > 0){ $pagenext = $page++; ?> <a href="StationaryOrdersTEST.php?page=$pagenext\">NEXT".$limit."</a> <?php }else{ echo("NEXT".$limit); } mysql_free_result($result); ?> But it is displaying 20 odd rows of the letter 'n' before outputting the data from the database and also the links are not working for the 'PREV', 'NEXT' or the page numbers. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/88206-displaying-records-across-a-number-of-pages/#findComment-451381 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.