如何一次在不同的 2 div 中获取数据?

我想在 2 个不同的 div 中显示获取的数据。我从数据库中获取了 2 个随机数据,以显示在两个 div 中。我的第一个 div 有左侧内容和右侧图像,第二个 div 有左侧图像和右侧内容。那么如何在 2 个不同的 div 中显示获取的数据。


下面我提供我的 div


<div class="new-product">

    <div class="row">

        <div class="col-sm-6">

            <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quia sint minima, ab asperiores dolores, aspernatur tempora veritatis consectetur omnis ea dolor, doloremque impedit assumenda placeat quod eligendi! Error, quos, quidem.</p>

        </div>

        <div class="col-sm-6">

            <img src="" alt="">

        </div>

    </div>

    <div class="row">   

        <div class="col-sm-6">

            <img src="" alt="">

        </div>

        <div class="col-sm-6">

            <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quia sint minima, ab asperiores dolores, aspernatur tempora veritatis consectetur omnis ea dolor, doloremque impedit assumenda placeat quod eligendi! Error, quos, quidem.</p>

        </div>

    </div>

</div>

这是获取数据查询


$result  = $this->connect()->query("SELECT * FROM wm_products ORDER BY RAND() LIMIT 2");

        if($result->num_rows > 0){

            while($row = $result->fetch_assoc()){

            }

        }

下面是输出-

http://img3.mukewang.com/61ee0d82000182fb07700505.jpg

慕尼黑5688855
浏览 120回答 1
1回答

慕码人2483693

我认为你可以这样做:$result&nbsp; = $this->connect()->query("SELECT * FROM wm_products ORDER BY RAND() LIMIT 2");$i = 0;$html = "";if($result->num_rows > 0) {&nbsp; &nbsp; $html .= "<div class='new-product'>";&nbsp; &nbsp; while($row = $result->fetch_assoc()) {&nbsp; &nbsp; &nbsp; &nbsp; if($i%2 == 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $html .= "<div class='row'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class='col-sm-6'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <p>{$row->text}</p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class='col-sm-6'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <img src='{$row->image}' alt=''>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>";&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $html .= "<div class='row'>&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class='col-sm-6'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <img src='{$row->image}' alt=''>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class='col-sm-6'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <p>{$row->text}</p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</div>";&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; $i++;&nbsp; &nbsp; }&nbsp; &nbsp; $html .= "</div>";}echo $html;假设您在 SQL 查询中的列被命名为text和image。否则,将$row->textand替换为$row->image相应的列名。
打开App,查看更多内容
随时随地看视频慕课网APP