We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,574 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Search pagination

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

2
Contributors
4
Replies
1 Day
Discussion Span
1 Year Ago
Last Updated
5
Views
Question
Answered
mmnewbee
Newbie Poster
13 posts since Apr 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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

pritaeas
Posting Prodigy
Moderator
9,265 posts since Jul 2006
Reputation Points: 1,173
Solved Threads: 1,456
Skill Endorsements: 86
<?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>";
    }
}
?>
mmnewbee
Newbie Poster
13 posts since Apr 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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).

pritaeas
Posting Prodigy
Moderator
9,265 posts since Jul 2006
Reputation Points: 1,173
Solved Threads: 1,456
Skill Endorsements: 86

Thanks did what you suggested and it worked.

mmnewbee
Newbie Poster
13 posts since Apr 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
Question Answered as of 1 Year Ago by pritaeas

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.0851 seconds using 2.8MB