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">«</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>';
}}
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();
}