[英]Fetch all results MYSQL table - 6 items per row. If less than 6 results exist, list advert templates?
I have a total of 8 results/users in my MYSQL table 'users'.我的 MYSQL 表“用户”中共有 8 个结果/用户。
I want to display 6 results/user profiles per row.我想每行显示 6 个结果/用户配置文件。 This is what I'm currently getting as the result:
这是我目前得到的结果:
1st Result. 2nd Result. 3rd Result. 4th Result. 5th Result. 6th Result
7th Result. 8th Result. No More Results.....
Not all users will have a profile image, so my code also checks for a profile image and if it can't find one it will use a template profile image instead.并非所有用户都有个人资料图片,因此我的代码还会检查个人资料图片,如果找不到,它将使用模板个人资料图片。
It is a requirement for each row to be complete with a minimum of 6 results/profiles.要求每行至少包含 6 个结果/配置文件。 Where there is not enough results/profiles to complete a row, then I am trying to fill in the remaining non existent profiles with an advert template 'advertise your profile here'.
如果没有足够的结果/配置文件来完成一行,那么我会尝试使用广告模板“在此处宣传您的个人资料”来填写剩余的不存在的配置文件。
The advert template image is stored in the following directory:广告模板图像存储在以下目录中:
<div><img src="data/profile/0/main/advert.jpg" alt="Profile" height="100%" width="100%"></div>';
This is my desired result:这是我想要的结果:
1st Result. 2nd Result. 3rd Result. 4th Result. 5th Result. 6th Result
7th Result. 8th Result. 9 Ad Here. 10 Ad Here. 11 Ad Here. 12 Ad Here.
Here is the code that I currently have.这是我目前拥有的代码。
<?php $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);
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>';
} }
echo '</div>';
} ?>
However, the code is still not giving the desired result.但是,代码仍然没有给出预期的结果。 Please can someone help me improve/amend the code to get it to give me the result i require.
请有人可以帮助我改进/修改代码以得到它给我所需的结果。 Thank you.
谢谢你。
I am copying your complete code because i change some formatting, which makes code more readable.我正在复制您的完整代码,因为我更改了一些格式,这使代码更具可读性。
The inserted pieces of code use the variable $nrProfilesOnScreen
插入的代码使用变量
$nrProfilesOnScreen
You might need to change the line that contains '?????'....您可能需要更改包含 '?????'.... 的行
<?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>';
}
?>
The %
operator is explained here:https://www.php.net/manual/en/language.operators.arithmetic.php %
运算符在这里解释:https://www.php.net/manual/en/language.operators.arithmetic.php
The for (;$nrProfilesOnScreen % $limit;) { }
may seem strange, it's an alternative for while ($nrProfilesOnScreen % $limit !=0) {}
for (;$nrProfilesOnScreen % $limit;) { }
可能看起来很奇怪,它是while ($nrProfilesOnScreen % $limit !=0) {}
的替代品
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.