简体   繁体   English

限制表格行中的列数

[英]Limit number of columns in table row

I am populating an HTML table from a SQL database using php. 我正在使用php从SQL数据库填充HTML表。 What I want to do is, I want to limit the number of columns in one row to 4. I tried doing it with CSS using 我想做的是,我想将一行中的列数限制为4。我尝试使用CSS使用

table.posts{
border:10px;
width:100%;
columns:auto 4;
-webkit-columns:auto 4;

This is the code I have: 这是我的代码:

<table class="posts">
    <tr>
        <?php 
        $respost = mysqli_query($link,"SELECT * FROM posts WHERE post_author=$uid");
        while($rowpost = mysqli_fetch_array($respost)) { ?>
            <td>    
                <h3><?php echo $rowpost['post_title']; ?></a></h3>
                <h4><?php //echo $rowpost['post_content']; ?></h4>
            </td>
            <?php 
        }
        ?>
        </tr>
</table>

There is no problem with the data I get from the db. 我从数据库获得的数据没有问题。 It's just that it all shows in one row. 只是它们全部显示在一行中。 I want to limit this to just 4 items per row and then break and continue on the next row. 我想将其限制为每行仅4个项目,然后中断并继续下一行。 What is a simple way to do this? 有什么简单的方法可以做到这一点?

Try like this 这样尝试

<table class="posts">
    <?php 
    $respost = mysqli_query($link,"SELECT * FROM posts WHERE post_author=$uid");
    $row_count=0;
    $col_count=0;
    while($rowpost = mysqli_fetch_array($respost)) { 
     if($row_count%4==0){
        echo "<tr>";
        $col_count=1;
     }
     ?>
        <td>    
            <h3><?php echo $rowpost['post_title']; ?></a></h3>
            <h4><?php //echo $rowpost['post_content']; ?></h4>
        </td>
        <?php
        if($col_count==4){
           echo "</tr>";
        }
        $row_count++; 
        $col_count++; 
    }
    ?>
</table>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM