将css应用于从数据库中提取的数据

时间:2014-12-04 10:01:26

标签: php css sql css3 mysqli

我有一个代码从数据库中获取数据并将其显示在网页上,但是在显示数据时我希望以适当的格式显示数据,每行应该有最多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以便每行都有固定数量的值

2 个答案:

答案 0 :(得分:0)

首先,在包装器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作为父级,并将所有div生成给子级

当我们应用float: left时,所有div将并排存在于前一个兄弟姐妹中。

如果他们没有获得所需的宽度,他们会转到另一条线,好像我们已经应用了换行符。

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

答案 1 :(得分:0)

你可以使用模数让你说想要在每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++;

                }