Last active
September 27, 2016 13:31
-
-
Save brucenorton/933e49d19016c27d722040fbcfa14a44 to your computer and use it in GitHub Desktop.
Filter by category (for Jason)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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)); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
See the demo online
http://www.micromediaweb.com/home/nortonb/hypertext/store/select_category.php?category=personal
http://www.micromediaweb.com/home/nortonb/hypertext/store/select_category.php?category=tech
http://www.micromediaweb.com/home/nortonb/hypertext/store/select_category.php
(blank category selects all)