【问题标题】:apply css to data that is being fetched from database将 css 应用于从数据库中获取的数据
【发布时间】:2015-02-02 03:47:04
【问题描述】:

我有一个代码可以从数据库中获取数据并将其显示在网页上,但是在显示数据时我希望以适当的格式显示它,即每行最多应该有 5-6 个项目和数量

1 2 3 4 5 6

7 8 9 10 11 12

13 .. 等等

 <?php  
    $con=mysqli_connect("localhost","root","","db");
    if (mysqli_connect_errno()) 
        {
            echo "Failed to connect to MySQL: " . mysqli_connect_error();
        }

    $student = mysqli_real_escape_string($con, $_POST['student']);
    $classid = mysqli_real_escape_string($con, $_POST['classid']);

    $result = mysqli_query($con,"SELECT * FROM class where classid='".$classid."'");
    while($row = mysqli_fetch_array($result)) 
        {
            $z=$row['studentid'];


            $resultt = mysqli_query($con,"SELECT * FROM  school where id='".$z."'");
            while($roww = mysqli_fetch_array($resultt)) 
                {
                    echo "<div class='ddd'>";
                        echo "<h5>";
                            echo "<ul>";
                                echo "<li>".$roww['student']."</li>";
                            echo "</ul>";
                        echo "</h5>";
                    echo "</div>";

                }


        }
    mysqli_close($con);
    ?>    

但是我无法理解如何应用 css 以便每一行在其中获得固定数量的值

【问题讨论】:

  • 您可能希望删除此问题的所有数据库部分,并简单地将您的问题基于输出的 HTML - PHP 和数据库代码与您的要求无关。

标签: php sql css mysqli


【解决方案1】:

首先,在包装器 div 中显示所有结果。

?>
<div id="rest" style="width:99%">
<?php
$resultt = mysqli_query($con,"SELECT * FROM  school where id='".$z."'");
while($roww = mysqli_fetch_array($resultt)) 
{
    echo "<div class='ddd'>";
    echo "<h5>";
    echo "<ul>";
    echo "<li>".$roww['student']."</li>";
    echo "</ul>";
    echo "</h5>";
    echo "</div>";
}
?>

</div>

然后应用样式表。

<style>
#rest .ddd {
  width: 16.3%;
  float: left;
}
</style>

说明:

我们将#rest 设为父母,并将所有由此产生的divs 设为儿童

当我们应用float: left 时,所有的 div 将与之前的兄弟并排驻留。

如果它们没有获得所需的宽度,它们将转到另一行,就像我们应用了换行符一样。

一行需要 6 个 divs,所以 100/6 = 13.66 这将是每个子 div 的宽度。

【讨论】:

    【解决方案2】:

    您可以使用模数,假设您想在每 6 个项目后中断

    $resultt = mysqli_query($con,"SELECT * FROM  school where id='".$z."'");
    $i=1;
                while($roww = mysqli_fetch_array($resultt)) 
                    {
                        echo "<div class='ddd'>";
                            echo "<h5>";
                                echo "<ul>";
                                    echo "<li>".$roww['student']."</li>";
                                echo "</ul>";
                            echo "</h5>";
                        echo "</div>";
    
                             if( $i % 6 == 0){
                                  Your code to break line like <br/> in HTML
                              }
                             $i++;
    
                    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-12-02
      • 2015-02-23
      • 1970-01-01
      • 2020-05-26
      • 2015-10-16
      • 2013-08-29
      • 2011-03-21
      相关资源
      最近更新 更多