0

I am at a loss here. I cannot for the life of me figure out why I am getting the error.

Notice: Undefined index: price in /home/*****/public_html/newfinal/home_view.php on line 14

Notice: Undefined index: description in /home/****/public_html/newfinal/home_view.php on line 15

Here are the relevant code snippets:

PHP/ SQL

function get_product($product_id) {
    global $db;
    $query = "SELECT * FROM prjProducts p"
            . " INNER JOIN prjCategories c"
            . " ON p.categoryID = c.categoryID"
            . " WHERE productID = :product_id";
    try {
        $stmt = $db->prepare($query);
        $stmt->bindValue(':product_id', $product_id);
        $stmt->setFetchMode(PDO::FETCH_ASSOC);
        $stmt->execute();
        $result = $stmt->fetchAll();
        $stmt->closeCursor();
        return $result;
    } catch (PDOException $ex) {
        $error = $ex->getMessage();
        echo $error;
    }
}

PHP

// Set the featured product Id's in an array
$product_ids = array(3, 5, 10);

// Get an array of featured producst from the database
$products = array();
foreach ($product_ids as $product_id) {
    $product = get_product($product_id);
    $products[] = $product; // add the product to the array

}

HTML

<?php foreach ($products as $product) :
          // Get product data
          $price = $product['price'];
          $description = $product['description'];

I have been banging my head at this one for a couple of hours now. I've tried to echo the arrays, and I can print out the arrays without issue. I'm sure it is some small formatting error that I am missing somewhere.

acm
  • 2,086
  • 3
  • 16
  • 34
Harlan
  • 133
  • 1
  • 3
  • 15

1 Answers1

0
  1. First check whether you have the price and description columns in your table prjProducts.

  2. In your foreach ($products as $product) loop, do a var_dump for $product and check to see whether it is an array or an object?

If it is not an array you should do $product->price instead of $product['price'].

masterFly
  • 1,072
  • 12
  • 24
  • Here is the `var_dump`: `array(1) { [0]=> array(7) { ["productID"]=> string(1) "3" ["categoryID"]=> string(1) "4" ["productName"]=> string(44) "Gerber Swagger Assisted Opening Pocket Knife" ["description"]=> string(125) "The Gerber Swagger assisted opening pocket knife offers a dual G-10 and Stainless steel handle for great grip and durability." ["price"]=> string(5) "39.99" ["imgFile"]=> string(16) "gerberpocket.jpg" ["categoryName"]=> string(13) "Pocket Knives" } } ` – Harlan Mar 23 '17 at 11:39
  • This is the var dump of products right? Not product? – masterFly Mar 23 '17 at 15:27
  • That is a var_dump of $product, inside the foreach loop. – Harlan Mar 23 '17 at 17:14
  • I think I figured out the problem, though not the solution. I ran a `print_r($products)` directly after the first foreach function. It looks as if it is a three level array instead of 2. And the second level does not have an index number? Why would this be and how do I fix it? – Harlan Mar 23 '17 at 17:47
  • 1
    So if this is the dump of single $product, $product['price'] is nit there. Because the details are inside another array. So it shoul be $product[0][price] – masterFly Mar 23 '17 at 17:48
  • That as it. Thanks. – Harlan Mar 23 '17 at 20:03
  • Happy that we figured this out before your life ends :P – masterFly Mar 24 '17 at 03:38