FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Displaying fixed number of records /page





bukaida
I am using php 4.4 with mysql 5.01 and apache 2. In the php program for a particular query, more than 100 records are displayed.How can I display 40 records per page and give a next button to display next 40.The option should be automatic in nature depending on the number of records.I am using the following code-
Code:

<? header("Content-Type: text/html; charset=UTF-8"); ?>
<?
include 'connect.php';
// Fetch all the records sorted by title

    $query="SELECT *
    FROM mytable
    ORDER BY book_title";
    $result=mysql_query($query)or die(mysql_error());
    $num=mysql_num_rows($result);
    echo '
<html>
<head>
<title> </title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
</head><body bgcolor="#66ccff">';

    if($num>0){
  echo'<div align="center"><strong><font face="Sylfaen" color="#800000" size="+3"><u>List of Books According to title<br>Total records:';
  echo $num;
  echo '</u></div><br><br><hr>';
  echo'<table align="center" border="1">
    <tr><td align="center"><b> Book ID</b> </td>
    <td align="center"><b>Title</b></td>
    <td align="center"><b>Category</b></td>
    <td align="center"><b> Volume</b></td>
    <td align="center"><b> Page Start</b></td>
    <td align="center"><b> Page End</b></td>
    <td align="center"><b> Total Pages</b></td>
    <td align="center"><b> Total Words</b></td>
   </tr>';

      $bg='#eeeeee';

      while($myrow=mysql_fetch_array($result)){
        extract($myrow);
        $bg=($bg=='#eeeeee' ? '#ffffff' : '#eeeeee');
       
        $query2="SELECT *
                 FROM mytable;";
   
    $result2=mysql_query($query2)or die(mysql_error());
    $myrow2=mysql_fetch_array($result2);
   
    echo "<tr bgcolor=$bg>
<td align=\"center\"><a href=\"display.php?id=$myrow[0]\">$myrow[0]</a></td>
        <td align=\"left\">".stripslashes($myrow[1])."</td>
        <td align=\"left\">".stripslashes($myrow[2])."</td>
        <td align=\"left\">".stripslashes($myrow[3])."</td>
        <td align=\"left\">".stripslashes($myrow[4])."</td>
         <td align=\"left\">".stripslashes($myrow[5])."</td>
       <td align=\"left\">".stripslashes($myrow[6])."</td>
       <td align=\"left\">".stripslashes($myrow[7])."</td>
       </tr>";
}
}else{
  echo'<p><font color="red" size="+3">There is currently no Book record for the Author:&nbsp;&nbsp;';echo $cls;echo'</font></p>';
}

echo'</body></html>';
include 'footer.php';
 ?>


Please help.
urtrivedi
I am posting my own code, you need to rework on it according to your need.

Code:


   <?php
   
   function linkstr($reccount,$rowperpage,$totalpages)
   {
   

      $showlinks=4; //how many links to show 1|2|3|4
      $frompage=$_POST['page'];
      $topage=$_POST['page']+$showlinks;
      if ($topage>$totalpages)
         $topage=$totalpages;
      
      if ($totalpages>$showlinks )
      {
         if (($topage-$frompage)<$showlinks)
            $frompage=$topage-$showlinks;
      }
      else
      {
         $frompage=1;
         $topage=$totalpages;
      }
      
      $prevpageno=$frompage-1;
      $nextpageno=$topage+1;
      if($nextpageno>$totalpages)
         $nextpageno=$totalpages;
      
      $linkstr=   "";
      $imgno="";
      $prevchar="";//chr(171);
      $nextchar="";//chr(187);

      if ($totalpages>1)
      {
          
          if ($frompage>1)
            $linkstr.= "\n<a class='text12' href='javascript:link_click(1);' title='First'><img src='../images/first{$imgno}.gif' border=0 >{$prevchar}{$prevchar}</a> | ";   
          if ($frompage>2)            
            $linkstr.=  "\n<a  class='text12' href='javascript:link_click({$prevpageno});' title='Previous'><img src='../images/previous{$imgno}.gif' border=0 >{$prevchar}</a> | ";   
         
         //for loop
         for ($i=$frompage;$i<=$topage;$i++)
         {
             if ($_POST['page']!=$i)
               $linkstr.= "\n<a  class='text12' href='javascript:link_click({$i});'><u><strong>";
            $linkstr.= $i;
             if ($_POST['page']!=$i)            
               $linkstr.= "</strong></u></a>";   
            if ($i<$topage)      
               $linkstr.= " | ";
         }
         //end for
         if ($topage<$totalpages-1)
            $linkstr.=   "\n | <a  class='text12' valign='bottom' href='javascript:link_click({$nextpageno});' title='Next'><img src='../images/next{$imgno}.gif' border=0>{$nextchar}</a>";
         if ($topage<$totalpages)
            $linkstr.=   "\n | <a  class='text12' href='javascript:link_click({$totalpages});' title='Last'><img src='../images/last{$imgno}.gif' border=0>{$nextchar}{$nextchar}</a>";   
            
      }
      
      return $linkstr;
   }

   $query="SELECT * FROM YOU TABLE WHERE YOUR CONDTIONS ";
   
   $reccount=execute query ("SELECT COUNT(*) FROM (".$query.") A ",$connection); // to find total no of records
   
   $rowperpage=10;
   
   $totalpages=ceil($reccount/$rowperpage);
   
   if (!isvalueset($_POST['page']))
      $_POST['page']=1;
      
   $fromrecord=((($_POST['page']-1)*$rowperpage)+1);
   $torecord=$_POST['page']*$rowperpage;
   
   $fromrecord=$fromrecord-1;
   $torecord=$rowperpage;


   $query=$query."  LIMIT {$fromrecord},{$torecord} ";


   //execute your query here;
   
?>
<html>
<head>
<script language='javscript'>
   function link_click(pageno)
   {
       document.frmsearch.target='_self';
       document.frmsearch.page.value=pageno;
      document.frmsearch.submit();
   }
</script>
</head>
<body>
<?php   
   print "\n<table width=100% border=1 cellspacing=0 cellpadding=0>";
   //show your records here
   print "\n</table>";   


   //display page navigaation
   print "\n<table width=100% border=1 cellspacing=0 cellpadding=0>";
   print "\n<tr>";
   

   if ($reccount>0)
   {
      print "\n<td align=left width=25%><b>Total Record(s): {$reccount}</b></td>";
      if ($showdesign)
      {      
         print "\n<td align=center width=50%>".linkstr($reccount,$rowperpage,$totalpages)." &nbsp;</td>";
         print "\n<td align=right width=25%><b>Page {$_POST['page']} of {$totalpages}</b> &nbsp;</td>";
      }
   }
   else
   {
      print "\n<td align=center width=100%><b><br>No Record found.<br></b></td>";
   }
   print "\n</tr>";
   print "\n</table>";   
   
?>
<form name=frmsearch id=frmsearch method=post>
<input  type=hidden name=page id=page value=1>
</form>
<body>
</html>
bukaida
Thanx buddy, thats a huge chunk of code but I will try it.
urtrivedi
If you struck somewhere then do let me know.
I will explain any portion that you are not able to get. Actually someone has given me this code for pagination and it was even longer than whatever I have posted.
Related topics
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.