Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
每行HTML表2中的PHP foreach循环_Php_Loops - Fatal编程技术网

每行HTML表2中的PHP foreach循环

每行HTML表2中的PHP foreach循环,php,loops,Php,Loops,我有以下PHP循环 foreach ($users as $key => $name){ echo $name; } 它显示了相当多的名字。我想在一个表列td中显示它,但在每2行之后,我想开始一个新行。所以我想要的表格是这样的: <table width="100%" border="0"> <tr> <td width="50%" align="center">$name</td> <td width="50%" align

我有以下PHP循环

foreach ($users as $key => $name){
    echo $name;
}
它显示了相当多的名字。我想在一个表列td中显示它,但在每2行之后,我想开始一个新行。所以我想要的表格是这样的:

<table width="100%" border="0">
<tr>
<td width="50%" align="center">$name</td>
<td width="50%" align="center">$name</td>
</tr>
<tr>
<td width="50%" align="center">$name</td>
<td width="50%" align="center">$name</td>
</tr>
</table>
<table width="100%" border="0">
    <?php $i = 0; ?>
    <?php foreach($users as $key => $name) ?>
        <?php if($i > 2) { $i = 0; } ?>
        <?php if($i == 2 || $key == 0) { ?>
            <tr>
        <?php } ?>
            <td width="50%" align="center">$name</td>
            <td width="50%" align="center">$name</td>
        <?php if($i == 2 || $key == 0) { ?>
            </tr>
        <?php } ?>
        <?php $i++; ?>
    <?php } ?>
</table>

$name
$name
$name
$name
因此,在循环中每隔2行创建一个新表行

有什么办法可以做到这一点吗


谢谢。

您可以这样做:

<table width="100%" border="0">
<tr>
<td width="50%" align="center">$name</td>
<td width="50%" align="center">$name</td>
</tr>
<tr>
<td width="50%" align="center">$name</td>
<td width="50%" align="center">$name</td>
</tr>
</table>
<table width="100%" border="0">
    <?php $i = 0; ?>
    <?php foreach($users as $key => $name) ?>
        <?php if($i > 2) { $i = 0; } ?>
        <?php if($i == 2 || $key == 0) { ?>
            <tr>
        <?php } ?>
            <td width="50%" align="center">$name</td>
            <td width="50%" align="center">$name</td>
        <?php if($i == 2 || $key == 0) { ?>
            </tr>
        <?php } ?>
        <?php $i++; ?>
    <?php } ?>
</table>

$name
$name

希望这有帮助

您可以这样做:

<table width="100%" border="0">
<tr>
<td width="50%" align="center">$name</td>
<td width="50%" align="center">$name</td>
</tr>
<tr>
<td width="50%" align="center">$name</td>
<td width="50%" align="center">$name</td>
</tr>
</table>
<table width="100%" border="0">
    <?php $i = 0; ?>
    <?php foreach($users as $key => $name) ?>
        <?php if($i > 2) { $i = 0; } ?>
        <?php if($i == 2 || $key == 0) { ?>
            <tr>
        <?php } ?>
            <td width="50%" align="center">$name</td>
            <td width="50%" align="center">$name</td>
        <?php if($i == 2 || $key == 0) { ?>
            </tr>
        <?php } ?>
        <?php $i++; ?>
    <?php } ?>
</table>
echo '<table><tr>';
$i=0;
foreach ($users as $key => $name){
    if($i%2==0 && $i!=0)
    {
     echo '</tr><tr><td width="50%" align="center">'.$name.'</td>';
    }
   else
    echo '<td width="50%" align="center">'.$name.'</td>';
  $i++;
}
echo '</tr></table>';

$name
$name
希望这有帮助

echo';
echo '<table><tr>';
$i=0;
foreach ($users as $key => $name){
    if($i%2==0 && $i!=0)
    {
     echo '</tr><tr><td width="50%" align="center">'.$name.'</td>';
    }
   else
    echo '<td width="50%" align="center">'.$name.'</td>';
  $i++;
}
echo '</tr></table>';
$i=0; foreach($key=>$name的用户){ 如果($i%2==0&&$i!=0) { 回显“.$name.”; } 其他的 回显“.$name.”; $i++; } 回声';
试试这个

echo';
$i=0;
foreach($key=>$name的用户){
如果($i%2==0&&$i!=0)
{
回显“.$name.”;
}
其他的
回显“.$name.”;
$i++;
}
回声';
试试这个

试试这个

$count = count($users);
$c = 0;
foreach ($users as $key => $name){ ?>

    <?php if ($c%2 === 0) {?>
        <table width="100%" border="0">
    <?php } ?>

        <tr>
            <td width="50%" align="center">$name</td>
            <td width="50%" align="center">$name</td>
        </tr>
        <tr>
            <td width="50%" align="center">$name</td>
            <td width="50%" align="center">$name</td>
        </tr>

    <?php if ($c%2 === 1 || $count == ($c+1)) {?>
        </table>
    <?php } ?>

<?php $c++; } ?>
$count=count($users);
$c=0;
foreach($key=>$name的用户){?>
$name
$name
$name
$name
试试这个

$count = count($users);
$c = 0;
foreach ($users as $key => $name){ ?>

    <?php if ($c%2 === 0) {?>
        <table width="100%" border="0">
    <?php } ?>

        <tr>
            <td width="50%" align="center">$name</td>
            <td width="50%" align="center">$name</td>
        </tr>
        <tr>
            <td width="50%" align="center">$name</td>
            <td width="50%" align="center">$name</td>
        </tr>

    <?php if ($c%2 === 1 || $count == ($c+1)) {?>
        </table>
    <?php } ?>

<?php $c++; } ?>
$count=count($users);
$c=0;
foreach($key=>$name的用户){?>
$name
$name
$name
$name

我尝试过,但似乎您缺少tr的结束标记。我在表的结束标记之前的右端放了一个,在if的tr标记之前也放了一个condition@Ahmed表将自动结束标记我尝试过,但似乎您缺少tr的结束标记。我在末尾的右边放了一个在表的结束标记之前,我也在if的tr标记之前放了一个condition@Ahmed表将自动带上关闭标记