I have joined two table images and post tables my script problem when i uploaded two or more images to image table post 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 title one but it repeating 5 time 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
}}
?>
<?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
}}
?>
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.
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;
}
?>
Related
I'm trying to make a website for people to post their cars for sale. I've done posting data into database. Everything works fine, but when they go to Member's page, it should show them added cars with photo, and edit button.
Here's my function to display added cars:
function get_user_auto($username) {
//extract from the database all the URLs this user has stored
$conn = db_connect();
$result = $conn->query("
SELECT
images.filename,
car_make.make,
car_model.model,
car_generation.name,
cars.year,
cars.price,
cars.car_id
FROM cars
LEFT JOIN car_make ON cars.make = car_make.make_id
LEFT JOIN car_model ON cars.model = car_model.model_id
LEFT JOIN car_generation ON cars.generation = car_generation.generation_id
LEFT JOIN images ON cars.car_id = images.car_id
WHERE cars.username = '$username';");
if (!$result) {
return false;
}
//create an array of the URLs
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$make = $row["make"];
$model = $row["model"];
$generation = $row["name"];
$year = $row["year"];
$price = $row["price"];
$filename = $row["filename"];
$imageURL = 'uploads/'.$filename;
?>
<img src="<?php echo $imageURL; ?>" alt="" width="100"/>
</div>
<div>
<?php echo "" . $make ." " . $model ." - " . $year ." " . $price ." USD";?>
Edit
</div>
</div>
<?php
}
} else {
echo "You did not add car for sale yet.";
}
}
So, i.e. I have added one car with 3 photos attached to it by car_id, when I retrieve data and echo it, it shows the same car 3 times with 3 different photos.
If you want only one row per car then you can add GROUP BY to query like this
GROUP BY car_id. This will allow only one row per car_id regardless of how many images car have (but it might not select image you like).
What I have here is a bulletin board in PHP with the ability to post comments and images. I am able to store my comments and my images to the database. My question involves 2 of my SQL tables, one for stored posts and the other table for storing images. When I echo out my table of posts I would like to echo out the corresponding image inside the post body. I have set up my tables to match with the column attribute of DATETIME but I fail to comprehend how to access my images' table while I am in a foreach loop with my posts table. I have included the code for my index.php file and my getImage.php file. Help would be greatly appreciated.
I made an update to my getImage file in the $ID = $_GET['id']; where 'id' needed to be lower case. I am now getting some images displayed in my table but some only partial. I think I am on my way. I know I need an if statement to determine if there is an image in the post before displaying. thanks for all the help.
<?php
include('connect.php');
$query = 'SELECT * FROM forumPosts LEFT JOIN PostImages ON
forumPosts.DATETIME = PostImages.ImageDATETIME ORDER BY replyIndex
ASC';
$statement = $db->prepare($query);
$statement->execute();
$posts = $statement->fetchAll();
$statement->closeCursor();
echo "<table>";
foreach ($posts as $post){
if ($post['post_type'] == "r"){
$post_id = $post['post_id'];
echo "<tr bgcolor='beige'><td>reply</td><td>".
$post['post_title'] . "</td ><td>". $post['post_body'] . "<img
src='getImage.php?id=".$post['ID']."'>". "</td><td>".
$post['DATETIME']. "</td><td>". $post['replyIndex']. "</td>
<td>".$post['post_type']."</td>";
?>
<?php
include('connect.php');
$ID = $_GET['id'];
$query = "SELECT * FROM PostImages WHERE ID=:ID";
$statement = $db->prepare($query);
$statement->bindvalue(':ID', $ID);
$statement->execute();
$row = $statement->fetch(PDO::FETCH_ASSOC);
header("Content-type: image/jpeg");
echo $row['image'];
?>
You probably want to use the JOIN syntax in your SELECT query. Something like:
$query = 'SELECT * FROM forumPosts LEFT JOIN postImages ON forumPosts.DATETIME = postImages.imageDATETIME ORDER BY replyIndex ASC'
Then you can get the image data from your foreach loop using $post['name'] and $post['image'].
The LEFT JOIN will still return all rows from the first table (forumPosts) even if there is no match on the second table (postImages).
Mysql Join Syntax
In Mysql database I have column name "product_id" and there is while statement code to check if row exist ...
If value of product_id exist, then it will show "note-icon.png" image, if not exit it will say "No" but the problem is that let say that in "product_id" has four product_id's SAME NUMBER and then result will SHOW FOUR images, my question is that how to write code to show ONE image instead of FOUR images!
<?php
$sql = new mysqli($mysql_hostname, $mysql_user, $mysql_password, $mysql_database);
$query = "SELECT * FROM notes WHERE product_id = $product_id";
$results = mysqli_query($sql, $query);
while ($row = mysqli_fetch_array($results, MYSQLI_ASSOC)){
if(isset($row['$product_id'])) {
echo "<p>No </p>";
} else {
echo '<img src="images/note-icon.png" width="16" height="16" />';
} }
?>
Select only one row:
MySQL:
SELECT * FROM notes WHERE product_id = ? LIMIT 1
SQL:
SELECT FIRST 1 * FROM notes WHERE product_id = ?
If you want to get all notes but draw one image per product you can do this:
$product_ids = array();
while ($row = mysqli_fetch_array($results, MYSQLI_ASSOC)){
if(!isset($row['product_id'])) {
echo "<p>No </p>";
} else {
if(!isset($product_ids[$row['product_id']])) {
$product_ids[$row['product_id']] = 1;
echo '<img src="images/note-icon.png" width="16" height="16" />';
}
}
}
Some code annotations:
$product_id in your query allowed, only if you are cast it to int before
else use placeholders (prepared statements)
if(isset($row['$product_id']))
What's this? think should be:
if(!isset($row['product_id']))
also you forgot to check $results, that it is not false, before using..
U can use grouping
SELECT * FROM notes WHERE product_id = ? GROUP BY product_id
I have 2 tables:
table 1) courses_cat (this one have a "title" field)
table 2) courses (this one have a "parent" field which I want use to "group", this "parent" is equal to "title"
So I need to put "graphically" the content of the table 2 inside the table 1.
Here my code (separated for both tables):
<?php
$query = $db->prepare('SELECT title, body, showeb, datapost FROM courses_cat WHERE showeb="si"');
$query->execute();
$query->store_result();
$query->bind_result($title, $body, $showeb, $datapost);
while ($query->fetch())
echo " <div class='pblock'><h2>$title</h2><pre>$body</pre></div> ";
?>
<?php
$query2 = $db->prepare('SELECT parent, title, body, showeb, datapost FROM courses WHERE showeb="si"');
$query2->execute();
$query2->store_result();
$query2->bind_result($parent, $title, $body, $showeb, $datapost);
while ($query2->fetch())
echo " <div id='courseblock'><a href='#'><span class='top'>$title</span><span class='content'>$shortbody</span></a></div>";
?>
I'm not sure I understand 100% what the end result should be, but I'm sure you can merge your two queries into one; looping over this properly might solve your issue:
SELECT cat.title as CatTitle, cat.body as CatBody, courses.title as CourseTitle, courses.body as CourseBody -- and any other fields you need like that
FROM courses_cat cat, courses
WHERE cat.parent = courses.title
GROUP BY cat.title
I try the code posted by Digital Chris, and it works (in part), for explain better, the follow code (see below the edited one) "group ok the categories and put the items inside" but I need to get more of one Course (div "courseblock") into each Category (div "pblock") and this code print just oneā¦(I think it "break the loop" and maybe need to JOIN the data of the "courses" into their "categories")
At this link can see an graphic example of what I mean:
http://liberartestudio.com/download/aaa.jpg
(the category blocks are those whites, the courses those red)
Here the link to the page:
http://lvdsys.liberartestudio.com/services_courses.php
<?php
$query = $db->prepare('
SELECT courses_cat.title AS CatTitle, courses_cat.body AS CatBody, courses_cat.datapost AS CatData, courses.parent, courses.title AS CourseTitle, courses.shortbody AS CourseShortbody, courses.datapost AS CourseData
FROM courses_cat, courses
WHERE courses_cat.title = courses.parent
GROUP BY parent
ORDER BY CatData AND CourseData
');
$query->execute();
$query->store_result();
$query->bind_result($CatTitle, $CatBody, $CatData, $parent, $CourseTitle, $CourseShortbody, $CourseData);
while ($query->fetch())
echo "
<div class='pblock'>
<h2>$CatTitle</h2><pre>$CatBody</pre>
<div id='courseblock'><a href='#'><span class='top'>$CourseTitle</span><span class='content'>$CourseShortbody</span></a>
</div>
</div>
"; ?>
Here my working code (simple version) for what I needed...
<?php
$cat = mysqli_query($db,"SELECT title AS catTitle, body AS catBody FROM courses_cat");
$result = mysqli_query($db,"SELECT parent, title, shortbody FROM courses");
//array x categories
$category = array(); while ($row = mysqli_fetch_assoc($cat)) {$category[] = $row;}
//array x courses
$course = array(); while ($row = mysqli_fetch_assoc($result)) {$course[] = $row;}
// here my divs
$cat_div="";
foreach ($category as $row) {
$cat_div = $row['catTitle'];
echo "<div class='pblock'>";
echo "<h2>".$row['catTitle']."</h2><pre>".$row['catBody']."</pre>";
foreach ($course as $row) {
if ($cat_div == $row['parent']) {
echo "<div id='courseblock'><a href='#'><span class='top'>".$row['title']."</span><span> class='content'>".$row['shortbody']."</span></a></div>";}}
echo "</div>";}
$cat->free_result();
$result->free_result();
mysqli_close($db);
?>
I have 2 tables
Photos: stores all photo information
Gallery: stores heading and description about the gallery
I need to echo to a new page the Gallery Description, then all the photos that should be there.
but I keep getting heading and description repeating because its in the same
$row = mysql_fetch_array($result)...
then there is more that one set of photos in that gallery I need also?
anyone help or am I being to vague....
$a="SELECT * from gallery where gallery_category=".$gallery_category;
$result = mysql_query($a,$c);
while($row = mysql_fetch_array($result)) {
echo $row['gallery_name'].'<br>'.$row['gallery_description'].'<br>';
$sql="SELECT * FROM photos WHERE gallery_id =".$gallery_id." ORDER BY photos_filename";
$result2 = mysql_query($sql,$c);
while($row = mysql_fetch_array($result2)) {
echo'<a rel="example_group" href="../galleries/images/'.$row['gallery_id'].'/'.$row['photos_filename'].'" width="130" height="100"><img src="../galleries/images/'.$row['gallery_id'].'/'.$row['photos_filename'].'" height="150px" alt=""/></a>';
}
}
Actually I'll post it up here so I can use some code formatting.
Option 1 - loop within a loop
$headerquery = $db->query("SELECT * FROM tbl1");
while ($headers= $db->fetchNextObject($headerquery )) {
echo "<table><tr><th>".$headers->GalleryName."</th></tr>"; // open table create header
$detailquery = $db->query("SELECT * FROM tbl2 WHERE GalleryID=".$headers->ID);
while ($details= $db->fetchNextObject($detailquery )) {
echo "<tr><td>".$details->Photo."</td></tr>"; //loop through 2nd table and spit out photos
}
echo "</table>"; //close table
}
Option 2 - joined query with selector
$galleryheaderid = 0;
$query = $db->query("SELECT * FROM tbl1 INNER JOIN tbl2 on tbl1.ID=tbl2.GalleryID");
while ($details = $db->fetchNextObject($query )) {
echo "<table>";
if ($galleryheaderid!=$details->ID) { //spit out header row if id's don't match
echo "<tr><th>".$details ->GalleryName."</th></tr>"; //create header
$galleryheaderid = $details->ID; //set header id to gallery id so we don't show header a 2nd time
}
echo "<tr><td>".$details->Photo."</td></tr>"; //spit out gallery information even on first row since all rows include photo information
echo "</table>"; //close table
}
Something like either of these should work obviously they'll need completing properly