By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,831 Members | 2,296 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,831 IT Pros & Developers. It's quick & easy.

Displaying dbase content

P: n/a
I am retreiving information from a database and it works ok, but its
rather a lot and I like to limit each web page to 10 rows of data,
then put the next ten on a new web page, probably with a back|forward
buttons at the bottom.

Problem is I really not sure how to do this any would hoping someone
could throw me some advice!

Cheers!!
Jul 16 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
in your file that you want the pager on:

<?php

require_once('pagedresults.php');

$cnx = @mysql_connect('localhost','user','password');
mysql_select_db('database',$cnx);
$rs = new MySQLPagedResultSet("select statement to database", 10,$cnx);
//10 is number of thingies to pull through

if(!$rs) die(mysql_error());
?>
//save the following into 'pagedresults.php'
when you want to call the navigator simply call it with

$rs->getPageNav();

and then you can format the results of $rs as you see fit :)
<?php

class MySQLPagedResultSet
{

var $results;
var $pageSize;
var $page;
var $row;

function MySQLPagedResultSet($query,$pageSize,$cnx)
{
global $resultpage;

$this->results = @mysql_query($query,$cnx);
$this->pageSize = $pageSize;
if ((int)$resultpage <= 0) $resultpage = 1;
if ($resultpage > $this->getNumPages())
$resultpage = $this->getNumPages();
$this->setPageNum($resultpage);
}

function getNumPages()
{
if (!$this->results) return FALSE;

return ceil(mysql_num_rows($this->results) /
(float)$this->pageSize);
}

function setPageNum($pageNum)
{
if ($pageNum > $this->getNumPages() or
$pageNum <= 0) return FALSE;

$this->page = $pageNum;
$this->row = 0;
mysql_data_seek($this->results,($pageNum-1) * $this->pageSize);
}

function getPageNum()
{
return $this->page;
}

function isLastPage()
{
return ($this->page >= $this->getNumPages());
}

function isFirstPage()
{
return ($this->page <= 1);
}

function fetchArray()
{
if (!$this->results) return FALSE;
if ($this->row >= $this->pageSize) return FALSE;
$this->row++;
return mysql_fetch_array($this->results);
}

function getPageNav($queryvars = '')
{
if (!$this->isFirstPage())
{
$nav .= "<a href=\"?resultpage=".
($this->getPageNum()-1).'&'.$queryvars.'">Prev</a> ';
}
if ($this->getNumPages() > 1)
for ($i=1; $i<=$this->getNumPages(); $i++)
{
if ($i==$this->page)
$nav .= "$i ";
else
$nav .= "<a href=\"?resultpage={$i}&".
$queryvars."\">{$i}</a> ";
}
if (!$this->isLastPage())
{
$nav .= "<a href=\"?resultpage=".
($this->getPageNum()+1).'&'.$queryvars.'">Next</a> ';
}

return $nav;
}
}

?>
David wrote:
I am retreiving information from a database and it works ok, but its
rather a lot and I like to limit each web page to 10 rows of data,
then put the next ten on a new web page, probably with a back|forward
buttons at the bottom.

Problem is I really not sure how to do this any would hoping someone
could throw me some advice!

Cheers!!


Jul 16 '05 #2

P: n/a
I can get the code you gave me to display the first 10 results but the
navbar at the bottom refuses to work, and I get a notice telling me

Notice: Undefined variable: nav in c:\program files\apache
group\apache\htdocs\drivemasters\pagedresults.php on line 75

The URL seems to suggest it is working

http://localhost/Drivemasters/pictur...irstname=david

but the content displayed on this page is exactly the same as page 1
so clearly it hasnt worked!

any ideas???
Jul 16 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.