You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Bootstrap Row中输出数据库条目至独立col-md-2列的问题

问题解决:Bootstrap列横向排列修正

你的问题出在每次循环都生成了一个独立的.row容器——Bootstrap的.row是一行的包裹容器,所有要横向排列的列必须放在同一个.row里面,否则每个列都会单独占一行,自然就纵向堆叠了。

修正后的代码如下:

$sql = "SELECT advertiser_URL, advertiser_logo  FROM advertisers";
if ($result = mysqli_query($link, $sql))
{
    if (mysqli_num_rows($result) > 0)
    {
        // 把.row放在循环外面,所有列都归入同一行容器
        echo "<div class='row'>";
        while ($row = mysqli_fetch_array($result))
        {
            // 循环里只输出单个col-md-2列,给HTML属性加引号避免解析问题
            echo "<div class='col-md-2'>
                    <a href='{$row['advertiser_URL']}'>
                        <img src='data:image/jpeg;base64," . base64_encode($row['advertiser_logo']) . "'/>
                    </a>
                  </div>";
        }
        echo "</div>"; // 闭合row容器
    }
    else
    {
        echo "No records matching your query were found.";
    }
}
else
{
    echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}

补充两个细节:

  • 给HTML属性(class、href等)加上引号是标准写法,能避免因属性值含特殊字符导致的解析错误
  • 若后续数据量超过6条(Bootstrap一行总宽度为12,12/2=6),列会自动换行,符合响应式布局逻辑

内容的提问来源于stack exchange,提问作者Georgeehall

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.25 01:06:26