PDA

View Full Version : Pagination Help



Chris1989
10-20-2011, 10:04 AM
Hi Guys, I am still a beginner at PHP and could really do with some help please.

I have successfully done a pagination with postgre/php however I now need to simply add a title below the page numbers to say "page 1 of 4" when on page 1 etc.

My code is:



//$page = $_GET['page'];
$page = (isset($_GET['page'])) ? (int)$_GET['page'] : 0;
//echo $page;
$start = 0;
if($page >= 1){
$start = (7 * $page) - 1;
}

$sql = "SELECT id,client_name,SUBSTRING(review, 1, 120) AS review FROM repmanagement.reviews WHERE site_id = %d ORDER BY submitted DESC LIMIT %d OFFSET %d";
$sql = sprintf($sql,$SITE_ID,7,$start);
$results = pg_query($dbcon, $sql);


$format = '<p class=\'reviewmaintext\'>&ldquo;%s&rdquo;...<br /><a class=\'reviewlinks\' href=\'review.php?rid=%d\'>Click here to read more</a></p><p class=\'reviewmainperson bold\'>%s</p>';
while ($row = pg_fetch_array($results)) {
printf($format, $row['review'], $row['id'], $row['client_name']);
}
echo "<br />";


$totalperpage = 7;
$query = "SELECT COUNT(*) AS nrecords FROM repmanagement.reviews WHERE site_id = %d";
$query = sprintf($query,$SITE_ID);
$presults = pg_query($dbcon, $query);
while($prow = pg_fetch_array($presults)) {
$nrecords = $prow['nrecords'] + 1;
}

echo "<div id=\"leftspace\"></div>" ;

// $totalresults = pg_num_rows($query);
$totalpages = round($nrecords / $totalperpage);


for($i = 0; $i <= $totalpages; $i++)
{
$j = $i + 1;
echo "<a class=\"pagelinks\" href=\"reviews.php?page=$i\">$j</a>";
}

pg_close($dbcon);


I thought I could just add in below the final for loop something like "echo <h3>page $j of $totalpages</h3>"

But that just doesnt work. Any help is appreciated.

hinch
10-20-2011, 01:31 PM
here's some basic pagination I use its not perfect but it works.



<?php
$tbl_name="tblclients"; //your table name
// How many adjacent pages should be shown on each side?
$adjacents = 10;

/*
First get total number of rows in data table.
If you have a WHERE clause in your query, make sure you mirror it here.
*/
$query = "SELECT COUNT(*) as num FROM $tbl_name";
$total_pages = mysql_fetch_array(mysql_query($query));
$total_pages = $total_pages[num];

/* Setup vars for query. */
$targetpage = "dashboard.php?p=listclients"; //your file name (the name of this file)
if (strlen($_GET['display'])>0) {
$limit = mysql_real_escape_string($_GET['display']);//how many items to show per page user selected
} else {
$limit = 10; //how many items to show per page
}
$page = $_GET['page'];
if($page)
$start = ($page - 1) * $limit; //first item to display on this page
else
$start = 0; //if no page var is given, set start to 0

/* Get data. */
$sql = "SELECT * FROM $tbl_name ORDER BY ClientName ASC LIMIT $start, $limit";
$result = mysql_query($sql);

/* Setup page vars for display. */
if ($page == 0) $page = 1; //if no page var is given, default to 1.
$prev = $page - 1; //previous page is page - 1
$next = $page + 1; //next page is page + 1
$lastpage = ceil($total_pages/$limit); //lastpage is = total pages / items per page, rounded up.
$lpm1 = $lastpage - 1; //last page minus 1

/*
Now we apply our rules and draw the pagination object.
We're actually saving the code to a variable in case we want to draw it more than once.
*/
$pagination = "";
if($lastpage > 1)
{
$pagination .= "<div class=\"pagination\">";
//previous button
if ($page > 1)
$pagination.= "<a href=\"$targetpage&page=$prev\">&lt;&lt; previous</a>";
else
$pagination.= "<span class=\"disabled\">&lt;&lt; previous</span>";

//pages
if ($lastpage < 7 + ($adjacents * 2)) //not enough pages to bother breaking it up
{
for ($counter = 1; $counter <= $lastpage; $counter++)
{
if ($counter == $page)
$pagination.= "<span class=\"current\">$counter</span>";
else
$pagination.= "<a href=\"$targetpage&page=$counter\">$counter</a>";
}
}
elseif($lastpage > 5 + ($adjacents * 2)) //enough pages to hide some
{
//close to beginning; only hide later pages
if($page < 1 + ($adjacents * 2))
{
for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
{
if ($counter == $page)
$pagination.= "<span class=\"current\">$counter</span>";
else
$pagination.= "<a href=\"$targetpage&page=$counter\">$counter</a>";
}
$pagination.= "...";
$pagination.= "<a href=\"$targetpage&page=$lpm1\">$lpm1</a>";
$pagination.= "<a href=\"$targetpage&page=$lastpage\">$lastpage</a>";
}
//in middle; hide some front and some back
elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2))
{
$pagination.= "<a href=\"$targetpage&page=1\">1</a>";
$pagination.= "<a href=\"$targetpage&page=2\">2</a>";
$pagination.= "...";
for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++)
{
if ($counter == $page)
$pagination.= "<span class=\"current\">$counter</span>";
else
$pagination.= "<a href=\"$targetpage&page=$counter\">$counter</a>";
}
$pagination.= "...";
$pagination.= "<a href=\"$targetpage&page=$lpm1\">$lpm1</a>";
$pagination.= "<a href=\"$targetpage&page=$lastpage\">$lastpage</a>";
}
//close to end; only hide early pages
else
{
$pagination.= "<a href=\"$targetpage&page=1\">1</a>";
$pagination.= "<a href=\"$targetpage&page=2\">2</a>";
$pagination.= "...";
for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++)
{
if ($counter == $page)
$pagination.= "<span class=\"current\">$counter</span>";
else
$pagination.= "<a href=\"$targetpage&page=$counter\">$counter</a>";
}
}
}

//next button
if ($page < $counter - 1)
$pagination.= "<a href=\"$targetpage&page=$next\">next &gt;&gt;</a>";
else
$pagination.= "<span class=\"disabled\">next &gt;&gt;</span>";
$pagination.= "</div>\n";
}
?>

<?php
while($vars = mysql_fetch_array($result)) {
if ($vars[3]==1) {
$clienttype = "Commercial (C&amp;I)";
}
if ($vars[3]==2) {
$clienttype = "Construction (C&amp;D)";
}
if ($vars[3]==3) {
$clienttype = "Multi-Site";
}
echo "<tr><td>".stripslashes($vars[1])."</td><td>".stripslashes($vars[2])."</td><td>$clienttype</td><td align=\"center\"><a href=\"dashboard.php?p=viewclient&id=".stripslashes($vars[0])."\">View</a> :: <a href=\"dashboard.php?p=editclient&id=".stripslashes($vars[0])."\">Edit</a></td></tr>";
}
?>
</table>
<?php echo $pagination?>


failing that I think someone has a pagination class available for usage here think its Fu-lou perhaps