Reputation: 458
Hello first of all what i am doing in , i am coding a website for advertise .
Now what do i need is a help to display a lots of data from two tables of database .
What i have done so far u can check at My project you have to login use (Username : test , password : 123456a) to login , so there is everything is okay except an image image are the same on every ads and i do not find the way to make it right .
So i have a "posts" table with an information about ads and an "images" table with a path of an image this is how its looks like :
and this is my code :
<?php
$userid = $_SESSION["userid"];
$sql = "SELECT * FROM posts WHERE userid='$userid' ";
$res = mysqli_query($connect,$sql);
while ($row = mysqli_fetch_assoc($res)) {
?>
<div id="ads">
<div id="titlepic">
<a href=""><?php echo $row["title"]; ?></a><br>
<a href=""><img src="<?php echo $Photo[0]; ?>" height="100px;"></a>
</div>
<div id="managead">
<a href="">Edit</a><br style="margin-bottom: 5px;">
<a href="">Delete</a><br style="margin-bottom: 5px;">
<a href="">Renew</a>
</div>
<div id="dates">
<b>Date Added:</b> <?php echo date('m/d/Y', $row["dateadded"]); ?><br>
<b>Renew Date:</b> <?php if($row["renewdate"] > 0){ echo date('m/d/Y', $row["renewdate"]); } ?><br>
<b>Location:</b> <?php echo $row["location"]; ?><br>
<b>Price:</b> <?php echo $row["price"]; ?><br>
</div>
</div>
<hr width="100%">
<?php
so the question is how to extract and images from other table at the same time or how tu run two query at the same time and get an information from them
Upvotes: 0
Views: 297
Reputation: 41051
your SQL statement needs a JOIN
in order to include data from two tables in one query.
$sql = "
SELECT *
FROM posts p
JOIN images i
ON p.id = i.postid
WHERE p.userid='$userid'
";
this result set will be populated with all columns from both tables. now you can access path1
via:
<?php echo $row["path1"]; ?>
while this will work for all of your images, such as $row["path2"]
, $row["path3"]
, etc, keep in mind this is a bad design for a many-to-many relationship, so it should be normalized to include a linking table which would hold all of your images.
Upvotes: 1