1 回答
TA贡献1993条经验 获得超5个赞
我正在复制您的完整代码,因为我更改了一些格式,这使代码更具可读性。
插入的代码段使用变量$nrProfilesOnScreen
您可能需要更改包含“??????”的行 ....😉
<?php
$conn = new mysqli("localhost", "root", "******", "test");
$sql = "SELECT * FROM users WHERE status = 'active' AND usertype = 'advertiser'";
$result = $conn->query($sql)->fetch_all(MYSQLI_ASSOC);
$limit = 6;
$chunks = array_chunk($result, $limit);
$nrProfilesOnScreen = 0;
foreach($chunks as $chunk){
echo '<div id="category_case_holder">';
foreach($chunk as $chunkItem){
$i = htmlspecialchars($chunkItem['user_id']);
$filename = "data/profile/$i/main/profile.jpg";
if (file_exists($filename)) {
echo '<div id="prime"><a href="profile.php?id='.htmlspecialchars($chunkItem['user_id']).'"><img src="data/profile/'.htmlspecialchars($chunkItem['user_id']).'/main/profile.jpg" alt="Profile" height="100%" width="100%"></a></div>';
}else{
echo '<div id="prime"><a href="profile.php?id='.htmlspecialchars($chunkItem['user_id']).'"><img src="data/profile/0/main/profile.jpg" alt="Profile" height="100%" width="100%"></a></div>';
}
$nrProfilesOnScreen++;
}
for (; $nrProfilesOnScreen % $limit !=0; ) {
echo '<div id="prime"><a href="profile.php?id='.'?????'.'"><img src="data/profile/0/main/profile.jpg" alt="Profile" height="100%" width="100%"></a></div>' . PHP_EOL;
$nrProfilesOnScreen++;
}
echo '</div>';
}
?>
运算%符在这里解释:https ://www.php.net/manual/en/language.operators.arithmetic.php
这for (;$nrProfilesOnScreen % $limit;) { } 可能看起来很奇怪,它是while ($nrProfilesOnScreen % $limit !=0) {}
- 1 回答
- 0 关注
- 89 浏览
添加回答
举报