Now i have every thing working, the pagination is a bit fine, only when i click next to go to page 2 or 3, nothing is being shown on the page, the browser shows that i am on the page2 but no data is beeing shown, now when i force the page to be 2 or 3 i see the rest of the data, what am i doing wrong?

I have been researching this for a while and still didn't figure this out, i was told that the &_POST is not being passed, but i don't know what is going on and how to fix that, i am a bit nw to php and i'm stitching together code as i go along, on other thing that i have to do is secure the data from injection i may need help with that aswell, can anyone help me with this please thanks!

<?php    

if(isset($_POST['submit'])){
if(isset($_GET['go'])){
if(preg_match("/[A-Z | a-z]+/", $_POST['name'])){
$name=$_POST['name'];

include "connect/connect.php";

//-query the database table
$sql=mysql_query("SELECT userId, Attribute, Name FROM government  WHERE Name LIKE '%" . $name . "%' OR Attribute LIKE '%" . $name . "%' ORDER BY userid ASC");

///////////////-PAGINATION LOGIC./////////////
$nr=mysql_num_rows($sql); // Get total of Num rows from the database query
if (isset($_GET['pn'])) { // Get pn from URL vars if it is present
$pn = preg_replace('#[^0-9]#i', '', $_GET['pn']); // filter everything but numbers for security(new)
} else { // If the pn URL variable is not present force it to be value of page number 1
$pn = 1;
}

//This is where we set how many database items to show on each page 
$itemsPerPage = 7; 

// Get the value of the last page in the pagination result set
$lastPage = ceil($nr / $itemsPerPage);

// Be sure URL variable $pn(page number) is no lower than page 1 and no higher than $lastpage
if ($pn < 1) { // If it is less than 1
$pn = 1; // force it to be 1
} else if ($pn > $lastPage) { // if it is greater than $lastpage
$pn = $lastPage; // force it to be $lastpage's value
} 

// This creates the numbers to click in between the next and back buttons
/*$centerPages = "";
$sub1 = $pn - 1;
$sub2 = $pn - 2;
$add1 = $pn + 1;
$add2 = $pn + 2;
if ($pn == 1) {
    $centerPages .= "  <span class=\"pagNumActive\">" . $pn . "</span>  ";
    $centerPages .= "  <a href=\"search_govern.php?pn=$add1\">" . $add1 . "</a>  ";
} else if ($pn == $lastPage) {
    $centerPages .= "  <a href=\"search_govern.php?pn=$sub1\">" . $sub1 . "</a>  ";
    $centerPages .= "  <span class=\"pagNumActive\">" . $pn . "</span>  ";
} else if ($pn > 2 && $pn < ($lastPage - 1)) {
    $centerPages .= "  <a href=\"search_govern.php?pn=$sub2\">" . $sub2 . "</a>  ";
    $centerPages .= "  <a href=\"search_govern.php?pn=$sub1\">" . $sub1 . "</a>  ";
    $centerPages .= "  <span class=\"pagNumActive\">" . $pn . "</span>  ";
    $centerPages .= "  <a href=\"search_govern.php?pn=$add1\">" . $add1 . "</a>  ";
    $centerPages .= "  <a href=\"search_govern.php?pn=$add2\">" . $add2 . "</a>  ";
} else if ($pn > 1 && $pn < $lastPage) {
    $centerPages .= "  <a href=\"search_govern.php?pn=$sub1\">" . $sub1 . "</a>  ";
    $centerPages .= "  <span class=\"pagNumActive\">" . $pn . "</span>  ";
    $centerPages .= "  <a href=\"search_govern.php?pn=$add1\">" . $add1 . "</a>  ";
}

*/
// This line sets the "LIMIT" range... the 2 values we place to choose a range of rows from database in our query
//$limit = 'LIMIT ' .($pn - 1) * $itemsPerPage .',' .($pn * $itemsPerPage); 

//$limit = 'LIMIT ' .($pn - 1) * $itemsPerPage .',' .($pn * $itemsPerPage);

//$limit = 'LIMIT ' . $itemsPerPage .' OFFSET ' .($pn - 1) * $itemsPerPage;

$limit = 'LIMIT ' .($pn - 1) * $itemsPerPage .',' .$itemsPerPage;

// Now we are going to run the same query as above but this time add $limit onto the end of the SQL syntax
// $sql2 is what we will use to fuel our while loop statement below
$sql2 = mysql_query("SELECT userId, Attribute, Name FROM government  WHERE Name LIKE '%" . $name . "%' OR Attribute LIKE '%" . $name . "%' ORDER BY userid ASC $limit"); 
////////////////////////////////////-END OF PAGINATION LOGIC.///////////////////////////////////////////////////////////////////////////////

///////////////////////////////////// Pagination Display Setup /////////////////////////////////////////////////////////////////////////////
$paginationDisplay = ""; // Initialize the pagination output variable
// This code runs only if the last page variable is not equal to 1, if it is only 1 page we require no paginated links to display
if ($lastPage != "1"){
// This shows the user what page they are on, and the total number of pages
//$paginationDisplay .= 'Page <strong>' . $pn . '</strong> of ' . $lastPage. '        ';
// If we are not on page 1 we can place the Back button
//if ($pn != 1) {
//    $previous = $pn - 1;
//    $paginationDisplay .=  "   <a href=\"search_govern.php?pn=$previous\">" . "Back" . "</a>";
//} 

if ($pn != 1) {
    $previous = $pn - 1;
    $paginationDisplay .=  '   <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $previous . '"> Back</a> ';
}


// Lay in the clickable numbers display here between the Back and Next links
//$paginationDisplay .= '<span class="paginationNumbers">' . $centerPages . '</span>';
// If we are not on the very last page we can place the Next button
//if ($pn != $lastPage) {
 //   $nextPage = $pn + 1;
//    $paginationDisplay .=  "   <a href=\"search_govern.php?pn=$nextPage\">" . "Next" . "</a>";
//} 

   if ($pn != $lastPage) {
    $nextPage = $pn + 1;
    $paginationDisplay .=  '   <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $nextPage . '"> Next</a> ';
    } 
}
///////////////////////////////////// END Pagination Display Setup ///////////////////////////////////////////////////////////////////////////

//-count results
//$numrows=mysql_num_rows($sql2);
//echo "<div>"." "." ". $numrows . " results found for " . stripslashes($name) . "</div>"; 

//-display the result of the array
echo "<div align=\"center\">" . $paginationDisplay . "</div>";

//-create while loop and loop through result set
    while($row=mysql_fetch_array($sql2)){
        $userId=$row['userId'];
        $Attribute=$row['Attribute'];
        $Name=$row['Name'];


//-display the result of the array
echo "<p style=\"margin:2px 0px 5px 0px;\" >"; 
echo "<p>" ."<a href=\"search_govern_test.php?id=$userId\"> "  . $Name . "</a></p>";
echo "</p>";
}
}
else{
echo "<p>   Please enter a search query</p>";
}
}
}

?>
Member Avatar for diafol

Whne it comes to pagintion - there are about 2 new threads per week on it. Have you searched the forum? I'm a little confused - how are you passing info - form or link? If by link, no POST data will be sent. However forms can send via POST or GET.

yeah,,^^ look likes i need also that on my project..:}

Check this thread...it contains one easy pagination by tmato.pgn.
Click here

I am not get the data to be passed, i am using form to pass the data eg:

<form method="post" action="search_govern.php?go" id="searchform">
  <input type="text" name="name">
  <input type="submit" name="submit" value="go">
</form>

this code is at the start of mp php block, every thing is working, i can search my data base the info comes up but when time to click page 2, i go to page 2, but no data comes up, this is very frustrating!

Member Avatar for diafol

Shouldn't the action have a 'pn' parameter too?

<form method="post" action="search_govern.php?go=1&pn=<?php echo $pageno;?>" id="searchform">

Or something similar?

I would think so, but here's the thing the form is actually at the very top of the php blaock of codes, so putting

<form method="post" action="search_govern.php?go=1&pn=<?php echo $pageno;?>" id="searchform">

is causing an error Notice: Undefined variable: pageno in search_govern.php on line 73 Call Stack #TimeMemoryFunctionLocation 10.0012753432{main}( )..\search_govern.php:0 " id="searchform">

Member Avatar for diafol

I'd advise placing all the php processing at the top of the page, then if there's any html - place it below. You need php/html separation.

Ok thanks i'll try it and get back to you, i appreciate your help!

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.