Lone_Ranger Posted September 6, 2013 Share Posted September 6, 2013 How would I create a pagination from the following code I got for looking up a database. $select_gamereview = mysql_query("select * from news where category='gamereview' order by topic ASC"); while($news = mysql_fetch_array($select_gamereview)) { $position=780; $message="$news[article]"; $post = substr($message, 0, $position); echo "$news[topic]"; echo "$post ..."; how can I update this in order for it to show the first 15 results on page one and page 2 has the next 15 results and so on...... Quote Link to comment Share on other sites More sharing options...
fastsol Posted September 6, 2013 Share Posted September 6, 2013 Check out this tutorial series http://www.youtube.com/playlist?list=PL1A88F9FF46F82198 Quote Link to comment Share on other sites More sharing options...
Barrikor Posted September 6, 2013 Share Posted September 6, 2013 (edited) Just change: "select * from news where category='gamereview' order by topic ASC" To: "select * from news where category='gamereview' order by topic ASC LIMIT $limit OFFSET $offest" Where $limit and $offset are set by: $current_page_number = 1; // for example $limit = 15; $offset = $limit * ($current_page_number - 1); Edited September 6, 2013 by Barrikor Quote Link to comment Share on other sites More sharing options...
Solution Lone_Ranger Posted September 7, 2013 Author Solution Share Posted September 7, 2013 figured it this is how I have done it in case anyone browsing wants to use the code $page = isset($_GET['page']) ? (int) $_GET['page'] : 1; $pages = implode(mysql_fetch_assoc(mysql_query("SELECT COUNT(id) FROM news"))); $pages = ceil($pages / 10); $querystring = ""; foreach ($_GET as $key => $value) { if ($key != "page") $querystring .= "$key=$value&"; } echo "Pages: "; for ($i = 1; $i <= $pages; $i++) { echo "<a " . ($i == $page ? "class=\"selected\" " : ""); echo "href=\"?{$querystring}page=$i"; echo "\">$i</a> "; } $result = mysql_query("SELECT * FROM news where category='gamereview' order by topic ASC LIMIT " . (($page - 1) * 10) . ", 10"); while($news = mysql_fetch_array($result)) { $position=780; $message="$news[article]"; $post = substr($message, 0, $position); echo "$news[topic]"; echo "$post ..."; } 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.