Skip to content

Instantly share code, notes, and snippets.

@brucenorton
Last active September 27, 2016 13:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brucenorton/933e49d19016c27d722040fbcfa14a44 to your computer and use it in GitHub Desktop.
Save brucenorton/933e49d19016c27d722040fbcfa14a44 to your computer and use it in GitHub Desktop.
Filter by category (for Jason)
<?php
require_once '_includes/connect.php';
//added error reporting for debugging
error_reporting(E_ALL);
ini_set('display_errors', 'On');
//define table
$tbl = "items";//change to your table i.e. John_app
$tbl2 = "photos";
$tblCategories = "categories";
if (empty($_REQUEST["category"])) {
$category = '%';
}else{
$category = $_REQUEST["category"]; //tech, personal in my case
}
//write query
//$query = "SELECT $tbl.itemID, $tbl.title, $tbl.description, $tbl.price, $tbl2.src FROM $tbl INNER JOIN $tbl2 ON $tbl.itemID = $tbl2.itemID";
$query = "SELECT $tbl.itemID, $tbl.title, $tbl.description, $tbl.price, $tbl2.src, $tblCategories.category
FROM $tbl
INNER JOIN $tblCategories ON $tbl.itemID = $tblCategories.itemID
INNER JOIN $tbl2 ON $tbl.itemID = $tbl2.itemID
AND $tblCategories.category LIKE ?
GROUP BY $tbl.itemID";
//prepare statement, execute, store_result
if($displayStmt = $mysqli->prepare($query)){
$displayStmt->bind_param('s', $category);//added this!
$displayStmt->execute();
$displayStmt->store_result();
$numrows = $displayStmt-> num_rows;
//echo("<br>results: $numrows");
}
//bind results
$displayStmt->bind_result($IDResult, $titleResult, $descriptionResult, $priceResult, $srcResult, $categoryResult);
$itemsArray = [];
//fetch results
while($displayStmt->fetch()){
$itemsArray[] = [$IDResult, $titleResult, $descriptionResult, $priceResult, $srcResult, $categoryResult];
}
echo(json_encode($itemsArray));
?>