Page 1 of 1

Pagination problem

Posted: Tue Jul 12, 2005 12:43 am
by amlan_08
This is the code for pagination func
[syntax=php]function pagination()
{
$arr = array();
if (isset($_GET['pageno']))
{
$pageno = $_GET['pageno'];
}
else
{
$pageno = 1;
}

$query = "SELECT count(*) FROM tbluser";
$results = mysql_query($query)
or die(mysql_error());
$query_data = mysql_fetch_row($results);
$numrows = $query_data[0];

//echo $numrows;

$rows_per_page = 2;
$lastpage = ceil($numrows/$rows_per_page);
//echo $lastpage;

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

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

array_push($arr,$pageno);
array_push($arr,$lastpage);
array_push($arr,$limit);

print_r($arr);
return $arr;
}[/syntax]

This is the code for hyperlinking


[syntax=php]function hyperlink($pageno, $lastpage)
{
$str = "";
if ($pageno == 1)
{
$str=$str . " FIRST PREV ";
}
else
{
$str=$str . " <a href='{$_SERVER['PHP_SELF']}?pageno=1'>FIRST</a> ";
$prevpage = $pageno-1;
$str=$str . " <a href='{$_SERVER['PHP_SELF']}?pageno=$prevpage'>PREV</a> ";
}

$str=$str . " ( Page " .$pageno. " of ". $lastpage. " ) ";

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

This code is for fetching data from the table





[syntax=php]function user_list($limit)
{
$array = array();
$query = "select fname,lname,email from tbluser $limit";
$results = mysql_query($query)
or die(mysql_error());
while ($row = mysql_fetch_array($results, MYSQL_ASSOC))
{
$array[]=array('fname'=>$row['fname'],
'lname'=>$row['lname'],
'email'=>$row['email']);
}
//print_r($array);
return($array);
}[/syntax]

The output depends upon only upon the no of rows I want in per page. If I want to 2 records per page then that code only fetch 2 records from the table. Plz help me.

Posted: Tue Jul 12, 2005 2:54 am
by pleigh
you can check this thread
viewtopic.php?t=35008&highlight=
some samples about pagination...