pagination

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
hrubos
Forum Contributor
Posts: 172
Joined: Sat Oct 07, 2006 3:44 pm

pagination

Post by hrubos »

How to do to output table with default row by pagination. I have done it but it'snot exactly

Code: Select all

<?php

db_connect();
if (isset($_GET['pageno'])) {
   $pageno = $_GET['pageno'];
} else {
   $pageno = 1;
}

$query = "SELECT rc_student FROM student ";
$result = mysql_query($query)or die(mysql_error);
$numrows = mysql_numrows($result);

for($i =0;$i<$numrows;$i++){

$rc = mysql_result($result,$i,"rc_student");

echo "<table><tr><td>$rc</td></tr></table>";

}
echo $numrows;
$rows_per_page = 5;
$lastpage      = ceil($numrows/$rows_per_page);
echo $lastpage;

$pageno = (int)$pageno;
if ($pageno < 1) {
   $pageno = 1;
} elseif ($pageno > $lastpage) {
   $pageno = $lastpage;
} // if

$limit = 'LIMIT ' .($pageno - 1) * $rows_per_page .',' .$rows_per_page;

$query = "SELECT * FROM student $limit";

$result = mysql_query($query) or die(mysql_error());

if ($pageno == 1) {
   echo " FIRST PREV ";
} else {
   echo " <a href='{$_SERVER['PHP_SELF']}?pageno=1'>FIRST</a> ";
   $prevpage = $pageno-1;
   echo " <a href='{$_SERVER['PHP_SELF']}?pageno=$prevpage'>PREV</a> ";
} // if

echo " ( Page $pageno of $lastpage ) ";

if ($pageno == $lastpage) {
   echo " NEXT LAST ";
} else {
   $nextpage = $pageno+1;
   echo " <a href='{$_SERVER['PHP_SELF']}?pageno=$nextpage'>NEXT</a> ";
   echo " <a href='{$_SERVER['PHP_SELF']}?pageno=$lastpage'>LAST</a> ";
} // if



?>
Begby
Forum Regular
Posts: 575
Joined: Wed Dec 13, 2006 10:28 am

Post by Begby »

So what is it not doing right?
hrubos
Forum Contributor
Posts: 172
Joined: Sat Oct 07, 2006 3:44 pm

Post by hrubos »

thankyou, i have solved it.
I have a question, this code which i will posted then,is for only the easy query

Code: Select all

$limit = 'LIMIT ' .($pageno - 1) * $rows_per_page .',' .$rows_per_page;
$query = "SELECT * FROM student $limit ";
if I have a query like this, how to do with $limit ???

Code: Select all

SELECT s.id_student,s.rc_student,s.last_name,s.first_name,s.street,
                      s.city,s.country,s.number_telefone,
                      s.faculty,s.year_school,h.number_room,p.building
               FROM student s,housing h,room p
                WHERE s.rc_student=h.rc_student
                AND h.id_numberRoom=p.id_numberRoom
                ORDER BY s.first_name
This code is repaired and someone can use .

Code: Select all

<?php
include ("template/function.php");
db_connect();
if (isset($_GET['pageno'])) {
   $pageno = $_GET['pageno'];
} else {
   $pageno = 1;
}

$query = "SELECT rc_student FROM student ";
$result = mysql_query($query)or die(mysql_error);
$numrows = mysql_numrows($result);


$rows_per_page = 5;
$lastpage      = ceil($numrows/$rows_per_page);


$pageno = (int)$pageno;
if ($pageno < 1) {
   $pageno = 1;
} elseif ($pageno > $lastpage) {
   $pageno = $lastpage;
} // if

$limit = 'LIMIT ' .($pageno - 1) * $rows_per_page .',' .$rows_per_page;

$query = "SELECT * FROM student $limit ";


$result = mysql_query($query) or die(mysql_error());
$num = mysql_numrows($result);

// here to output

for($i =0;$i<$num;$i++){

$rc = mysql_result($result,$i,"rc_student");

echo "<table><tr><td>$i</><td>$rc</td></tr></table>";

}


if ($pageno == 1) {
   echo " FIRST PREV ";
} else {
   echo " <a href='{$_SERVER['PHP_SELF']}?pageno=1'>FIRST</a> ";
   $prevpage = $pageno-1;
   echo " <a href='{$_SERVER['PHP_SELF']}?pageno=$prevpage'>PREV</a> ";
} // if

echo " ( Page $pageno of $lastpage ) ";

if ($pageno == $lastpage) {
   echo " NEXT LAST ";
} else {
   $nextpage = $pageno+1;
   echo " <a href='{$_SERVER['PHP_SELF']}?pageno=$nextpage'>NEXT</a> ";
   echo " <a href='{$_SERVER['PHP_SELF']}?pageno=$lastpage'>LAST</a> ";
} // if



?>
Post Reply