dongxuxian1123
dongxuxian1123
2015-06-17 21:09

PDO MySQL的分页搜索多个表单字段

已采纳

I am having a heck of a time getting this script to work. I have two search fields. That is the reason for the different queries. I can't get the pagination to work with the search script. The pagination alwasy brings back all the records and I get a blank page when clicking the next button. Any help would be greatly appreciated.

Here is the FORM

<form action="" accept-charset="UTF-8" method="get"> 
<div style="float: left; margin-right: 50px;">
 <label for="edit-field-oem-value">OEM</label>  <br />                      
 <input type="text" maxlength="128" name="field_oem_value" size="30" value="" class="form-text" />
</div>
<div style="float: left;">
 <label for="edit-field-oem-pn-value">OEM P/N</label>  <br />                       
 <input type="text" maxlength="128" name="field_oem_pn_value" size="30" value="" class="form-text" /> 
 <input type="submit" id="edit-submit-Products-view" value="Search" class="form-submit" />
 </div>
</form>

Here is the PHP

if(isset($_GET['field_oem_value'], $_GET['field_oem_pn_value'])) {
try { 

$host = "localhost";
$user = "root";
$password = "";
$database_name = "database";
$dbh = new PDO("mysql:host=$host;dbname=$database_name", $user, $password, array(
//PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
));

$conditions = array();
$oemSearch = $_GET['field_oem_value'];
$oempnSearch = $_GET['field_oem_pn_value'];
$min = 3;
$oemLen = strlen($oemSearch);
$oempnLen = strlen($oempnSearch);

if ($oemSearch == "" && $oempnSearch == "") {
 echo "You must enter a OEM or Part Number";
 exit;
}
 if ($oemSearch != "" && $oemLen < $min) { 
 echo "You must enter at least 3 OEM characters.";
 exit;
}
 if ($oempnSearch != "" && $oempnLen < $min) { 
 echo "You must enter at least 3 P/N characters.";
 exit;
}
if ($oemSearch != "" && $oempnSearch == "") {
$stmt = $dbh->prepare("select * from parts_search WHERE oem LIKE '%$oemSearch%' ORDER BY oem LIMIT :limit OFFSET :offset");
//$query->bindValue(1, "%$oemSearch%", PDO::PARAM_STR);
}
if ($oemSearch == "" && $oempnSearch != "") {
$stmt = $dbh->prepare("select * from parts_search WHERE oem_pn LIKE '%$oempnSearch%' ORDER BY oem LIMIT :limit OFFSET :offset");
//$query->bindValue(1, "%$oemSearch%", PDO::PARAM_STR);
}
if ($oemSearch != "" && $oempnSearch != "") {
$stmt = $dbh->prepare("select * from parts_search WHERE oem LIKE '%$oemSearch%' OR oem_pn LIKE '%$oempnSearch%' ORDER BY oem LIMIT :limit OFFSET :offset");
//$query->bindValue(1, "%$oemSearch%", PDO::PARAM_STR);
}

// Find out how many items are in the table
$total = $dbh->query("SELECT COUNT(*) AS num FROM parts_search ")->fetchColumn();

// How many items to list per page
$limit = 20;

// How many pages will there be
$pages = ceil($total / $limit);

// What page are we currently on?
$page = min($pages, filter_input(INPUT_GET, 'page', FILTER_VALIDATE_INT, array(
    'options' => array(
    'default'   => 1,
    'min_range' => 1,
    ),
)));

// Calculate the offset for the query
$offset = ($page - 1) * $limit;

// Some information to display to the user
$start = $offset + 1;
$end = min(($offset + $limit), $total);

// Bind the query params
$stmt->bindParam(':limit', $limit, PDO::PARAM_INT);
$stmt->bindParam(':offset', $offset, PDO::PARAM_INT);
$stmt->execute();

// Do we have any results?
if ($stmt->rowCount() > 0) {
    // Define how we want to fetch the results
    $stmt->setFetchMode(PDO::FETCH_ASSOC);
    $iterator = new IteratorIterator($stmt);

    // Display the results
      echo "<table class='views-table'>";   
      echo "<tr class='tblheader'><td>OEM</td><td>OEM P/N</td><td>Description</td></tr>";
    foreach ($iterator as $row) {
        echo "<tr class=odd views-row-first1><td>";         
        echo $row['oem'];
        echo "</td><td>";
        echo $row['oem_pn'];
        echo "</td><td>";
        echo $row['desc'];
        echo "</td></tr>";
    }
     echo "</table>"; 

     // The "back" link
      $prevlink = ($page > 1) ? '<a href="?page=1" title="First page">&laquo;</a> <a href="?page=' . ($page - 1) . '" title="Previous page">&lsaquo;</a>' : '<span class="disabled">&laquo;</span> <span class="disabled">&lsaquo;</span>';

      // The "forward" link
      $nextlink = ($page < $pages) ? '<a href="?page=' . ($page + 1) . '" title="Next page">&rsaquo;</a> <a href="?page=' . $pages . '" title="Last page">&raquo;</a>' : '<span class="disabled">&rsaquo;</span> <span class="disabled">&raquo;</span>';

      // Display the paging information
      echo '<div id="paging"><p>', $prevlink, ' Page ', $page, ' of ', $pages, ' pages, displaying ', $start, '-', $end, ' of ', $total, ' results ', $nextlink, ' </p></div>';   

} else {
    echo '<p>No results could be displayed.</p>';
}

} catch (Exception $e) {
    echo '<p>', $e->getMessage(), '</p>';
}}

UPDATE: I got the count on the pagination to work with the following change to the $total var.

   if ($oemSearch != "" && $oempnSearch == "") {
      $total = $dbh->query("select COUNT(*) from parts_search WHERE oem LIKE '%$oemSearch%'")->fetchColumn();
  }
  if ($oemSearch == "" && $oempnSearch != "") {
  $total = $dbh->query("select COUNT(*) from parts_search WHERE oem_pn LIKE '%$oempnSearch%'")->fetchColumn();
  }
  if ($oemSearch != "" && $oempnSearch != "") {
  $total = $dbh->query("select COUNT(*) from parts_search WHERE oem LIKE '%$oemSearch%' OR oem_pn LIKE '%$oempnSearch%'")->fetchColumn();
  }
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • doukeng1922 doukeng1922 6年前

    Your field_oem_value and field_oem_pn_value params are getting lost after clicking on any of the pagination links. You need to specify them explicitly in link's href (or save in session).

    Do something like this:

      $params = [
          'field_oem_value' => $oemSearch,
          'field_oem_pn_value' => $oempnSearch
      ];
    
      // The "back" link
      $prevlink = ($page > 1) ? '<a href="?'. http_build_query($params + ['page' => 1]) .'" title="First page">&laquo;</a> <a href="?' . http_build_query($params + ['page' => $page-1]) . '" title="Previous page">&lsaquo;</a>' : '<span class="disabled">&laquo;</span> <span class="disabled">&lsaquo;</span>';
    
      // The "forward" link
      $nextlink = ($page < $pages) ? '<a href="?' . http_build_query($params + ['page' => $page+1]) . '" title="Next page">&rsaquo;</a> <a href="?' . http_build_query($params + ['page' => $pages]) . '" title="Last page">&raquo;</a>' : '<span class="disabled">&rsaquo;</span> <span class="disabled">&raquo;</span>';
    
    点赞 评论 复制链接分享

相关推荐