Jump to content

Pagination problems with form output (long code post)


Thundarfoot

Recommended Posts

I am making a drop down list that gets its values from mysql table, and ouputs the user choice into a variable in a mysql query to display matching records ina  table.

 

The scrip works, except for the pagination which somehow loses the appropriate variable...allso having trouble figuring out how to make a default choice of All return all errors.

 

I will include the lengthy dreamweaver 8 code, I would have trimmed it down if I was sure what area was causing the trouble.

 

Thank you in advance for helping me learn.

 

<?php require_once('../Connections/Lw.php'); ?>
<?php
$currentPage = $_SERVER["PHP_SELF"];

mysql_select_db($database_Lw, $Lw);
$query_Class = "SELECT `Class` FROM class_list ORDER BY id ASC";
$Class = mysql_query($query_Class, $Lw) or die(mysql_error());
$row_Class = mysql_fetch_assoc($Class);
$totalRows_Class = mysql_num_rows($Class);

mysql_select_db($database_Lw, $Lw);
$query_Cards = "SELECT Name, `Class`, Drops, Comments, Pic FROM skill_cards ORDER BY Name ASC";
$Cards = mysql_query($query_Cards, $Lw) or die(mysql_error());
$row_Cards = mysql_fetch_assoc($Cards);
$totalRows_Cards = mysql_num_rows($Cards);

$maxRows_Cards = 10;
$pageNum_Cards = 0;
if (isset($_GET['pageNum_Cards'])) {
  $pageNum_Cards = $_GET['pageNum_Cards'];
}
$startRow_Cards = $pageNum_Cards * $maxRows_Cards;

mysql_select_db($database_Lw, $Lw);
$name = $_POST['select'];
$query_Cards = "SELECT Name, `Class`, Drops, Comments, Pic FROM skill_cards WHERE skill_cards.`Class` LIKE '$name%' ORDER BY Name ASC";

$query_limit_Cards = sprintf("%s LIMIT %d, %d", $query_Cards, $startRow_Cards, $maxRows_Cards);
$Cards = mysql_query($query_limit_Cards, $Lw) or die(mysql_error());
$row_Cards = mysql_fetch_assoc($Cards);

if (isset($_GET['totalRows_Cards'])) {
  $totalRows_Cards = $_GET['totalRows_Cards'];
} else {
  $all_Cards = mysql_query($query_Cards);
  $totalRows_Cards = mysql_num_rows($all_Cards);
}
$totalPages_Cards = ceil($totalRows_Cards/$maxRows_Cards)-1;

$queryString_Cards = "";
if (!empty($_SERVER['QUERY_STRING'])) {
  $params = explode("&", $_SERVER['QUERY_STRING']);
  $newParams = array();
  foreach ($params as $param) {
    if (stristr($param, "pageNum_Cards") == false && 
        stristr($param, "totalRows_Cards") == false) {
      array_push($newParams, $param);
    }
  }
  if (count($newParams) != 0) {
    $queryString_Cards = "&" . htmlentities(implode("&", $newParams));
  }
}
$queryString_Cards = sprintf("&totalRows_Cards=%d%s", $totalRows_Cards, $queryString_Cards);
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>
<form id="form1" name="form1" method="post" action="">
  <label>Class
  <select name="select">
    <option value="">All</option>
<?php
do {  
?>
    <option value="<?php echo $row_Class['Class']?>"><?php echo $row_Class['Class']?></option>
    <?php
} while ($row_Class = mysql_fetch_assoc($Class));
  $rows = mysql_num_rows($Class);
  if($rows > 0) {
      mysql_data_seek($Class, 0);
  $row_Class = mysql_fetch_assoc($Class);
  }
?>
  </select>
  </label>
  <label></label>
  <input type="submit" name="submit" value="submit">
</form>
<table border="1" cellpadding="5" cellspacing="5">
  <tr>
    <td>Name</td>
    <td>Class</td>
    <td>Drops</td>
    <td>Comments</td>
    <td>Pic</td>
  </tr>
  <?php do { ?>
    <tr>
      <td><?php echo $row_Cards['Name']; ?></td>
      <td><?php echo $row_Cards['Class']; ?></td>
      <td><?php echo $row_Cards['Drops']; ?></td>
      <td><?php echo $row_Cards['Comments']; ?></td>
      <td><?php echo $row_Cards['Pic']; ?></td>
    </tr>
    <?php } while ($row_Cards = mysql_fetch_assoc($Cards)); ?>
</table>
<br />
<table border="0" width="50%" align="Left">
  <tr>
    <td width="23%" align="center"><?php if ($pageNum_Cards > 0) { // Show if not first page ?>
          <a href="<?php printf("%s?pageNum_Cards=%d%s", $currentPage, 0, $queryString_Cards); ?>"><img src="First.gif" border=0></a>
          <?php } // Show if not first page ?>
    </td>
    <td width="31%" align="center"><?php if ($pageNum_Cards > 0) { // Show if not first page ?>
          <a href="<?php printf("%s?pageNum_Cards=%d%s", $currentPage, max(0, $pageNum_Cards - 1), $queryString_Cards); ?>"><img src="Previous.gif" border=0></a>
          <?php } // Show if not first page ?>
    </td>
    <td width="23%" align="center"><?php if ($pageNum_Cards < $totalPages_Cards) { // Show if not last page ?>
          <a href="<?php printf("%s?pageNum_Cards=%d%s", $currentPage, min($totalPages_Cards, $pageNum_Cards + 1), $queryString_Cards); ?>"><img src="Next.gif" border=0></a>
          <?php } // Show if not last page ?>
    </td>
    <td width="23%" align="center"><?php if ($pageNum_Cards < $totalPages_Cards) { // Show if not last page ?>
          <a href="<?php printf("%s?pageNum_Cards=%d%s", $currentPage, $totalPages_Cards, $queryString_Cards); ?>"><img src="Last.gif" border=0></a>
          <?php } // Show if not last page ?>
    </td>
  </tr>
</table>
</p>
</body>
</html>
<?php
mysql_free_result($Class);

mysql_free_result($Cards);
?>

Link to comment
Share on other sites

Ok I think part of my trouble is that the drop down form resets to ALL after teh submit button is pressed, causing my variable to change...

 

Not sure what to do about it, right now I have changed to $maxRows_Cards = 999; which removes the need for pages, but its not the fix I would like.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.