Page 1 of 1

Pagination help needed ( simple)

Posted: Thu Feb 05, 2009 6:52 am
by ryansdistrict
hello guys
i am creating a simple script which fetches baby's names from database and shows them i made the code below so far i still need help in pagination
Please can you modify for me the code below so that i will have a nice pages links displayed for each query result

below is what am up to so far

Code: Select all

<?php
require"config.php";// connects to mysql
 
 
require("body1.php") ; // will build the page layout 
 
$ns=$_GET['name-starting'];// baby name  first letter 
$gen=$_GET['gender']; // gender 
 
 
// the basic query 
$result  = mysql_query(" SELECT * FROM `names` WHERE ( UPPER(name) REGEXP '^$ns' AND `gender` LIKE '$gen' )");
 
// building table
echo ' <table border="1" cellpadding="0" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="100%">
  <tr><td>  </td>
    <td width="20%"><b><font size="4">Name</font></b></td>
    <td width="20%"><b><font size="4">Gender </font></b></td>
  </tr>';
// showes result if exist 
while($row = mysql_fetch_array($result))
  {
 
//showes content from database and  colors blue or pink based on gender 
$name=$row['name'];
$gender=$row['gender'];
if($gender==male){ $color=E1F4FF;}
if($gender==female){ $color=FDE8F3;}
echo'  <tr bgcolor='.$color.'><td>..</td>
    <td width="20%" >'.$name.'</td>
    <td width="20%">'.$gender.'</td>
 
  </tr>';
  }
 
echo "</table>";
 
require("footer.php"); 
?>
 

Re: Pagination help needed ( simple)

Posted: Thu Feb 05, 2009 9:04 am
by susrisha
hi...
I have done a pagination of your code and am posting the resulting code to you.. Hope that helps

Code: Select all

 
<?php
require"config.php";// connects to mysql
 
 
require("body1.php") ; // will build the page layout
 
$ns=$_GET['name-starting'];// baby name  first letter
$gen=$_GET['gender']; // gender
 
//added code by susrisha
$result1  = mysql_query(" SELECT * FROM `names` WHERE ( UPPER(name) REGEXP '^$ns' AND `gender` LIKE '$gen' )");
 
$max_rows = mysql_num_rows($result1); //get the maximum rows first
$rows_per_page = 10;//set this to the number of rows you require
$max_pages = ceil($max_rows/$rows_per_page); //set the max pages limit
$page = 1;
if(isset($_GET['page']))
{
    $page = $_GET['page'];
}
$offset = ($page-1)*$rows_per_page;
 
// the basic query now modified
$result  = mysql_query(" SELECT * FROM `names` WHERE ( UPPER(name) REGEXP '^$ns' AND `gender` LIKE '$gen' ) LIMIT $offset $rows_per_page");
 
// building table
echo ' <table border="1" cellpadding="0" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="100%">
  <tr><td>  </td>
    <td width="20%"><b><font size="4">Name</font></b></td>
    <td width="20%"><b><font size="4">Gender </font></b></td>
  </tr>';
// showes result if exist
while($row = mysql_fetch_array($result))
  {
 
//showes content from database and  colors blue or pink based on gender
$name=$row['name'];
$gender=$row['gender'];
if($gender==male){ $color=E1F4FF;}
if($gender==female){ $color=FDE8F3;}
echo'  <tr bgcolor='.$color.'><td>..</td>
    <td width="20%" >'.$name.'</td>
    <td width="20%">'.$gender.'</td>
 
  </tr>';
  }
 
echo "</table>";
 
//edited by susrisha to get the next and before links
    $self = $_SERVER['PHP_SELF'];
        if($page>1)
        {
            $page= $page -1;
            $prev = "<a href = \"$self?page=$page&name-starting=$ns&gender=$gen\"><abbr title=\"Previous\">[<]</abbr></a>";
            $first = "<a href =\"$self?page=1&name-starting=$ns&gender=$gen\"><abbr title=\"First\">[<<]</abbr></a>";
        }
        else
        {
            $prev="&nbsp;";
            $first="&nbsp;";
        }//end of previous and first
        
        if($page<$max_pages)
        {
            $page = $page +1;
            $next = "<a href =\"$self?page=$page&name-starting=$ns&gender=$gen\"><abbr title=\"Next\">[>]</abbr></a>";
            $last = "<a href =\"$self?page=$max_pages&name-starting=$ns&gender=$gen\"><abbr title=\"Last\">[>>]</abbr></a>";
        }
    else
        {
            $next = "&nbsp;";
            $last = "&nbsp;";
        }
        echo $first.$prev.$next.$last;
        
//edited by susrisha
 
 
require("footer.php");
?>
 

Re: Pagination help needed ( simple)

Posted: Thu Feb 05, 2009 9:05 am
by susrisha
Here is a sample pagination class that i have written for any further usage

Code: Select all

 
<?php 
/* Developed and implemented by susrisha@gmail.com
 * For public use..Have fun
 * Member variables of the class
 * @param : $page_num -> the present page number of the page.
 * @param : $offset -> the offset from which the records are to be counted
 * @param : $rows_per_page-> the number of rows you need to have in each page
 * @param : $max_pages -> the maximum pages for the records
 */
 
class cPagination
{
    public $page_num; //the page number
    public $offset;  //the offset
    public $rows_per_page; //number of rows per page
    public $max_pages; //max pages 
    
    /* construct function for the class
     * @param : $maxrows -> them number of rows in mysql query to get the results paginated
     * @param : $rows_in_page -> the number of rows each page will have
     * 
     * function sets the variables page_num, offset, rows_per_page, $max_pages for the object
     * 
     */
    
  public function __construct($maxrows,$rows_in_page)
 {
    $this->max_pages= ceil($maxrows/$rows_in_page);
    $this->rows_per_page = $rows_in_page;
    if(isset($_GET['page']))
    {
        $this->page_num= $_GET['page'];
    }
    else
    {
        $this->page_num = 1;
    }
    $this->offset = ($this->page_num-1)*$this->rows_per_page;
    
 }//end of construct
 
 /* function get_links
  * gives out the First, prev, next, Last links 
  */
    
    public function get_links()
    {
        $self = $_SERVER['PHP_SELF'];
        if($this->page_num>1)
        {
            $page= $this->page_num -1;
            $prev = "<a href = \"$self?page=$page\"><abbr title=\"Previous\">[<]</abbr></a>";
            $first = "<a href =\"$self?page=1\"><abbr title=\"First\">[<<]</abbr></a>";
        }
        else
        {
            $prev="&nbsp;";
            $first="&nbsp;";
        }//end of previous and first
        
        if($this->page_num<$this->max_pages)
        {
            $page = $this->page_num +1;
            $next = "<a href =\"$self?page=$page\"><abbr title=\"Next\">[>]</abbr></a>";
            $last = "<a href =\"$self?page=$this->max_pages\"><abbr title=\"Last\">[>>]</abbr></a>";
        }
    else
        {
            $next = "&nbsp;";
            $last = "&nbsp;";
        }
        echo $first.$prev.$next.$last;
        
        
    }//end of get_links
    
}//end of class
 
 
 
?>