php表格:每行显示3个单元格

php表格:每行显示3个单元格,php,html-table,Php,Html Table,我看这里: 但是我不能修改我的代码 我需要在表中显示PHP数组结果。 表格每行需要有3个单元格 我不知道如何每三行输出和,如果表中包含的记录不是3的倍数,我也不知道如何输出最后的 这是我的密码: echo "<table>"; $num=mysql_numrows($result1); $i=0; while ($i < $num) { $aaa=mysql_result ($result1,$i,"aaa"); $bbb=mysql_result ($re

我看这里:

但是我不能修改我的代码

我需要在表中显示PHP数组结果。 表格每行需要有3个单元格

我不知道如何每三行输出
,如果表中包含的记录不是3的倍数,我也不知道如何输出最后的

这是我的密码:

echo "<table>";


$num=mysql_numrows($result1);
$i=0;
while ($i < $num)

{


$aaa=mysql_result   ($result1,$i,"aaa");
$bbb=mysql_result   ($result1,$i,"bbb");

echo "

<td>$aaa $bbb</td> ";

$i++;

}



echo "</table>";
echo”“;
$num=mysql\u numrows($result1);
$i=0;
而($i<$num)
{
$aaa=mysql_结果($result1,$i,“aaa”);
$bbb=mysql_结果($result1,$i,“bbb”);
回声“
$aaa$bbb”;
$i++;
}
回声“;

使用mod运算符%检查第三个元素。e、 g

if($i%3 == 0) {
  // this is the third element, start a new tr here
}

使用mod运算符%检查第三个元素。e、 g

if($i%3 == 0) {
  // this is the third element, start a new tr here
}
试试这个

echo "<table><tr>";
$num=mysql_numrows($result1);
$i=0;

while ($i < $num)
{
  $aaa=mysql_result   ($result1,$i,"aaa");
  $bbb=mysql_result   ($result1,$i,"bbb");
  if($i %3 ==0)
  {
     echo"</tr><tr><td> $aaa $bbb </td>";  
  }
  else
  {
    echo"<td> $aaa $bbb </td>";  
  }
  $i++;
}
echo "</tr></table>";
echo”“;
$num=mysql\u numrows($result1);
$i=0;
而($i<$num)
{
$aaa=mysql_结果($result1,$i,“aaa”);
$bbb=mysql_结果($result1,$i,“bbb”);
如果($i%3==0)
{
回声“$aaa$bbb”;
}
其他的
{
回声“$aaa$bbb”;
}
$i++;
}
回声“;
试试这个

echo "<table><tr>";
$num=mysql_numrows($result1);
$i=0;

while ($i < $num)
{
  $aaa=mysql_result   ($result1,$i,"aaa");
  $bbb=mysql_result   ($result1,$i,"bbb");
  if($i %3 ==0)
  {
     echo"</tr><tr><td> $aaa $bbb </td>";  
  }
  else
  {
    echo"<td> $aaa $bbb </td>";  
  }
  $i++;
}
echo "</tr></table>";
echo”“;
$num=mysql\u numrows($result1);
$i=0;
而($i<$num)
{
$aaa=mysql_结果($result1,$i,“aaa”);
$bbb=mysql_结果($result1,$i,“bbb”);
如果($i%3==0)
{
回声“$aaa$bbb”;
}
其他的
{
回声“$aaa$bbb”;
}
$i++;
}
回声“;

要实现以下目标:

通过将其包装到自己的
迭代器中,您可以轻松地对其进行配置,该迭代器提供行和列:

/**
 * function to fetch one row from database
 */
function fetch_row($resultSet = NULL) {
    static $result;
    static $i = 0;
    if ($resultSet) {
        $result = $resultSet;
        return;
    }
    return mysql_fetch_assoc($result);
}

fetch_row($result1); // initialize fetch function

$it = new FetchIterator('fetch_row');

$table = new TableIterator($it, 5);

if ($table->valid()) : ?>

<table border="1">

    <?php foreach ($table as $row => $columns) : ?>

        <tr class="<?php echo $row % 2 ? 'odd' : 'even'; ?>">

            <?php foreach ($columns as $index => $column) : ?>

            <td>
                <?php if ($column) echo $column['aaa'], ' ', $column['bbb']; ?>
            </td>

           <?php endforeach; ?>

        </tr>

    <?php endforeach; ?>

</table>

<?php endif;
/**
*函数从数据库中获取一行
*/
函数fetch_行($resultSet=NULL){
静态结果;
静态$i=0;
如果($resultSet){
$result=$resultSet;
返回;
}
返回mysql\u fetch\u assoc($result);
}
获取_行($result1);//初始化提取函数
$it=新的FetchIterator('fetch_row');
$table=新的TableIterator($it,5);
如果($table->valid()):?>

要实现这样的目标:

通过将其包装到自己的
迭代器中,您可以轻松地对其进行配置,该迭代器提供行和列:

/**
 * function to fetch one row from database
 */
function fetch_row($resultSet = NULL) {
    static $result;
    static $i = 0;
    if ($resultSet) {
        $result = $resultSet;
        return;
    }
    return mysql_fetch_assoc($result);
}

fetch_row($result1); // initialize fetch function

$it = new FetchIterator('fetch_row');

$table = new TableIterator($it, 5);

if ($table->valid()) : ?>

<table border="1">

    <?php foreach ($table as $row => $columns) : ?>

        <tr class="<?php echo $row % 2 ? 'odd' : 'even'; ?>">

            <?php foreach ($columns as $index => $column) : ?>

            <td>
                <?php if ($column) echo $column['aaa'], ' ', $column['bbb']; ?>
            </td>

           <?php endforeach; ?>

        </tr>

    <?php endforeach; ?>

</table>

<?php endif;
/**
*函数从数据库中获取一行
*/
函数fetch_行($resultSet=NULL){
静态结果;
静态$i=0;
如果($resultSet){
$result=$resultSet;
返回;
}
返回mysql\u fetch\u assoc($result);
}
获取_行($result1);//初始化提取函数
$it=新的FetchIterator('fetch_row');
$table=新的TableIterator($it,5);
如果($table->valid()):?>

最后,执行类似于
$empty\u cells=$i%3
的操作,并使用while循环来绘制更多tds的$empty\u cells。您可能会发现用变量替换文字“3”是一个好主意,这样可以轻松地将此代码复制并粘贴到其他项目中,在这些项目中,每行需要不同数量的列。最后,您可以执行类似于
$empty\u cells=$i%3
的操作,并使用while循环来绘制更多tds的$empty\u cells。您可能会发现用变量替换文字“3”是一个好主意,这样可以很容易地将此代码复制并粘贴到其他项目中,在这些项目中,每行需要不同数量的列。每个
..
有多少数据库行?每个
..
有多少数据库行?