How to prevent title repeating in php mysqli

I have joined two tableimages
andpost
tables my script problem when i uploaded two or more images toimage
tablepost
table title only one but it repeating images like number i want to know how to fix it anyone can help me thanks my English problem
Post table like this
id title
3 example
Images table like this
id img post_id
1 123.jpg 3
2 22.jpg 3
3 11.jpg 3
4 21.jpg 3
5 34.jpg 3
Post titleone
but it repeating 5time
images with i want to prevent it
Here is my source code
<?php
if ($stmt = $con->prepare(" SELECT p.*,i.img,title,id
from post AS p LEFT JOIN images AS i ON i.post_id= p.id ")) {
$stmt->execute();
}
$result = $stmt->get_result();
if ($result->num_rows>0) {
while ($row = $result->fetch_assoc()) {
?>
<?php echo $row['title']; ?>
<img src="images/<?php echo $row['img']; ?>"height="20"width="20"/>
<?php
}}
?>
Answer
Solution:
<?php
if ($stmt = $con->prepare(" SELECT p.*,i.img,title,id
from post AS p LEFT JOIN images AS i ON i.post_id= p.id ")) {
$stmt->execute();
}
$result = $stmt->get_result();
if ($result->num_rows>0) {
$firstTime=true;
while ($row = $result->fetch_assoc()) {
?>
<?php
if($firstTime==true){
echo $row['title'];
$firstTime=false;
}
?>
<img src="images/<?php echo $row['img']; ?>"height="20"width="20"/>
<?php
}}
?>
Answer
Solution:
The column title will be repeated equal to the number of rows returned in the image table, you can not do anything to prevent it.
If you do group by title it will return the number of rows equal to the number of rows in the post table.
But if you elaborate more on what you actually want to do with the result I may help you.
Answer
Solution:
Try this please :-
<?php
$a1 = "";
$stmt = $con->prepare("SELECT p.*,i.img,title,id
from post AS p INNER JOIN images AS i ON i.post_id=p.id");
$stmt->execute();
$result = $stmt->get_result();
if ($result->num_rows>0) {
while ($row = $result->fetch_assoc()) {
$title = $row['title'];
$img = $row['img'];
}
if($a1!= $title){
$activity="";
$activity.="<tr>
<td>$title</td>
</tr>;
}
$activity.="<tr>
<td><img src="images/<?php echo $img ?>"height="20"width="20"/></td>
</tr>";
$a1=$title;
}
?>
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: uncaught mysqli_sql_exception
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.