[英]Create multi-row tables in PHP using MySQL
I'm trying to create something like the following: 我正在尝试创建如下内容:
But my current code has given me: 但是我当前的代码给了我:
The part in white is what I get with the code below, and the part in black is what I get when I add in two more <td>
rows. 白色的部分是我在下面的代码中得到的,黑色的部分是当我再添加两行
<td>
行时得到的。 The problem is I can't get 3 pieces of data per row to be created - only vertically or horizontally. 问题是我无法每行创建3条数据-仅垂直或水平。
$result = mysqli_query($con,"SELECT * FROM show_listing");
while($row = mysqli_fetch_array($result))
{
echo "<table>
<tr>
<td>".$row['name']."'</td>
</tr>
</table>";
}
mysqli_close($con);
It may be simpler than I think but does anyone know how I could do this? 它可能比我想象的要简单,但是有人知道我该怎么做吗?
$c = 0;
echo "<table>";
while($row = mysqli_fetch_array($result))
{
if ($c%3 == 0) {
if ($c) echo "</tr>";
echo "<tr>";
}
echo "<td>".$row['name']."</td>";
$c++;
}
if ($c) echo "</tr>";
echo "</table>";
You have the correct idea. 你有正确的主意。 You do not want the declaration in the while loop as the result is that you will get multiple tables.
您不希望在while循环中声明,因为结果是您将获得多个表。
So I moved the table declarations outside of the while loop. 所以我将表声明移到while循环之外。 I also made the declarations conditional based on the current column.
我还根据当前列为条件声明。 You can now set the variable $columns to however many columns you want.
现在,您可以将变量$ columns设置为所需的任意多列。
$result = mysqli_query($con,"SELECT * FROM show_listing");
echo "<table>"
$columns=3;
$current_column=1;
while($row = mysqli_fetch_array($result))
{
if($current_column==1) {
echo "<tr>";
}
echo "<td>".$row['name']."'</td>";
if($current_column==$columns) {
echo "</tr>";
$current_column=1;
} else
$current_column++;
}
echo "</table>
mysqli_close($con);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.