Two query at the same time - php

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">
<?php echo $row["title"]; ?><br>
<img src="<?php echo $Photo[0]; ?>" height="100px;">
</div>
<div id="managead">
Edit<br style="margin-bottom: 5px;">
Delete<br style="margin-bottom: 5px;">
Renew
</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

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.

Related

how to view newest replies from my followed users

i am creating a following system in my website.
now when a user follows lets say three users and each user has lets say 3-4 messages.
when one of the users replies to a new message that has been sent to him i want this message to be the top message to be displayed.
what i am getting now is that the messages will go in stacks for each user even if it is a new message.
here is my php and queries:
$uid=$_SESSION['active_user_id'];
$usersFollowingsQuery = $db->prepare("SELECT mr.*, f.* FROM messages_reply mr INNER JOIN follow f on mr.from_id = f.followed_id
WHERE follower_id = ? ORDER BY follow_id desc");
$usersFollowingsQuery->bindValue(1,$uid);
$usersFollowingsQuery->execute();
if($usersFollowingsQuery->rowCount()==0){
echo"<div class ='forgot'>Follow users to find their content here</div>";
}
else{
echo"<div class ='forgot'>This is your feed</div>";
while($row3 =$usersFollowingsQuery->fetch()){
$followed_id = $row3['followed_id'];
$usersPicQuery = $db->prepare("SELECT * FROM users WHERE id=?");
$usersPicQuery->bindValue(1,$followed_id);
$usersPicQuery->execute();
$row2 = $usersPicQuery->fetch();
$d=$row['date'];
?>
<div Class="inside-card" style="width:95%; margin-top:24px;"
<td>
<div class="msg-body">
</a>
</img>
<p style="font-size:16px; color:#696969"><?php echo $row3['question']; ?></p>
<div class="msg-action">
<div>
<a href=<?php echo "'q=" . $row2['username'] . "'" ;?>> <img src=<?php echo "'" . $row2['picture'] . "'";?> width="32px" height="32px" >
<p ><?php echo "" . $row2['username'] . "" ;?></p></a>
<span class="msg-icon" ><?php echo timeAgo($d); ?></span>
</div>
<p ><?php echo $row3['answer']; ?></p>
</div>
</td>
</tr>
</div>
<?php
}
}
echo"</div>";
how can i modify my query such that whenever one of my followers replies to a new message it becomes the first message to be displayed regardless of who i followed first?
I solved the problem, only needed to add order by date desc

how to pick a category name if i chose a news of that category

I am setting a query where i chose an ID of news to show that specific id news, i have a foreign key of category table as category ID in news table, now i want that if i show a news i show title of category also.
I have tried picking up the category id which show only the id and i want to show the title of that category.
<?php
if(isset($_GET['news_id'])){
$the_news_id = $_GET['news_id'];
}
$query = "SELECT * FROM news_title WHERE news_id = '$the_news_id'";
$select_all_news_query = mysqli_query($connection,$query);
while($row= mysqli_fetch_assoc($select_all_news_query)){
$title = $row['news_title'];
$description = $row['news_description'];
$image = $row['news_image'];
$news_cat_title= $row['news_cat_id'];
?>
<h1 class="page-header">News Page</h1>
<!-- First Blog Post -->
<h2>
<?php echo $title;?>
</h2>
<img class="img-responsive" src="image/<?php echo $image; ?>"
alt="abc">
<hr>
<p><?php echo $description; ?></p>
<hr>
<?php }
?>
<!-- Second Blog Post -->
<hr>
<div class="container">
<div class="row">
<h1 class="page-header">
<!--This is where i want to show title but i am getting the ID--!>
<?php echo $news_cat_title; ?>
</h1>
</div>
</div>
i expect to get the title but i am getting a number.
Without knowing the schemas for the various tables it is hard to say for sure if I interpreted the question correctly but I assume what you are trying to do is join two tables? If you can you add relevant table schemas it would help.
select * from news_title t
left outer join category c on c.category_id=t.category_id
where news_id = '$the_news_id'
Based upon your last comment that the category table is simply called category and the foreign key is cat_id
select * from news_title t
left outer join category c on c.cat_id=t.cat_id
where news_id = '$the_news_id'
Then, when displaying the recordset you should be able to:-
echo $row['cat_title'];
An easy way ( on windows ) to capture and display the table schema:
Open cmd prompt
type mysql -u root -p <DBNAME> ~ changing for the actual database!
type describe <TABLE>; - press return
copy displayed info and paste into text editor
Repeat for all tables of relevance
Please try this code :
<?php
if(isset($_GET['news_id'])){
$the_news_id = $_GET['news_id'];
}
$query = "SELECT N.*, NC.cat_id AS news_cat_id
FROM news_title AS N
LEFT JOIN news_category AS NC
ON N.cat_id = NC.cat_id
WHERE news_id = '$the_news_id'";
$select_all_news_query = mysqli_query($connection,$query);
while($row= mysqli_fetch_assoc($select_all_news_query)){
$title = $row['news_title'];
$description = $row['news_description'];
$image = $row['news_image'];
$news_cat_title= $row['news_cat_id'];
?>
<h1 class="page-header">News Page</h1>
<!-- First Blog Post -->
<h2>
<?php echo $title;?>
</h2>
<img class="img-responsive" src="image/<?php echo $image; ?>" alt="abc">
<hr>
<p><?php echo $description; ?></p>
<hr>
<?php } ?>
<!-- Second Blog Post -->
<hr>
<div class="container">
<div class="row">
<h1 class="page-header">
<!--This is where i want to show title but i am getting the ID--!>
<?php echo $news_cat_title; ?>
</h1>
</div>
</div>
The query statement based on your question and comments is maybe like this :
$query = "SELECT N.*, NC.cat_id AS news_cat_id
FROM news_title AS N
LEFT JOIN news_category AS NC
ON N.cat_id = NC.cat_id
WHERE news_id = '$the_news_id'";

Inserting PHP database into HTML table

Hey I've recently been making a website and want to display the data from my database in a grid format opposed to it just listing down the page.
Here is my code right now:
<p>
<a href="pokemondetails.php?dex=<?php echo $row['dex'];?>">
<?php echo $row['name']; ?>
<br>
<img src="assets/<?php echo $row['dex']?>.png">
</a>
</p>
I was wondering how I would go about creating a for loop to allow the data from this database in conjunction with the image to span across the page with 7 columns and however many rows down until it reaches the end of the database.
Thanks!
<?php
$query = "Select * from tablename";
$bind = $conn->query($query);
if ($bind->num_rows > 0){
while ($row = $bind->fetch_assoc()){
?>
<p>
<a href="pokemondetails.php?dex=<?php echo $row['dex'];?>">
<?php echo $row['name']; ?>
<br>
<img src="assets/<?php echo $row['dex']?>.png">
</a>
</p>
<?php
}
}
?>
Try this, I just add while loop until End Of file (EOF table)

How to loop through rows for data to display multiple results?

I have searched many forums including Stack Overflow and not been able to find a solution for this that works. Please do not mark this as duplicate.
I have a database of users with multiple entries for certain items. The users are saved in one table ("TABLE 1") and the items are saved in another ("TABLE 2"). What I need the code to do is search TABLE 2 for user IDs that match the selected user from TABLE 1 and then for each matching entry display the item data for that row. The code I currently have is below but it is only displaying one result and then stopping. Any help is greatly appreciated.
<?php
$queryItems = mysql_query("SELECT * FROM items WHERE user_id='$results[id]'");
$itemmatch_result = mysql_fetch_array($queryitems) or die($itemmatch."<br/><br/>".mysql_error());
{
?>
<div class="column6">
<div class="item-container">
<p class="itemname"><?php echo $itemmatch_result['item_make'] . " BRAND " . $itemmatch_result['item_type']; ?> item</p>
<p class="itemsize"><strong>Item Size:</strong> <span><?php echo $itemmatch_result['item_size']; ?></span></p>
<p class="tiresize"><strong>Item Model:</strong> <span><?php echo $itemmatch_result['item_model']; ?></span></p>
<p class="tiresize"><strong>Registered:</strong> <span><?php echo date('F jS, Y', $itemmatch_result['item_registered']); ?> AT STORE</span></p>
<span class="purchase-data">BOUGHT <?php echo date('F jS, Y', $itemmatch_result['item_bought']); ?></span>
</div>
<?php
}
unset($itemmatch_result);
?>
You need to loop through all records as long as the query is giving you the records you expect.
while ($data = mysql_fetch_array($queryItems)) {
echo '
<p class.........>' . $data['item_make'] . ' BRAND .....
';
}
Btw, you shouldn't be using mysql anymore as it's deprecated, I would suggest starting to use mysqli/pdo instead.

Looping through PHP database with columns that link to specific profiles

I have a website where I am getting information of college student profiles on a database and displaying it as a linked collection. When I'm looping through the database I want to give each row a specific link to the profile of the student. Right now I am linking it to "profilePage.html" with generic information but I want it to be correlated with the row the user chose on the last(college) page.How do I save/transfer that information to the page. I do not want multiple profile pages but one template that is filled with the user previous choice.
<?php
$result = mysql_query("SELECT * FROM student_info WHERE college='Boston College'", $db);
if (!$result) {
die("Database query failed: " . mysql_error());
}
while ($row = mysql_fetch_array($result, MYSQL_BOTH)) {
?>
<a href="profilePage.html" class="collection-item">
<div class="row summary">
<div class="col s4 center">
<img class = "profile-pic" src="img/defaultProfile.jpg">
</div>
<div class="col s8">
<div class="title"> <?php echo $row[student_Name]; ?> </div>
<div class="black-text condensed thin"><i class="tiny material-icons">today</i> Founder, CEO at Linkle since January 1st, 2015</div>
<div></div>
</div>
</div>
</a>
<?php } ?>
Key thing, my urls are mysite.com/college.php and have no id's to specify them.
Structure of the Database student_info:
Shows the structure of the database
First, do you have an URL Rewriting ? If not, your target page should be a PHP page, like profilePage.php.
Your link to this page have to include a parameter (Query String) which is, for example, the student's ID :
<a href="profilePage.php?id=<?php echo $row['id'] ?>" class="collection-item">
This type of URL will be generated: profilePage.php?id=36
In profilePHP.php, retrieve the parameter in the Query String :
<?php
$idStudent = mysql_real_escape_string($_GET['id']);
?>
mysql_real_escape_string() is really important, it prevents SQL injections.
After that, you could retrieve the student's informations with a SQL query.
<?php
$idStudent = mysql_real_escape_string($_GET['id']);
$sql = sprintf("SELECT * FROM student_info WHERE id = %s", $idStudent);
$query = mysql_query($sql);
$student = mysql_fetch_object($query);
// you should check if the ID exists, and if there is 1 result
?>
<p>Student name is <?php echo $student['student_Name'] ?></p>
A little advice: mysql_query() will disappear soon, you should take a look at PDO.

Categories