希望使用php-mysql在水平表中显示结果


want to display results in horizontal table using php mysql

我正在使用以下代码来获取结果,并希望将其水平显示在两行中。

echo "<table border='0' width='700px' align='center'>";
echo "<tr>";
while($number = mysqli_fetch_array($result2))
{
    echo "<td class='ball_p'>" . $number['number'] . "</td>";
    **echo "</tr><tr>";**
    echo "<td class='search'>" . $number['count'] . "</td>";
}
echo "<tr>";
echo "</table>";

因此,我需要这部分代码不要包含在循环中echo"

请帮忙,这一定很简单。

由于我将查询限制为前10名。

因此:

数字数字等等计数计数等等

因为表需要一次绘制一行,所以您可以将所有number值存储在一个数组中,将所有count值存储在第二个阵列中,然后在构建行时循环遍历每个数组。因此,首先从数据库中获取您的值:

// let's get a variable with the total number of records, i.e. horizontal cells:
$totalRecords = 0;
// get values from db:
while($number = mysqli_fetch_array($result2))
{
    $numberArr[] = $number['number'];
    $countArr[] = $number['count'];
    $totalRecords++;
}

然后在另一组循环中渲染表(每行一个循环):

// first row
echo "<tr>";
for ($i = 0; $i < $totalRecords; ++$i) // you could also use count($numberArr) instead of $totalRecords
{
    echo "<td class='ball_p'>" . $numberArr[$i] . "</td>";
}
echo "</tr>";
// second row
echo "<tr>";
for ($i = 0; $i < $totalRecords; ++$i) 
{
    echo "<td class='ball_p'>" . $countArr[$i] . "</td>";
}
echo "</tr>";

这个怎么样:

$i = 0;
while($number = mysqli_fetch_array($result2))
{
   echo "<td class='ball_p'>" . $number['number'] . "</td>";
    while($i++ >= 1)
    {
      echo "</tr><tr>";
      $i = 0;
    }
    echo "<td class='search'>" . $number['count'] . "</td>";
}

我还没有测试过它,但第二个while循环中$I之后的++应该在评估之后递增,这应该第一次跳过</tr><tr>,第二次打印它。