首页 > 解决方案 > 动态构建引导行,每行 3 个

问题描述

我不确定解决此问题的最佳方法,在我正在构建的站点(使用引导程序)上,我希望使用网格系统每行有 3 张卡,例如:

HTML:

<div class="container">
  <div class="row">
    <div class="col-sm">
      One of three columns
    </div>
    <div class="col-sm">
      One of three columns
    </div>
    <div class="col-sm">
      One of three columns
    </div>
  </div>
</div>

这通常没问题,但我正在动态构建(或尝试):

PHP:

  <main>
    
    <br /><br /><br />
    
    <?php
    
        $pages = array_slice(scandir($_SERVER['DOCUMENT_ROOT']), 2);    
        $mixed = shuffle($pages);
        $count = 0;

        echo '<div class="container"><div class="row">';    
    
        foreach($pages as $page) 
        {
            $count++;
            if (strpos($page, '.php') !== false && $page != 'index.php') {
                
                $html = file_get_contents($page);
                $code = explode("|", extractXvideos($html));
                
            ?>
                <div class="col-md-4">
                    <div class="card" style="width: 18rem;">
                      <img src="<?= $code[3]; ?>" class="card-img-top" alt="<?= $code[0]; ?>">
                      <div class="card-body">
                        <p class="card-text"><a href="<?= $page; ?>"><?= substr($code[0], 0, 25); ?> ...</a></p>
                      </div>
                    </div>
                </div>

            <?php
            
            if ($count == 18) {
                // The reason only 15 is showing is because we ignore ".", ".." & "index.php".
                break;
            }
            
            }           
        }
        
        echo '</div></div>';        
    
    ?>
    
  </main>

对于这个项目,我正在服务器上扫描 .php 页面,然后尝试将它们布置为每行 3 个,所以在每行 3 个之后我需要echo '<div class="container"><div class="row">';从我所看到的开始一个新行,我不知道最好的方法为此,我们将不胜感激。

标签: php

解决方案


main

<?php
    $pages = array_slice(scandir($_SERVER['DOCUMENT_ROOT']), 2);    
    $mixed = shuffle($pages);
    $count = 0;
    $output = '';

    foreach($pages as $page) {
        $count++;
        if (strpos($page, '.php') !== false && $page != 'index.php') {
            
            $html = file_get_contents($page);
            $code = explode("|", extractXvideos($html));
            
            $output .= '<div class="container">';
                $output .= '<div class="row">';
                    $output .= '<div class="col">Column</div>';
                    $output .= '<div class="col">Column</div>';
                    $output .= '<div class="col">Column</div>';
                $output .= '</div>
            $output .= '</div>';
        
        }            
    } 
    echo $output;   
?>

main

试试这个模板并应用你的条件。


推荐阅读