Get function

Hi guys,
I got this search engine that’s supposed to go into a database according to a country the client selects and output data pertaining to that specific country with pagination.
The data shows up if the submit button is clicked, but the pagination stops functioning since the “$country_form” is never reset and the mysql query doesn’t know where to pull the data from.

How can I tell the code to use the previously set “$country_form” (the one set after submit button was clicked) in order for the the pagination to work?

Please any help appreciated.

<?php
include ('connectionhost.php');


  if($_POST['submit'])
  {
   $country_form = $_POST['inputcountry'];
  }
  else
  {

   $country_form =
  }
//grab POST data

//max displayed per page
$per_page = 2;

//get start variable: this determines where we're starting from when extracting data from database
$start = $_GET['start'];

//count records: this will count how many rows are returned
$record_count = mysql_num_rows(mysql_query("SELECT * FROM mainweb WHERE country='$country_form'"));

//count max pages
$max_pages = $record_count / $per_page; //may come out as decimal

if(!$start)
$start = 0;

//display data           //add between mainweb and LIMIT: WHERE country='$country_form'
$get = mysql_query("SELECT * FROM mainweb WHERE country='$country_form' LIMIT $start, $per_page");
while ($row = mysql_fetch_assoc($get))
{
  //get data
  $firstname = $row['firstname'];
  $lastname = $row['lastname'];
  
  echo $firstname."(".$lastname.")<br />";

}

//setup prev and next variables
$prev = $start - $per_page;
$next = $start + $per_page;

//show prev button
if(!($start<=0))
    echo"<a href='pagination.php?start=$prev'>Prev</a> ";

//show page numbers: $x< is the condition. $x=$x is the increment

//set variable for first page
$i=1;

for ($x=0;$x<$record_count;$x=$x+$per_page)
{
   if($start!=$x)
      echo "<a href='pagination.php?start=$x'>$i</a> ";
   else
      echo "<a href='pagination.php?start=$x'><b>$i</b></a> ";
   //++ means to add 1 to a value
   $i++;
}

//show next button
if(!($start>=$record_count-$per_page))
    echo"<a href='pagination.php?start=$next'>Next</a>" ;

?>

<form action="pagination.php" method="post">
<div style="text-align: center;">Country:
<select name="inputcountry"><option><?php echo $country_form;?></option><option>---</option><option>Brazil</option><option>Canada</option><option>Chile</option><option>Ecuador</option><option>England</option><option>France</option><option>Israel</option><option>Mexico</option><option>South Africa</option><option>USA</option></select>
<br />
<p><input name="submit" value="Search" type="submit" /> </p>
</div>
</form>

In your else statement use

$country_form = isset($_GET['country']) ? $_GET['country'] : false;

then for your pagination you can use

//show prev button
if(!($start<=0))
    echo"<a href='pagination.php?start=$prev&amp;country=$country_from'>Prev</a> ";

//show page numbers: $x< is the condition. $x=$x is the increment

//set variable for first page
$i=1;

for ($x=0;$x<$record_count;$x=$x+$per_page)
{
    if($start!=$x)
        echo "<a href='pagination.php?start=$x&amp;country=$country_from'>$i</a> ";
    else
        echo "<a href='pagination.php?start=$x&amp;country=$country_from'><b>$i</b></a> ";
    //++ means to add 1 to a value
    $i++;
}

//show next button
if(!($start>=$record_count-$per_page))
    echo"<a href='pagination.php?start=$next&amp;country=$country_from'>Next</a>" ;

On a side note you should use some input filtering and in the select box use the value attribute for the options.

I tries doing what you said, still not working. It presents the data after submit button is pressed but the pagination still doesn’t work.

It shows this in the URL after I click submit and then try to use the pagination:

pagination.php?start=0&country=

as if doesn’t recognize the country.

My fault, simply change $country_from to $country_form in the anchor links.

SgtLegend, first of all I want to thank you for your help. I can’t believe I missed that misspelling. I should have seen it.

It seems to be working fine but there is one last problem.
The “next” and “number” buttons work, but no matter which page I’m on, the “previous” button doesn’t work. If I click on it, the data is gone and I’m left with my form only.

Do you have any idea why?

I was recently helped by someone to get my pagination to work but could not learn from my mistakes since I didn’t understand something he/she added to my script.
Can anyone please tell me (as if you were writing to a 6-year old) what this means: isset($_GET[‘country’]) ? $_GET[‘country’] : false;

Sorry to have bothered you SgtLegend. I fixed it. I just didn’t change all the misspelling errors.
Thank you for your help, you saved me hours of work.