gpt4 book ai didi

php - 想要使用 php mysql 在水平表中显示结果

转载 作者:行者123 更新时间:2023-11-30 00:59:44 26 4
gpt4 key购买 nike

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

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 "";

请帮忙,这一定很简单。

I want the results to be displayed 10 across as I limit my query to the top 10.

So it would be:

Number Number Number etc. Count Count Count etc.

最佳答案

由于表格需要一次绘制一行,因此您可以将所有 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>";

关于php - 想要使用 php mysql 在水平表中显示结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20252113/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com