很高兴你来了。我需要解决方案,而且我在这些事情上是个新手。现在我的页面看起来像这样
1.text 2.text 3.text 4.text但我需要的是这样
1.text 2.text
3.text 4.text我拥有的代码
<table >
<?php
for ($x = 1; $x <= 2; $x++): ?>
<tr>
<?php while($row = $choices->fetch_assoc()): ?>
<td><button class="pogaAtbilzuVarianti" value="<?php echo $row['id_var']; ?>" id="atbilde_a"><?php echo $row['teksts']; ?></button></td>
<?php endwhile; ?>
</tr>
<?php endfor; ?>
</table>发布于 2017-02-04 08:48:16
for()循环在这方面没有用处,因为它内部的while()循环将处理查询返回的每一行。
您应该只使用while循环,然后使用计数器来判断是否启动新的<tr>。
<table>
<?php
$counter = 0;
while ($row = $choices->fetch_assoc()) {
if ($counter % 2 == 0) { // Start a new row before event elements
echo "<tr>";
}
?>
<td><button class="pogaAtbilzuVarianti" value="<?php echo $row['id_var']; ?>" id="atbilde_a"><?php echo $row['teksts']; ?></button></td>
<?php
if ($counter % 2 == 1) { // End row after odd elements
echo "</tr>";
}
$counter++;
}
if ($counter % 2 == 1) { // End last row if it only had 1 column
echo "</tr>";
}
?>
</table>发布于 2017-02-04 09:06:35
<table >
<?php while($row = $choices->fetch_assoc()):
if(($row['id_var'] % 2) == 1) echo '<tr>';
?>
<td><button class="pogaAtbilzuVarianti" value="<?php echo $row['id_var']; ?>" id="atbilde_a"><?php echo $row['teksts']; ?></button></td>
<?php
if(($row['id_var'] % 2) == 0) echo '</tr>';
endwhile; ?>
</table>https://stackoverflow.com/questions/42035228
复制相似问题