1 回答
TA贡献1860条经验 获得超9个赞
我认为你可以这样做:
$result = $this->connect()->query("SELECT * FROM wm_products ORDER BY RAND() LIMIT 2");
$i = 0;
$html = "";
if($result->num_rows > 0) {
$html .= "<div class='new-product'>";
while($row = $result->fetch_assoc()) {
if($i%2 == 0) {
$html .= "<div class='row'>
<div class='col-sm-6'>
<p>{$row->text}</p>
</div>
<div class='col-sm-6'>
<img src='{$row->image}' alt=''>
</div>
</div>";
} else {
$html .= "<div class='row'>
<div class='col-sm-6'>
<img src='{$row->image}' alt=''>
</div>
<div class='col-sm-6'>
<p>{$row->text}</p>
</div>
</div>";
}
$i++;
}
$html .= "</div>";
}
echo $html;
假设您在 SQL 查询中的列被命名为text和image。否则,将$row->textand替换为$row->image相应的列名。
- 1 回答
- 0 关注
- 118 浏览
添加回答
举报