ConfusedCoder
ConfusedCoder

Reputation: 1

MySQL PHP Pagination - My search works but paginations does not

I searched a read about 75 post but none seem to work for me.

I have a search form in my search-form.php to allow a user to search for a particular user, the results are displayed in search.php. The from works and the results are displayed with no issues except I have a limit of "10" set and 10 are displayed on the initial page but when clicking the pagination button for next page it does not bring up the remaining results.

Any help in pointing me in the right direction would be greatly appreciated. This is not a live server and I am using it to learn both php and mysql so I am new to anything but your general php at this point.

search-form.php code:

<form action="getdata.php" method="get">
  <label>Name:
    <input type="text" name="keyname" />
  </label>
  <input type="submit" value="Search" />
</form>

getdata.php code:

<?php
$searchTerm = trim($_GET['keyname']);

if($searchTerm == "")
{
  echo "Enter name you are searching for.";
  exit();
}

$dbhost = 'localhost';
$dbuser = 'xxxxx';
$dbpass = 'xxxxx';
$rec_limit = 10;

$conn = mysql_connect($dbhost, $dbuser, $dbpass);

if(! $conn )
{
  die('Could not connect: ' . mysql_error());
}

mysql_select_db('xxxxx_xxxxx');

$sql = "SELECT count(note_id) FROM notes ";
$retval = mysql_query( $sql, $conn );

if(! $retval )
{
  die('Could not get data: ' . mysql_error());
}

$row = mysql_fetch_array($retval, MYSQL_NUM );
$rec_count = $row[0];

if( isset($_GET{'page'} ) )
{
  $page = $_GET{'page'} + 1;
  $offset = $rec_limit * $page ;
}
else
{
  $page = 0;
  $offset = 0;
}

$left_rec = $rec_count - ($page * $rec_limit);

$sql = ("SELECT * FROM notes WHERE emp_code LIKE '%$searchTerm%' LIMIT $offset, $rec_limit");
$retval = mysql_query( $sql, $conn );

if(! $retval )
{
  die('Could not get data: ' . mysql_error());
}

while($row = mysql_fetch_array($retval, MYSQL_ASSOC))
{
  echo "<ul data-role='listview' data-inset='true'>" .
    "<li data-role='list-divider'>Friday, October 8, 2010 <span class='ui-li-count'>Note ID:&nbsp;{$row['note_id']}</span></li>" .
    "<li><a href='#'>" .
    "<p>Employee Code:&nbsp;{$row['emp_code']}</p>" .
    "<p>Note:&nbsp;{$row['emp_note']}</p>" .
    "</a></li></ul>"; 
  }

  if( $page > 0 )
  {
    $last = $page - 2;
    echo "<a href=\"" . $_SERVER['PHP_SELF'] . "?page=$last\">Last 10 Records</a> |";
    echo "<a href=\"" . $_SERVER['PHP_SELF'] . "?page=$page\">Next 10 Records</a>";
  }
  else if( $page == 0 )
  {
    echo "<a href=\"" . $_SERVER['PHP_SELF'] . "?page=$page\">Next 10 Records</a>";
  }
  else if( $left_rec < $rec_limit )
  {
    $last = $page - 2;
    echo "<a href=\"" . $_SERVER['PHP_SELF'] . "?page=$last\">Last 10 Records</a>";
  }

  mysql_close($conn);
  ?>

Upvotes: 0

Views: 168

Answers (1)

d.abyss
d.abyss

Reputation: 212

You need to use square brackets for your GET, not curly brackets. if( isset($_GET{'page'} ) ) this is incorrect syntax, and wont work.

if( isset($_GET['page'] ) )
{
$page = $_GET['page'] + 1;

Upvotes: 1

Related Questions