Jump to content

[SOLVED] Pagination of MySQL Results


cheechm

Recommended Posts

Hello,

Could someone help paginate the following code? I have tried so many tutorials without results. I would be grateful if it could be 7 rows a page.

Thank you

<?php

$con = mysql_connect("***","***","***");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("b7_463487_LPS", $con);

$query = mysql_query("SELECT * FROM points ORDER BY LastName ASC, FirstName ASC") or die(mysql_error());
$rows = mysql_num_rows($data);  

while($row = mysql_fetch_array($query))
  {

$link = 'addpoints.php?firstname=' . $row['FirstName'] . '&lastname=' . $row['LastName'] . '&Points=' . $row['Points']; 
$link2 = 'confirmdelete.php?firstname=' . $row['FirstName'] . '&lastname=' . $row['LastName']; 

  echo $row['FirstName'] . " " . $row['LastName'] ." / ". $row['Tutor'] . " / " . $row['Points'] ;
  echo " ";
  echo " / ";
  echo "<a href='".$link."'> +points </a>"; 
  echo "/";
  echo "<a href='".$link2."'> -delete </a>"; 
  echo "/";

  echo "<br />";
  }  

mysql_close($con)

?>

Link to comment
https://forums.phpfreaks.com/topic/64851-solved-pagination-of-mysql-results/
Share on other sites

ok I have thrown this together and it should work fine ;D as i briefly tested it.

 

<?php

$con = mysql_connect("***","***","***");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("b7_463487_LPS", $con);

$page_num = $_GET['page'];
if(empty($page_num))
{
$page_num = 1;
}
$ppp = 7; //posts per page
$min_ppp = ($ppp*$page_num)-$ppp;
$query = mysql_query("SELECT * FROM points") or die(mysql_error());
$num_rows = mysql_num_rows($query);
$query = mysql_query("SELECT * FROM points ORDER BY LastName ASC, FirstName ASC LIMIT $min_ppp, $ppp") or die(mysql_error());
$rows = mysql_num_rows($data);
$pages = ceil($num_rows/$ppp);

while($row = mysql_fetch_array($query))
{
$first_name = $row['FirstName'];
$last_name = $row['LastName'];
$points = $row['Points'];
$tutor = $row['Tutor'];

$link = "addpoints.php?firstname={$first_name}&lastname={$last_name}&Points={$points}"; 
$link2 = "confirmdelete.php?firstname={$first_name}&lastname={$last_name}"; 

echo "{$first_name} {$last_name} /{$tutor} /{$points}";
echo " ";
echo " / ";
echo "<a href=\"{$link}\"> +points </a>"; 
echo "/";
echo "<a href=\"{$link2}\"'> -delete </a>"; 
echo "/";
echo "<br />";
}

if($page_num == 1)
{
echo "Prev ";
} else
{
	$prev_page = $page_num-1;
	echo "<a href=\"{$PHP_SELF}?page={$prev_page}\">Prev</a>";
}
for($i = 1; $i <= $pages; $i++)
{
if($i == 1)
{
	echo " ";
}
if($i == $page_num)
{
	echo $i;
} else
	{
		echo "<a href=\"{$PHP_SELF}?page={$i}\">{$i}</a>";
	}
if($i != $pages)
{
	echo ", ";
} else
	{
		echo " ";
	}
}
if($page_num == $pages)
{
echo "Next";
} else
{
	$next_page = $page_num+1;
	echo "<a href=\"{$PHP_SELF}?page={$next_page}\">Next</a>";
}
mysql_close($con)

?>

 

Hope it helps ;D

 

~ Chocopi

REALLY ?!?! Sweet ;D

 

And by the way, just in case you havent looked this script will list all the numbers so if you had 100 pages it would write 1,2,3,4,5,6,7,8,9,10,11,12,13,14 and so forth until 100.

 

So what im saying is that, if you have lots of pages it wont work pretty.

 

Well im glad it works

 

~ Chocopi

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.