首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何为3-4列数据生成新div行

如何为3-4列数据生成新div行
EN

Stack Overflow用户
提问于 2021-02-11 12:15:06
回答 1查看 42关注 0票数 1

我知道这个问题以前已经提出了很多,但我仍然不能让它起作用。我正在尝试添加我制作的每个帖子,以并排显示每个3-4列。现在,我有这样的东西:

但我想让它们并排在一起,就像这样

(图片来自上一个问题)。下面是我的代码片段

代码语言:javascript
复制
<div>
            <?php
            global $connection;
            $nRows = $connection->query("SELECT * 
                                                   FROM posts
                                                   ORDER BY post_id DESC");
            if($nRows->rowCount() > 0) {
                while ($row = $nRows->fetch()) {
                    $post_title = str_replace('_', ' ', $row['post_title']);
                    $post_author = $ed->encrypt_decrypt('decrypt',$row['post_author']);
                    $post_file = $row['post_file'];
                    $post_date = $row['post_time'];
                    ?>
                    <!-- Blog Content BEGIN Display-->
                    <div class="column">
                        <h2>
                            <!-- This redirects you to a specific post -->
                            <a href="post/postFetch/fetchByTitle/fetchByPT.php?post_id=<?php echo $row['post_id'];?>&post_title=<?php echo $row['post_title'];?>" class="link-post-title" style="font-family: Arial"><?php echo "#".$row['post_id'] . " ". $post_title;?></a>
                        </h2>

                        <p class="lead" style="font-family: FontAwesome">
                            <!-- This redirects you to the post author -->
                            &#xf21b; <a href="post/postFetch/fetchByAuthor/fetchByPA.php?post_author=<?php echo $row['post_author'];?>" class="link-post-author" style="font-family: Arial"><?php echo $post_author;?> </a> <?php //echo ' ' . '#' . str_replace(',', '#', $row['post_tags']);?>
                        </p>

                        <!-- if there is no image, just remove the entire div --><?php
                        $file_parts = pathinfo($post_file);
                        if(isset($file_parts['extension'])){
                            switch ($file_parts['extension']){
                                case "jpg":
                                    if(!empty($post_file)) { ?>
                                        <div class="imgBox">
                                             <img src="post/postFiles/<?php echo $post_file;?>">
                                        </div><?php
                                    }
                                    break;
                                case "mp4":?>
                                    <div class="vidBox">
                                         <video width="615" height="315" controls>
                                             <source src="post/postFiles/<?php echo $post_file;?>">
                                         </video>
                                    </div><?php
                                    break;
                                case "": // Handle file extension for files ending in '.'
                                case NULL: // Handle no file extension
                                    break;
                            }
                        }?>
                        <p style="font-family: Arial"><span class="glyphicon glyphicon-time"></span><?php
                            echo "Posted on " . $post_date;?>
                        </p>
                    </div>
                    <!-- Blog Content END Display --><?php
                }
            } else { ?>
                <p style="color: darkgoldenrod" class="mssgAlign"><u>NO RECORDS</u></p><?php
            }
            $nRows = null;
            }?>
        </div>

我没有使用Bootstrap。也许这是我的错误,我应该开始这样做,但在此期间,我将继续我的做法。欢迎任何帮助/提示

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-02-11 19:40:59

您可以将每4列(带有内容的div)的组包装在包装器(parent Div)中,并设置样式列以显示内嵌块。

代码语言:javascript
复制
<?php
$break = 0;

for($i = 0; $i < 200; $i++)
{   
    if($break % 4 === 0)
    {
        echo '<br>open div<br/>'; //your wrapper/row start
    }

    $break++;

    echo $i . ' '; //your column with content

    if($break % 4 === 0)
    {
        echo '<br />close div<br />'; //your wrapper/row end
    }   
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66148683

复制
相关文章

相似问题

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