我正在尝试列出表用户的所有结果/用户数,并显示其个人资料图像。
我想在每行显示6,然后将下一个6,下一个和下一个依次类推,以此类推。
到目前为止,这是我的代码:
<div id="category_case_holder">
<?php $sql = "SELECT * FROM users WHERE status = 'active' AND usertype = 'advertiser'";
$result = $conn->query($sql)->fetch_all(MYSQLI_ASSOC);
for ($i = 1; $i <= 6; $i++) {
$key = $i - 1;
if (isset($result[$key])) {
$filename = "data/profile/$i/main/profile.jpg";
if (file_exists($filename)) {
echo '<div id="prime"><a href="profile.php?id='.htmlspecialchars($result[$key]['user_id']).'"><img src="data/profile/'.htmlspecialchars($result[$key]['user_id']).'/main/profile.jpg" alt="Profile" height="100%" width="100%"></a></div>';
} else {
echo '<div id="prime"><a href="profile.php?id='.htmlspecialchars($result[$key]['user_id']).'"><img src="data/profile/0/main/profile.jpg" alt="Profile" height="100%" width="100%"></a></div>';
} } else {
echo '<div id="prime"><img src="data/profile/0/main/advert.jpg" alt="Profile" height="100%" width="100%"></div>';
} } ?>
</div>
CSS样式:
#category_case_holder {
height:150px;
display: flex;
width:100%;
height:150px;
position:relative;
}
#category_case_holder div {
flex-basis: 100%;
background:#1f1f1f;
margin:0 0 0 10px;
border: 1px solid #333;
box-shadow: 0 0 8px 2px #ccc; /* the shadow */
}
请有人可以帮助改善/修改我的代码以获得我需要的结果。
谢谢
The following is wholly untested and perhaps wide of the mark having not seen the data nor had an answer regarding the profile image but the general aim was to break the results array into little bits -
array_chunk
is ideal for this.如果我理解您的问题,我认为您应该尝试以下操作: