Jump to content

pagination


Lone_Ranger

Recommended Posts

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

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.