dongwai4434 2017-08-02 17:59 采纳率: 0%
浏览 22
已采纳

如何在三列中打印出数据? [关闭]

I want to print out a simple list of names from a mysql database into an html page that is using Bootstrap 3 css.

But rather than have one column with a long list I want to divide up the list in three columns ( on a desktop).

The code below does what I want but is not responsive. If I shrink the page all the names overlap. How can I print out the list in a responsive page (with three colums for desktop and perhaps two for mobile).

Thanks.

<?php
$result = mysqli_query($db_conx,"SELECT count(*) FROM mydatabase"); //Count Records
$row = mysqli_fetch_row($result);

$x=0; //Create a counter to count records that have been echoed.
$num = $row[0];
$sum =($num)/3; //Divide the total number of records by 3

$sql = mysqli_query($db_conx, "SELECT * FROM mydatabase"); //Get records
while($row = mysqli_fetch_assoc($sql)){
  $x++; //Add 1 to counter


  echo $row['name']; //echo row

  echo "</br>";

if ($x>$sum){ //If the number of records already echoed is more than a third of the total then create a new column.
  echo "</div>";
  echo "<div class=\"col-sm-4\">";
  $x=0; //Reset counter
  }

} //End while loop
echo "</div>";
?>
  • 写回答

1条回答 默认 最新

  • duanfen2349 2017-08-02 18:09
    关注

    Just try like this code 100% working and tested:

    <?Php
        $connection = new mysqli('HOST_NAME', 'USER_NAME', 'PASSWORD', 'DATABASE_NAME');
    
        if ($connection->connect_errno > 0) {
            die ('Unable to connect to database [' . $connection->connect_error . ']');
        }   
        $sql = "SELECT * FROM products";
        if (!$result = $connection->query($sql)) {
            die ('There was an error running query[' . $connection->error . ']');
        }   
    ?>
    
    ...
    
    <?php
        $rows = $result->num_rows;    // Find total rows returned by database
        if($rows > 0) {
            $cols = 3;    // Define number of columns
            $counter = 1;     // Counter used to identify if we need to start or end a row
            $nbsp = $cols - ($rows % $cols);    // Calculate the number of blank columns
    
            $container_class = 'container-fluid';  // Parent container class name
            $row_class = 'row';    // Row class name
            $col_class = 'col-sm-4'; // Column class name
    
            echo '<div class="'.$container_class.'">';    // Container open
            while ($item = $result->fetch_array()) {
                if(($counter % $cols) == 1) {    // Check if it's new row
                    echo '<div class="'.$row_class.'">';    // Start a new row
                }
                        $img = "http://yourdomain.com/assets/".$item['image_name'];
                        echo '<div class="'.$col_class.'">'.$img.'<h5>'.$item['name'].'</h5></div>';     // Column with content
                if(($counter % $cols) == 0) { // If it's last column in each row then counter remainder will be zero
                    echo '</div>';   //  Close the row
                }
                $counter++;    // Increase the counter
            }
            $result->free();
            if($nbsp > 0) { // Adjustment to add unused column in last row if they exist
                for ($i = 0; $i < $nbsp; $i++)  { 
                    echo '<div class="'.$col_class.'">&nbsp;</div>';        
                }
                echo '</div>';  // Close the row
            }
            echo '</div>';  // Close the container
        }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 删除虚拟显示器驱动 删除所有 Xorg 配置文件 删除显示器缓存文件 重启系统 可是依旧无法退出虚拟显示器
  • ¥15 vscode程序一直报同样的错,如何解决?
  • ¥15 关于使用unity中遇到的问题
  • ¥15 开放世界如何写线性关卡的用例(类似原神)
  • ¥15 关于并联谐振电磁感应加热
  • ¥60 请查询全国几个煤炭大省近十年的煤炭铁路及公路的货物周转量
  • ¥15 请帮我看看我这道c语言题到底漏了哪种情况吧!
  • ¥66 如何制作支付宝扫码跳转到发红包界面
  • ¥15 pnpm 下载element-plus
  • ¥15 解决编写PyDracula时遇到的问题