I'm working on a small catalogue which will take a list of products in a SQL database and list them on a catalogue.php page. When you click on a product, you'll be taken to a page with that specific product. So I have a productbuild.php which is the class that builds both the the catalogue and the individual product. The individual product is located at productview.php. I can view the products on the catalogue page but not on the productview page.
Here is my class:
require_once('connect.php');
class Catalogue {
public $ksdb = '';
public function __construct() {
$this->ksdb = new Database;
}
//This function gets all the products on the main catalogue page, it works fine
public function getProds() {
$id = 0;
//This is the prod array that is not being passed
$prod = $return = array();
$querystr = $this->ksdb->db_connect->prepare("SELECT * FROM products");
try {
$querystr->execute();
for($i=0; $row = $querystr->fetch(); $i++) {
$return[$i] = array();
foreach($row as $rowi => $rowitem) {
$return[$i][$rowi] = $rowitem;
}
}
} catch (PDOException $e) {
echo $e->getMessage();
}
return $return;
//$prod = $return;
}
//This is the one that is not working on the productview.php page
public function viewProd($prodID) {
$prod = $return = array();
$querystr = $this->ksdb->db_connect->prepare("SELECT * FROM products WHERE ID= ?");
try {
$querystr->execute(array($id));
for ($i = 0; $row = $querystr->fetch(); $i++) {
$return[$i] = array();
foreach($row as $rowi => $rowitem) {
$return[$i][$rowi] = $rowitem;
}
}
} catch(PDOException $e) {
echo $e->getMessage();
}
$prod = $return;
$prod[0]['Description'] = $prod[0]['Description'];
}
}
And this is my productview.php page:
//Include the class
<?php include_once('/productbuild.php');
//Create an object of catalogue
$viewproduct = new Catalogue;
//if there is something in productview.php?ID= viewProd function is called
if(!empty($_GET['ID'])) {
$prod = $viewproduct->viewProd($_GET['ID']);
}
else {
echo "No Products";
}
?>
//Echo the product name (from the DB) and Description
<h3><?php echo htmlspecialchars($prod['Name']); ?></h3>
<?php echo htmlspecialchars($prod['Description']); ?>
答案 0 :(得分:0)
How about changing your viewProd function from
$querystr->execute(array($id));
to
$querystr->execute(array($prodID))
Let me know if this solves your problem.