我有一个PHP循环遍历我的Media表,在表中他们是这些字段: id(唯一id_,post_id(每个帖子的随机ID),user_id,file_format,group_id
如果上传了一组多个图像,则它们具有唯一的post_id,并具有相同的group_id,以区分其他组照片。但是,我无法弄清楚如何将循环内的这些组与if语句分开以打破div行。 这是我的代码:
<?php
$sql = "SELECT * FROM media WHERE user_id = '".$_SESSION['user']."'";
$result = $conn->query($sql);
?>
<?php while($row = mysqli_fetch_array($result)):?>
<?php $group_id = $row['group_id'] ?>
<?php if($row['group_id'] == $group_id):?>
<div class="card-group" style="color:black;">
<div class="card">
<img class="card-img-top" src="uploads/<?=$row['post_id']?>.<?=$row['file_format']?>" alt="Card image cap">
<div class="card-body">
<h4 class="card-title">Card title</h4>
<p class="card-text">This is a wider card with supporting text below as a natural lead-in to additional content. This content is a little bit longer.</p>
<p class="card-text"><small class="text-muted">Last updated 3 mins ago</small></p>
</div>
</div>
</div>
<?php endif;?>
<?php endwhile;?>
&#13;
答案 0 :(得分:0)
我假设您的表media
是这样的:
+---------+-------------+-----------+-----------+---------+
|unique_id| post_id | user_id |file_format|group_id |
+---------+-------------+-----------+-----------+---------+
|1 |56 | johndoe66 |.jpg |Home |
+---------+-------------+-----------+-----------+---------+
|2 |57 | johndoe66 |.png |School |
+---------------------------------------------------------+
首先得到SELECT DISTINCT group_id FROM media WHERE user_id = $_SESSION['user_id']
$conn = new mysqli("server","user","pass","your_database_name");
//will use the Object Oriented way, i'm used to it
$sql = "SELECT DISTINCT group_id FROM media WHERE user_id = $_SESSION['user_id']`";
$groups_result = $conn->query($sql);
//create a group array and format it as you like
$user_groups = array();
while($row = $groups_result->fetch_array()){
$user_groups[] = $row['group_id`];
}
首先会获得该用户的所有不同组,并将其存储在$user_groups
中。然后:
Foreach $user_groups
:
您现在想要获取每个组的帖子:
for($i = 0;$i < count($user_groups);$i++){
$sql = "SELECT * FROM media WHERE user_id = '".$_SESSION['user_id']."' AND group_id = '".$user_groups[$i]."'"
//create a group div with maybe the <div id='$user_groups[$i]'>
while($row = $mysqli_fetch_array($conn->query($sql)){
//create a picture collage of all the posts in the current $i group
}
//close the group </div>
}
编辑有帮助吗?。
答案 1 :(得分:0)
你想要得到类似的东西吗?我使用临时变量来确定组在循环期间何时更改,并且我通过group_id命令您的mysql查询:
<?php
$sql = "SELECT * FROM media WHERE user_id = '".$_SESSION['user']."' order by group_id";
$result = $conn->query($sql);
$oldGroupId = -1;
while($row = mysqli_fetch_array($result)) {
$group_id = $row['group_id'];
if($oldGroupId != $group_id) {
if($oldGroupId != -1) {
echo("</div>");
}
echo("<div class=\"card-group\" style=\"color:black;\">\n");
$oldGroupId = $group_id;
}
echo("<div class=\"card\">
<img class=\"card-img-top\" src=\"uploads/<?=".($row['post_id'])."?>.<?=".($row['file_format'])."?>\" alt=\"Card image cap\">
<div class=\"card-body\">
<h4 class=\"card-title\">Card title</h4>
<p class=\"card-text\">This is a wider card with supporting text below as a natural lead-in to additional content. This content is a little bit longer.</p>
<p class=\"card-text\"><small class=\"text-muted\">Last updated 3 mins ago</small></p>
</div>
</div>");
}
echo("</div>\n");