【问题标题】:Foreach in php not workingphp中的foreach不起作用
【发布时间】:2017-08-21 01:33:28
【问题描述】:

我收到一个错误

未定义的变量标签

我不知道为什么。我正在使用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());
   }

【问题讨论】:

  • 你提到var_dump($labels)怎么样?
  • 请提供完整的错误信息和重现错误的代码。
  • 我同意上面的海报,没有看到你的其他代码很难判断......
  • 猜测,您的错误来自代码的不同部分。您问题中的代码不可能产生该错误消息。完整的错误消息将显示问题的确切位置
  • @Phil 如果未启用 PHP 短标签,则给出的代码可能会产生错误。除此之外,我倾向于同意

标签: php foreach


【解决方案1】:

您可能无法在本地环境中使用短标签&lt;?。检查本地php.ini 文件中的short_open_tag = On 值。

您可以在下面的代码中使用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>

【讨论】:

  • 非常感谢!有用!我只是有一个小问题:视图正在显示?> 从行“”你如何解决这个问题?
【解决方案2】:

尝试将您的代码修改为:

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 格式。

【讨论】:

  • 嗯,很遗憾听到这个消息。测试时,它在我的机器上有所不同。我会更新另一个选项来尝试。
猜你喜欢
  • 2018-08-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-20
  • 1970-01-01
  • 1970-01-01
  • 2017-06-16
  • 1970-01-01
相关资源
最近更新 更多