简体   繁体   English

使用PDO MySQL进行分页搜索多个表单字段

[英]Pagination with PDO MySQL Search Multiple Form Fields

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 这是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. 更新:我对分页计数很满意,可以对$ 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();
  }

Your field_oem_value and field_oem_pn_value params are getting lost after clicking on any of the pagination links. 单击任何分页链接后,您的field_oem_value和field_oem_pn_value参数将丢失。 You need to specify them explicitly in link's href (or save in session). 您需要在链接的href中明确指定它们(或保存在会话中)。

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>';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM