Foreach in php not working -


i getting error

undefined variable label

i don't know why. using foreach($labels $label), not sending data label. used var_dump($labels) , got whole array, problem doesn't seem $labels, why doesn't foreach $label work?

here code:

foreach($labels $label) { ?>   <tr>     <td>img src=<?= $label["image"] ?>  </td>     <td>50 cents</td>     <td>$<?= $label["quantity"] ?> </td>     <td>$<?= $label['quantity']*.5 ?></td>    </tr> <? }   

here var_dump:

array(11) { [0]=> array(2) { ["image"]=> string(14) "circles/l3.jpg"    ["quantity"]=> string(2) "72" } [1]=> array(2) { ["image"]=> string(14) "circles/l2.jpg" ["quantity"]=> string(2) "24" } [2]=> array(2) { ["image"]=> string(11) "tags/t4.jpg" ["quantity"]=> string(2) "72" } [3]=> array(2) { ["image"]=> string(14) "circles/l3.jpg" ["quantity"]=> string(2) "60" } [4]=> array(2) { ["image"]=> string(15) "circles/l11.jpg" ["quantity"]=> string(2) "72" } [5]=> array(2) { ["image"]=> string(12) "tags/t12.jpg" ["quantity"]=> string(2) "72" } [6]=> array(2) { ["image"]=> string(14) "circles/l3.jpg" ["quantity"]=> string(2) "36" } [7]=> array(2) { ["image"]=> string(14) "circles/l3.jpg" ["quantity"]=> string(2) "60" } [8]=> array(2) { ["image"]=> string(14) "circles/l3.jpg" ["quantity"]=> string(2) "36" } [9]=> array(2) { ["image"]=> string(14) "circles/l3.jpg" ["quantity"]=> string(2) "36" } [10]=> array(2) { ["image"]=> string(11) "tags/t3.jpg" ["quantity"]=> string(2) "60" } }   

here rest of code:

  $id="";   if(!empty($_get['id'])){  $id=$_get['id'];  }   $cs = "mysql:host=localhost;dbname=purimlabels"; $user = "seforim"; $password = '1234';  try {     $options = [pdo::attr_errmode => pdo::errmode_exception];     $db = new pdo($cs, $user, $password, $options);     $query='select  image,quantity  labels  customerid=? ,    submitted="no" ';     $statement = $db->prepare($query);     $statement->bindvalue(1,$id);     $statement->execute();     $labels = $statement->fetchall(pdo::fetch_assoc);     $statement->closecursor();      }catch(pdoexception $e) {     die("something went wrong " . $e->getmessage());    } 

may cannot use short tag <? on local environment. check short_open_tag = on value in local php.ini file.

you may use below code php tag, short open tags disabled because can cause issues reading xml, opt second option

<table> <?php foreach($labels $label): ?>   <tr>     <td><img src="<?php echo $label['image']; ?>" /></td>     <td>50 cents</td>     <td>$<?php echo $label['quantity']; ?> </td>     <td>$<?php echo $label['quantity'].'*.5'; ?></td>    </tr> <?php endforeach; ?> </table> 

Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -