Jump to content

pagination


Lone_Ranger
Go to solution Solved by 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
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);
Edited by Barrikor
Link to comment
Share on other sites

  • Solution

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.