我有一段时间让这个脚本工作。我有两个搜索字段。这就是不同查询的原因。我无法使用搜索脚本进行分页。分页alwasy带回所有记录,当我点击下一个按钮时我得到一个空白页面。 任何帮助将不胜感激。
这是表格
<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>
这是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">«</a> <a href="?page=' . ($page - 1) . '" title="Previous page">‹</a>' : '<span class="disabled">«</span> <span class="disabled">‹</span>';
// The "forward" link
$nextlink = ($page < $pages) ? '<a href="?page=' . ($page + 1) . '" title="Next page">›</a> <a href="?page=' . $pages . '" title="Last page">»</a>' : '<span class="disabled">›</span> <span class="disabled">»</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>';
}}
更新:我对分页进行了计数,以便对$ 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();
}
答案 0 :(得分:0)
点击任何分页链接后,你的field_oem_value和field_oem_pn_value参数就会丢失。您需要在链接的href中明确指定它们(或保存在会话中)。
做这样的事情:
$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">«</a> <a href="?' . http_build_query($params + ['page' => $page-1]) . '" title="Previous page">‹</a>' : '<span class="disabled">«</span> <span class="disabled">‹</span>';
// The "forward" link
$nextlink = ($page < $pages) ? '<a href="?' . http_build_query($params + ['page' => $page+1]) . '" title="Next page">›</a> <a href="?' . http_build_query($params + ['page' => $pages]) . '" title="Last page">»</a>' : '<span class="disabled">›</span> <span class="disabled">»</span>';