表PHP中的嵌套循环 [英] Nested Loop in table PHP

查看:129
本文介绍了表PHP中的嵌套循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是PHP新手,我想用两个foreach来创建一个表,而我没有得到我想要的输出。

 < HTML> 
< head>
< title> Didier Test< / title>
< / head>
< body>
< h1> Yesso!< / h1>
< / body>

< table border =1>
< tr>
< th>书籍< / th>
Price< th>
< / tr>
< tr>

<?php foreach($ name as $ item):?>
< td><?= $ item?>< / td>
<?php foreach($ price as $ item2):?>
< td><?= $ item2?>< / td>
< / tr>
<?php endforeach; ?>
<?php endforeach; ?>
< / table>

< / body>
< / html>

我知道我有一个与我内心的问题,但我不知道如何纠正



请让我知道。

解决方案

速记php输出符号<?= 完整<?php echo

 < html> 
< head>
< title> Didier Test< / title>
< / head>
< body>
< h1> Yesso!< / h1>
< / body>
< table border =1>
< tr>
< th>书籍< / th>
Price< th>
< / tr>
< tr>
<?php foreach($ name as $ item):?>
< td><?php echo $ item?>< / td>
<?php foreach($ price as $ item2):?>
< td><?php echo $ item2?>< / td>
< / tr>
<?php endforeach; ?>
<?php endforeach; ?>
< / table>
< / body>
< / html>

你可能没有得到任何输出,因为你的php.ini设置为不允许速记php'

哦,是的,就像Barmer和Revent所提到的,你也有一些HTML标签嵌套问题。欢迎来到PHP&祝你好运:)

I am new to PHP and I am trying to make a table using two foreach and I am not getting the output I want.

<html>
    <head>
         <title>Didier Test</title>
    </head>
    <body>
        <h1>Yesso!</h1>
    </body>

    <table border="1">
        <tr>
            <th>Books</th>
            <th>Price</th>
        </tr>
    <tr>

    <?php foreach($name as $item): ?>
        <td><?=$item?></td>
        <?php foreach($price as $item2): ?>
            <td><?=$item2?></td>
            </tr>
        <?php endforeach; ?>
    <?php endforeach; ?>
    </table>

    </body>
</html>

I know I am having an issue with my inner foreach, but I don't know how to correct it.

Please let me know.

解决方案

Try changing your shorthand php output notation <?= with full <?php echo

<html>
<head>
  <title>Didier Test</title>
</head>
<body>
<h1>Yesso!</h1>
</body>
<table border="1">
<tr>
    <th>Books</th>
    <th>Price</th>
</tr>
<tr>
<?php foreach($name as $item): ?>
    <td><?php echo $item?></td>
    <?php foreach($price as $item2): ?>
    <td><?php echo $item2?></td>
</tr>
     <?php endforeach; ?>
<?php endforeach; ?>
</table>
</body>
</html>

You probably dont get any output because your php.ini has set not to allow shorthand php '

Oh, and yes, like mentioned by Barmer and Revent, you have some HTML tag-nesting issues as well. Welcome to the wonderful world of PHP & Good luck :)

这篇关于表PHP中的嵌套循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆