I need help to solve a problem that has been troubling me for some time. I have a php search page that im having trouble paginating. The problem is that only the first page outputs anything the rest of the pages are blank. And I have already gone through all the posts on daniweb on pagination and search with php, and I have no problem with either of them the only problem is how to combine them effectively.
Please please I really need your help. Thanx

Recommended Answers

All 4 Replies

Sounds like an error in your query. We can probably help you if you post some code. (remember to use code tags.)

<?php
require_once("db_select.php");
db_select("me");
if(isset($_POST['search'])){
//get the year to search records for
$year = $_POST['year'];
//get the total number of rows returned
$query = "SELECT * FROM child where year(birthday) = '$year'";
$resulta = mysql_query($query);
if(!$resulta){
    die("Database Query Failed". mysql_error());
}

//get the total number of rows to be returned
$num = mysql_num_rows($resulta);
//set the number of rows to show per page
$perpage = 1;

echo $num;

//find the total number of pages that will be returned
$totalpages = ceil($num/$perpage);

//get the current page or set a default
if(isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])){
//cast var as int
    $currentpage = (int)$_GET['currentpage'];
    }else{
        //set page number to the default
        $currentpage = 1;
}

//the offsset of the list based on the current page
$offset = ($currentpage - 1) * $perpage;

//get info from db
$sql = "SELECT * FROM child WHERE year(birthday)='$year' LIMIT $offset,$perpage";
$result = mysql_query($query);
$number = mysql_num_rows($result);
echo $number;

//while there are rows to be fetched display them
while($list = mysql_fetch_array($result)){
    echo "<br />" ."name: ".$list['name']. "<br />";
}


//build the pagination links
//range of the number of links to show
$range = 3;
//if not on page 1, show back links
if($currentpage > 1){
//show the << link to go back to page 1
    echo "<a href='{$_SERVER['PHP_SELF']}?currentpage=1'> << </a>";
//get the previous page num
    $prevpage = $currentpage - 1;
//show < link to go back 1 page
    echo "<a href='{$_SERVER['PHP_SELF']}?currentpage=$prevpage'> << </a>";
}

//loop to show links 2 range of pages around current page
for($x=($currentpage - $range); $x < (($currentpage + $range) + 1); $x++){
//check if its a valid page number
    if(($x > 0) && ($x <= $totalpages)){
//if we're on current page
        if($x == $currentpage){
//highlight it but dont make it a link
            echo "[<b>$x</b>]";
//if not on current page
            }else{
//make it a link
            echo "<a href='{$_SERVER['PHP_SELF']}?currentpage=$x'> $x </a>";
            }
    }
}

//if not on last page show forward and last page
if($currentpage != $totalpages){
//get the next page
    $nextpage = $currentpage + 1;
//echo forward link for the next page
    echo "<a href='{$_SERVER['PHP_SELF']}?currentpage=$nextpage'> > </a>";
//echo forwad link for last page
    echo "<a href='{$_SERVER['PHP_SELF']}?currentpage=$totalpages'> >> </a>";
    }
}
?>

Once you click a link, your $_POST values are no longer available. If you need to keep them I suggest storing them in a session variable (and use code tags next time).

Thanks did what you suggested and it worked.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.