0

The error:

Notice: Undefined index: image in /Applications/MAMP/htdocs/BEADERIE JEWELS/application/pages/necklaces/index.php on line 10

/>

The Query:

$query = "SELECT * FROM products";  
$data = $connection->query( $query);    
$data->setFetchMode( PDO::FETCH_ASSOC );
while ( $row = $data->fetchAll() ){
$products[] = $row; 
    }

The Html:

<?php foreach($products AS $product): ?>
<?php print_r($product); ?>
<div class="productbox">
<img src="site/Pictures/"<?php $product["image"]  ?>/>
<p><?php $product["price"]?></p>

print_r results: Array ( [0] => Array ( [id] => 1 [productname] => Bold Gold Necklace [description] => Gold chain Necklace [producttype] => necklace [image] => goldboldnecklace.jpg [stock] => 3 ) )

I tried adding isset() function but it didnt work. What am I doing wrong?

Mihai
  • 26,325
  • 7
  • 66
  • 81
user3053151
  • 137
  • 1
  • 14

2 Answers2

1

Instead of an array of products you're making an array containing an array of products.

Instead of fetchAll use fetch in your loop or simply assign $products=$data->fetchAll()

while ( $row = $data->fetch() ){
    $products[] = $row; 
}

or

$products = $data->fetchAll();
EWit
  • 1,954
  • 13
  • 22
  • 19
  • The fetch method works. Thanks. My image still doesnt show up though. – user3053151 Mar 22 '14 at 20:03
  • Not too sure about PHP anymore these days but shouldn't you use `echo $product["image"]` instead of only `$product["image"]`? Can you verify what output is actually generated? is `goldboldnecklace.jpg` mentioned in the generated HTML? – EWit Mar 22 '14 at 20:18
0

Put $product[0]["image"] instead $product["image"].

LevChurakov
  • 113
  • 10