在PHP中的Foreach无法正常工作

时间:2017-08-21 01:33:29

标签: php foreach

我收到错误

  

未定义的变量标签

我不知道为什么。我使用的是foreach($labels as $label),但它没有将数据发送到标签中。我使用了var_dump($labels)并且我得到了一个完整的数组,所以问题似乎不是$labels,为什么foreach$label不起作用?

这是我的代码:

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

这是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" } }  

这是代码的其余部分:

  $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  FROM labels WHERE  CustomerID=? AND    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());
   }

2 个答案:

答案 0 :(得分:0)

尝试将代码修改为:

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

<强>更新

上面的工作在我的机器上,但由于这没有帮助,尝试将所有内容留在PHP中,看看你是否仍然得到错误。以下是您的代码的修改版本:

<?php
    // define $labels as first two elements of your var_dump.
    // using older syntax
    $labels = array(array(
                    "image"=> "circles/L3.jpg",
                    "quantity"=> "72"
                   ),
                array(
                    "image"=> "circles/L2.jpg",
                    "quantity"=> "24"
                  ));

    echo "<html>\n\t<body>\n\t<table>\n";
    foreach($labels as $label) {
       echo "\t\t<tr>".
            "<td>img src=".$label["image"]."</td>".
            "<td>50 cents</td>".
            "<td>$".$label["quantity"]."</td>".
            "<td>$".($label["quantity"]*.5)."</td>".
            "</tr>\n";
    }
    echo "\t</table>\n\t</body>\n</html>";
?>

这为我输出以下内容:

<html>
    <body>
    <table>
       <tr><td>img src=circles/L3.jpg</td><td>50 cents</td><td>$72</td><td>$36</td></tr>
       <tr><td>img src=circles/L2.jpg</td><td>50 cents</td><td>$24</td><td>$12</td></tr>
    </table>
    </body>
</html>

希望这有效;我会留给你根据需要修改HTML格式。

答案 1 :(得分:0)

您可以在本地环境中使用短标记<?。检查本地short_open_tag = On文件中的php.ini值。

您可以在代码下方使用php标记,通常会禁用短打开标记,因为它们会导致读取xml的问题,所以我个人会选择第二个选项

<table>
<?php foreach($labels as $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>