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...... Link to comment https://forums.phpfreaks.com/topic/281925-pagination/ 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 Link to comment https://forums.phpfreaks.com/topic/281925-pagination/#findComment-1448456 Share on other sites More sharing options...
Barrikor Posted September 6, 2013 Share Posted September 6, 2013 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); Link to comment https://forums.phpfreaks.com/topic/281925-pagination/#findComment-1448472 Share on other sites More sharing options...
Lone_Ranger Posted September 7, 2013 Author 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 ..."; } Link to comment https://forums.phpfreaks.com/topic/281925-pagination/#findComment-1448582 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.