Jump to content

Pagination help?


LukeBateson

Recommended Posts

I can't get my head around it, no matter how many tutorials I read..

So would anyone be able to put this code into a pagination of 3 per page?

 

Many Thanks

 

<?php
if(!$_GET['id']) {
$query = "SELECT * FROM news where published = '1' ORDER by id DESC";
$result = mysql_query($query) or die(mysql_error());
$news = mysql_fetch_assoc($result);
?>
<?php do { ?>
<table width="590" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td colspan="2"><h2><? echo $news['title']; ?></h2></td>
  </tr>
  <tr>
    <td width="398"><? echo $news['story']; ?></td>
    <td width="192"><img src="<? echo $news['image']; ?>" alt="<? echo $news['shortstory']; ?>" width="192" height="108" /></td>
  </tr>
  <tr>
    <td colspan="2"><i><br />
    Posted by <? echo $news['author']; ?> on <? echo $news['date']; ?></i></td>
  </tr>
</table><br />
<?php } while ($news = mysql_fetch_assoc($result)); 
}
?>

Link to comment
Share on other sites

This should work:

<?php
//found out how many rows are in the table
$sql = "SELECT COUNT(*) FROM news";
$result = mysqli_query($cxn, $sql);
$row = mysqli_fetch_row($result);
$num = mysqli_num_rows($result);
$numrows = $row[0];

//number of rows to show per page
$rowsperpage = 10;

//finding total pages
$totalpages = ceil($numrows / $rowsperpage);

//getting the current page or setting a default
if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage']))
{
	//cast variable as integer
	$currentpage = (int) $_GET['currentpage'];
}
else
{
	//default page number
	$currentpage = 1;
}

//if current page is greater than total pages
if ($currentpage > $totalpages)
{
	//set current page to last page
	$curentpage = $totalpages;
}

//if current page is less than the first page
if ($currentpage < 1)
{
	//set current page to first page
	$currentpage = 1;
}

//offset of the list
$offset = ($currentpage - 1) * $rowsperpage;

echo "<table width=\"590\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n";
//retrieving news information from table
$query = "SELECT * FROM news WHERE published = '1' ORDER BY id DESC LIMIT $offset, $rowsperpage";
$result = mysql_query($query) or die(mysql_error());
while ($row = mysqli_fetch_assoc($result))
{	
	echo "<tr>\n";
	echo "\t<td colspan=\"2\"><h2>".$row['title']."</h2></td>\n";
	echo "</tr>\n";
	echo "<tr>\n";
	echo "\t<td width=\"398\">".$row['story']."</td>\n";
	echo "\t<td width=\"192\"><img src=\"".$row['image']."\" alt=\"".$row['shortstory']."\" width=\"192\" height=\"108\" /></td>\n";
	echo "</tr>\n";
	echo "<tr>\n";
	echo "\t<td colspan=\"2\"><i><br />\n";
	echo "\tPosted by ".$row['author']." on ".$row['date']."</i></td>\n";
	echo "</tr>\n";
}	
echo "\t</table>\n";

//BUILDING PAGINATION LINKS
//range of number of links to show
$range = 3;

echo "\t<p align=\"center\">";
//if not on page one, don't show back links
if ($currentpage > 1)
{
	//show link to go back to first page
	//echo "<a href=\"".$_SERVER['SCRIPT_NAME']."?currentpage=1\"><<</a>";
	//get previous page number
	$prevpage = $currentpage - 1;
	//show link to go back a previous page
	echo "<a href=\"".$_SERVER['SCRIPT_NAME']."?currentpage=".$prevpage."\"><</a>";
}
//loop to show links to range of pages around current page
for ($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++)
{
	//if its a valid page number
	if (($x > 0) && ($x <= $totalpages))
	{
		//if we're on the current page
		if ($x == $currentpage)
		{
			//highlight it, but dont make it a link
			echo "<b>".$x."</b>";
		}
		else
		{
			//if not current page
			//make it a link
			echo "<a href=\"".$_SERVER['SCRIPT_NAME']."?currentpage=".$x."\">".$x."</a>";
		}
	}
}

//if not on last page, show forward and last page links
if ($currentpage != $totalpages AND $num == 0)
{
	//get next page
	$nextpage = $currentpage + 1;
	//echo forward link for next page
	echo "<a href=\"".$_SERVER['SCRIPT_NAME']."?currentpage=".$nextpage."\">></a>";
	//echo forward link for last page
	//echo "<a href=\"".$_SERVER['SCRIPT_NAME']."?currentpage=".$totalpages."\">>></a>";
}
echo "</p>";
?>

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.