Results 1 to 1 of 1

Thread: PHP Rs Paging

Threaded View

  1. #1

    Thread Starter
    Addicted Member señorbadger's Avatar
    Join Date
    Oct 2003
    Location
    Mud pools of wellingborough
    Posts
    193

    PHP Rs Paging

    Hello, ive written a Php Recordset Paging function i see problems
    but cant think of solutions

    please view attached php file and comment on how to improve

    if you would like to use please feel free

    File Attached = Paging.php
    Content Below

    PHP Code:
    <style type="text/css">
    <!--
    input {
        background-color: #CCD6FF;
        font-size: small;
    }
    -->
    </style>
    <?php 
    function page_recordset($rs,$showfields)
    {

        
    //if their is no results then return message saying so
        
    if (mysql_num_rows($rs) > 0)
            {
                
    //read the get vars, to find current page
                
    if (!empty($_GET['page']) && is_numeric($_GET['page'])) $mypage $_GET['page']; else $mypage =1;
                
    //read the get vars, to get results per-page
                
    if (empty($_GET['perpage']) || !is_numeric($_GET['perpage'])) $perpage=10; else $perpage $_GET['perpage'];
                
    //store the number of results
                
    $numres mysql_num_rows($rs);
                
    //divide number of results by number per page
                
    $numpages $numres $perpage;
                
    //this is the bit i need help with to improve
                
    $bit explode(".",$numpages);
                
    // it needs to ignore everything after the decimal place (thus always round down)
                
    $numpages $bit[0];
                
    //if it wasn't dead on a page then add an extra page for the remaining results
                
    if ($numres $perpage != 0$numpages++;
                
    //if they gave us a invalid page act as page 1
                
    if ($mypage $numpages$mypage =1;
                
    //stop is the last record number 
                //calculate = current page * per page
                
    $stop $mypage $perpage;
                
    //if its the last page then the end is the last record
                
    if ($mypage == $numpages$stop $numres;
                    
    //if the page is 1 then record 0 is the start
                
    switch ($mypage)
                    {
                        case 
    1:
                            
    $start 0;
                            break;
                        case 
    2:
                            
    //the start is current page + (per page -2)
                            
    $start = ($mypage + ($perpage -2));
                            break;
                        default:
                            
    //start is (current page * per page) - per page
                            
    $start = ($mypage $perpage)-$perpage;
                            break;
                    }
                echo 
    "Showing records $start to $stop <br>\n";
                
    //previous page is page -1
                
    $prev $mypage 1;
                
    //if previous page = 0 then its the current page
                
    if ($prev == $prev 1;
                
    //if its the last page then next = current else next = current + 1
                
    if ($mypage == $numpages$next $numpages; else $next $mypage 1;
                
    //store the perpage value
                
    $qstring "&perpage=".$perpage;
                
    $numcol sizeof($showfields);
                
    //create the table structure
                
    ?>

    <table border="0" cellpadding="0" cellspacing="0" style="border:thin solid #CDADFF;" width="92%">
                <?php
                
    echo "\t<tr>";
                for (
    $c=0;$c<$numcol;$c++)
                
    //loop to create the header
                
    {
                echo 
    "\n\t\t<td bgcolor=\"#DDCDFF\"><strong>$showfields[$c]</strong></td>";
                }
                echo 
    "\n\t</tr>\n";
                
    //loop through and display results
                
    for ($e $start;$e $stop;$e++)
                    {
                        
    //fetch a row from the rs
                        
    $row mysql_fetch_array($rs,MYSQL_ASSOC);
                        if (
    $e == 0$bg "#DFDFFF"; else $bg "#FCFCFF";  
                        echo 
    "\r\t<tr bgcolor=\"$bg\">";
                        for (
    $c=0;$c<$numcol;$c++)
                            
    //loop to create the rows data
                            
    {
                                echo 
    "\n\t\t<td style=\"font-size:small;\">&nbsp;&nbsp;".$row[$showfields[$c]]."</td>";
                            }
                        echo 
    "\r\t</tr>";
                    }
                    echo 
    "\n\t<tr>\n\t\t<td colspan=\"".($numcol-1)."\" bgcolor=\"#CDADFF\" style=\"font-size:small;\">";
                    
    ?>
                    <form action="<?php echo $_SERVER['PHP_SELF']?>" method="get">
                        <form name="form1">Show 
      <select name="perpage">
        <option value="10">10</option>
        <option value="25">25</option>
        <option value="50">50</option>
        <option value="100">100</option>
      </select> Results per page  
      <input type="Submit" name="Mode" value="Go">
    </form>

                    </form>
                    <?php 
                    
    echo "</td>";
                echo 
    "\n\t\t<td colspan=\"1\" bgcolor=\"#CDADFF\">";
                 if (
    $numpages 1) {?>
                    <table border="0" align="right" cellpadding="2" cellspacing="0" class="main_border">
                      <tr>
                        <td><a href="<?php echo $_SERVER['PHP_SELF']?>?page=1<?php echo $qstring;?>">First</a></td>
                        <td><a href="<?php echo $_SERVER['PHP_SELF']?>?page=<?php echo $prev.$qstring;?>">Previous</a></td>
                        <td>
                          <?php 
                        
    for ($a 1;$a < ($numpages +1); $a++)
                        {
                        
                            if (
    $a == $mypage)
                                {
                                    
    ?>
                          <a href="<?php echo $_SERVER['PHP_SELF']?>?page=<?php echo $a.$qstring;?>" class="location"><span style="color:#FF2323;"><?php echo $a;?></span></a>
                          <?php 
                                
    }
                            else
                                {
                                    
    ?>
                          <a href="<?php echo $_SERVER['PHP_SELF']?>?page=<?php echo $a.$qstring;?>" class="location"><?php echo $a;?></a>
                          <?php 
                                
    }
                            }
                        
    ?>
                        </td>
                        <td><a href="<?php echo $_SERVER['PHP_SELF']?>?page=<?php echo $next.$qstring;?>">Next</a></td>
                        <td><a href="<?php echo $_SERVER['PHP_SELF']?>?page=<?php echo $numpages.$qstring;?>">Last</a></td>
                      </tr>
                    </table><?php }
                echo 
    "</td>\n\t</tr>";
                
                
    ?>
    </table><?php
            
    }
            else
            {
                echo 
    "The Supplied Recordset is empty\n <br>\n";
            }
    }
    //test by giving a rs
    $user "root";
    $pass "**********";
    $host "localhost";
    //connect to mysql or show error
    $mydb mysql_connect($host,$user,$pass) or die("Could not connect to database server<br><br>\n<h2>Technical Information</h2>".mysql_error());
    //select the database
    mysql_select_db("website",$mydb) or die(mysql_error());
    $rs mysql_query("select * from articles");
    $fd = array("articlename","articledesc","articledate");
     
    page_recordset($rs,$fd)
    ?>
    Attached Files Attached Files

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width