首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PHP样式设置

问PHP样式设置
EN

Stack Overflow用户
提问于 2017-02-04 08:42:09
回答 2查看 22关注 0票数 1

很高兴你来了。我需要解决方案,而且我在这些事情上是个新手。现在我的页面看起来像这样

代码语言:javascript
复制
1.text    2.text    3.text    4.text

但我需要的是这样

代码语言:javascript
复制
1.text    2.text
3.text    4.text

我拥有的代码

代码语言:javascript
复制
<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>
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-02-04 08:48:16

for()循环在这方面没有用处,因为它内部的while()循环将处理查询返回的每一行。

您应该只使用while循环,然后使用计数器来判断是否启动新的<tr>。

代码语言:javascript
复制
<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>
票数 1
EN

Stack Overflow用户

发布于 2017-02-04 09:06:35

代码语言:javascript
复制
<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>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42035228

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档