我的英语非常糟糕,我在这里感到困惑,有人可以帮助我吗?
我的PHP:
<?php require("db.php"); ?>
<?php //Select All Orders
$stmt = $db->prepare("SELECT * FROM `users`");
$stmt->execute();
$result = $stmt->get_result();
if($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$counter++;
if($counter % 2 == 0) {
echo "<div class='ui stackable six cards'>";
echo $one[] = "<div class='ui card small'><div class='image'><img src='".$row["avatar"]."'></div>";
echo $two[] = "<div class='content'><a class='header'>Dias".$row["username"]."</a></div></div>";
echo "</div>";
}
}
}
$stmt->close();
?>
所以我想要做的就是我要拆分的每2行。
所以它看起来像这样:
<div class="ui stackable six cards">
<div class="ui card small">
<div class="image">
<img src="assets/img/players/dias.jpg">
</div>
<div class="content">
<a class="header">Dias</a>
</div>
</div>
<div class="ui card small">
<div class="image">
<img src="assets/img/players/dias.jpg">
</div>
<div class="content">
<a class="header">Dias</a>
</div>
</div>
分离出来的是“<div class="ui stackable six cards">
”以及“</div>
”
我究竟如何使用我的PHP代码每两行拆分一次?我无法弄清楚
因此,对于它在数据库中找到的每2行,它需要在末尾回显“<div class="ui stackable six cards">
”和“</div>
”。
这是不可能的吗?它看起来如此
答案 0 :(得分:-1)
我建议使用切换布尔技术而不是模数条件,因为我发现它更容易阅读。如果结果集中有奇数行,则此方法还将完成外部(分组)div生成。
代码:(Demo)
$resultset=[
['avatar'=>1,'username'=>'A'],
['avatar'=>2,'username'=>'B'],
['avatar'=>3,'username'=>'C'],
['avatar'=>4,'username'=>'D'],
['avatar'=>5,'username'=>'E'],
['avatar'=>6,'username'=>'F']
];
if(sizeof($resultset)){ // your num rows
$new_group=true;
foreach($resultset as $row){ // you while loop
if($new_group){
echo "<div class='ui stackable six cards'>"; // start new group
}
echo "<div class='ui card small'>";
echo "<div class='ui card small'><div class='image'><img src='".$row["avatar"]."'></div>";
echo "<div class='content'><a class='header'>Dias".$row["username"]."</a></div></div>";
echo "</div>";
if(!$new_group){echo "</div>";} // end group
$new_group=!$new_group; // toggle the boolean value
}
if(!$new_group){ // if new group is not closed by the "second" member in the group
echo "</div>";
}
}
输出:
<div class='ui stackable six cards'>
<div class='ui card small'>
<div class='ui card small'><div class='image'><img src='1'></div>
<div class='content'><a class='header'>DiasA</a></div></div>
</div>
<div class='ui card small'>
<div class='ui card small'><div class='image'><img src='2'></div>
<div class='content'><a class='header'>DiasB</a></div></div>
</div>
</div>
<div class='ui stackable six cards'>
<div class='ui card small'>
<div class='ui card small'><div class='image'><img src='3'></div>
<div class='content'><a class='header'>DiasC</a></div></div>
</div>
<div class='ui card small'>
<div class='ui card small'><div class='image'><img src='4'></div>
<div class='content'><a class='header'>DiasD</a></div></div>
</div>
</div>
<div class='ui stackable six cards'>
<div class='ui card small'>
<div class='ui card small'><div class='image'><img src='5'></div>
<div class='content'><a class='header'>DiasE</a></div></div>
</div>
<div class='ui card small'>
<div class='ui card small'><div class='image'><img src='6'></div>
<div class='content'><a class='header'>DiasF</a></div></div>
</div>
</div>