Welcome visitor
* Login
* Register
It is currently September 20th, 2019, 1:49 pm
All times are UTC [ DST ]
* Search
View unanswered posts - View active topics

FORUMS » Server Side » PHP



Post new topic Reply to topic  [ 2 posts ] 
Author Message
  fixin a script - numbered pages
PostPosted: February 9th, 2009, 6:55 pm 
User avatar

Posts: 81
Hi,
I want to create on PHP a pagination code to number the pages for a query result to MySQL. I got lost kind on the middle of the process, so I will really apreciate any help.


Top
 Profile  
 
  Re: fixin a script - numbered pages
PostPosted: February 9th, 2009, 8:00 pm 

Posts: 117
//rows per page
$rpp =10;

extract($_GET);

$pagNum = isset($pagNum) ? $pagNum : '1';

//headlers for your query results
echo '
<table name="display" align="center" cellspacing=0 cellpadding=3>
  <tr>
    <th width=30 align=center>As</th>
    <th width=100 align=center>many</th>
    <th width=30 align=left>as</th>
    <th width=50 align=left>You</th>
    <th width=100 align=left>need</th>
  </tr>';


$db_query=mysql_query("SELECT * FROM table WHERE id LIKE '%' ORDER BY id ASC ");

$totalRows = mysql_numrows($db_query);


$rowFrom = ($pagNum * $rpp) - ($rpp - 1);
$rowTo = $pagNum * $rpp;
if ($rowFrom > $totalRows) {$rowTo = $totalRows;}

$numRow = 1;

while ($numRow < $rowFrom)
    {
    $db_Row=mysql_fetch_array($db_query);
    ++ $numeroRow;
    }

//this is to display the info. The name of the variables would be the name of the fields on MySQL
while (($numRow >= $rowFrom) and ($numRow <= $rowTo))
    {
        $db_Row=mysql_fetch_array($db_query);
        extract($db_Row);
             <tr> 
                <td align=center><b><small>' . $numRow . '</small></b></td>
                <td align=center>' . $content. '</td>
                <td align=left>' . $from . '</td>
                <td align=left>' . $MySQL . '</td>
                <td align=left>' . $table . '</td>
             </tr>';
        ++ $numRow;
    }

echo '</table>';

//optional footnote
if (mysql_numrows($db_query) != 0){
   echo '<small>Showing = ' , $rowTo - $rowFrom + 1 , ' (from ' . $rowFrom . ' to' . $rowTo . ')';}
echo '<br /><br />';

//getting the total of pages
if ($rpp < $totalRows) {
  $totalPages = ceil($totalRows/$rpp);
  echo 'Page ' . $pagNum . ' of ' . $totalPages  . '<br /><br />';


    if ($pagNum > 1) {echo '<a href="?pagNum=' , $pagNum - 1 , '">< PREVIOUS</a> &nbsp;&nbsp; ';}
    else {echo '< PREVIOUS &nbsp;&nbsp; ';}//with no link option, is there is no previous page

    $numPage = 1;
    while ($numPage <= $totalPages)
        {
        if ($numPage == $pagNum){echo $numPage . ' ';}
        else {echo '<a href="?pagNum=' . $numPage . '">' . $numPage . '</a> ';}
        ++ $numPage;
        }

    if ($pagNum < $totalPages) {echo ' &nbsp;&nbsp;<a href="?pagNum=' , $pagNum + 1 , '"> NEXT ></a>';}
    else {echo ' &nbsp;&nbsp; NEXT >';}//no link as there it is already the last page

    echo '<br /><br />';
    echo '<a href="?pagNum=1">first page</a> - <a href="?pagNum=' . $totalPages . '">last page</a></small>';
}


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

FORUMS » Server Side » PHP


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron