Jump to content

Pagination help needed ( simple)


ryansdistrict

Recommended Posts

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

 

<?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"); 
?>

Link to comment
https://forums.phpfreaks.com/topic/143913-pagination-help-needed-simple/
Share on other sites

$link2use = "http://www.monkeytooth.net";
$rowsPerPage = 50;
$pageNum = 1;
if(isset($_GET['pg'])) { $pageNum = $_GET['pg']; } elseif($_GET['pg'] == "") { $pageNum = 1; } else { $pageNum = 1; }
$offset = ($pageNum - 1) * $rowsPerPage;

$result  = mysql_query(" SELECT * FROM `names` WHERE ( UPPER(name) REGEXP '^$ns' AND `gender` LIKE '$gen' LIMIT $offset, $rowsPerPage)");

$abc = "1";
while($row = mysql_fetch_array($result)) {
echo $abc . ".) " . $row['name'] . " " . $row['gender'] . "<br />"
$abc = $abc+1;
}


$maxPage = ceil($viewcount/$rowsPerPage);
if ($pageNum > 1){
   $page  = $pageNum - 1;
   $prev  = '<a href="' . $link2use . '&pg=' . $page . '"><<</a>';
} else {
   $prev  = ''; //page one, no previous needed (i usually use graphic and make one that looks disabled).
}

if ($pageNum < $maxPage){
   $page = $pageNum + 1;
   $next = '<a href="' . $link2use . '&pg=' . $page . '">>> </a>';
} else {
   $next = ''; //last page, no next needed (i usually use graphic and make one that looks disabled).
}
echo $prev . " " . $pageNum . " of " . $maxPage . " " . $next;

 

Now this isn't going to paginate to the point where your going to have << 1 2 3 4 5 6 >>

It can be expanded to that however. this will output or should to the extent of

<< 1 of 37 >>

 

I also suggest making a dummy file copy and pasting your existing code there, then working this into it replacing what you need to etc.. and testing.. I cant guarantee the above is going to work first shot. and dont want you to go erasing your already working to one extent or another script for mine which could flop (shouldnt but could)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.