Displaying dbase content

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • David

    Displaying dbase content

    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!!
  • haptiK

    #2
    Re: Displaying dbase content

    in your file that you want the pager on:

    <?php

    require_once('p agedresults.php ');

    $cnx = @mysql_connect( 'localhost','us er','password') ;
    mysql_select_db ('database',$cn x);
    $rs = new MySQLPagedResul tSet("select statement to database", 10,$cnx);
    //10 is number of thingies to pull through

    if(!$rs) die(mysql_error ());
    ?>


    //save the following into 'pagedresults.p hp'


    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 MySQLPagedResul tSet
    {

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

    function MySQLPagedResul tSet($query,$pa geSize,$cnx)
    {
    global $resultpage;

    $this->results = @mysql_query($q uery,$cnx);
    $this->pageSize = $pageSize;
    if ((int)$resultpa ge <= 0) $resultpage = 1;
    if ($resultpage > $this->getNumPages( ))
    $resultpage = $this->getNumPages( );
    $this->setPageNum($re sultpage);
    }

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

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

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

    $this->page = $pageNum;
    $this->row = 0;
    mysql_data_seek ($this->results,($page Num-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_arr ay($this->results);
    }

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

    return $nav;
    }
    }

    ?>


    David wrote:
    [color=blue]
    > 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!![/color]

    Comment

    • David

      #3
      Re: Displaying dbase content

      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\ht docs\drivemaste rs\pagedresults .php on line 75

      The URL seems to suggest it is working



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

      any ideas???

      Comment

      Working...