php - Fetch all results MYSQL table - 6 items per row. If less than 6 results exist, list advert templates?
Get the solution ↓↓↓I have a total of 8 results/users in my MYSQL table 'users'.
I want to display 6 results/user profiles per row. 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. 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.
Answer
Solution:
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
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
Thefor (;$nrProfilesOnScreen % $limit;) { }
may seem strange,
it's an alternative forwhile ($nrProfilesOnScreen % $limit !=0) {}
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: fastcgi sent in stderr: "primary script unknown" while reading response header from upstream
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.