how to view newest replies from my followed users - php

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

Related

foreach and or if array statement

I have a profile that shows profiles in a list. as shown in the image below.
users table
id | email | full_name | job_title | bio | profile_photo
images table
image_id | id | artist_img
CODE
<?php
$db = dbconnect();
$stmt = $db->prepare('SELECT
users.email,
users.full_name,
users.job_title,
users.bio,
users.profile_photo,
images.id,
images.artist_img
FROM users
INNER JOIN images ON users.id=images.id GROUP BY images.id');
$stmt->execute();
$result = $stmt->get_result();
while (($row = mysqli_fetch_assoc($result)) != false) {
$id = $row['id'];
$full_name = $row['full_name'];
$email = $row['email'];
$job_title = $row['job_title'];
$bio = $row['bio'];
$ProfilePhoto = $row['profile_photo'];
$artist_img = $row['artist_img'];
if (isset($ProfilePhoto) && ! empty($ProfilePhoto)) {
$image = "$ProfilePhoto";
} else {
$image = "avatar.jpg";
}
echo "<div class='container team-wrap'>
<div class='row'>
<div class='col-md-6'>
<img class='img-responsive' src='artist/$image'>
</div>
<div class=\"col-md-6\">
<strong>$full_name<br>$job_title</strong>
<br>
<p>$bio</p>
<a href='mailto:$email' class='btn btn-info'>Contact Me</a>
</div>
</div>
</div>
<div class=\"container space team-wrap\">
<div class=\"row\">
<div class=\"col-lg-12\">
<div id=\"gallery-slider\" class=\"slider responsive\">
<div>";
echo"
<img src=\"gallery/$artist_img\" alt=\"\"></a>";
echo "</div>
</div>
<hr>
</div>
</div>
</div>";
}
?>
Problem area
echo"<img src=\"gallery/$artist_img\" alt=\"\"></a>";
The issue I am having is that it repeats the profile for each image if the user has 5 images it will add 5 profiles 1 for each img.
and does not show the other users profile at all. show how it shows up is look at the image for example its got 4 images under profile 1 and it shows there profile pic.. well it repats all that info for each image I want the pics that have the same id as the user to show up as a slider like below..
and it also refuses to show the other profiles of other users.
yes because it is not seeing the variable because you echo it as text
echo"<img src=\"gallery/$artist_img\" alt=\"\"></a>";
should be
$r=0;
foreach ($images as $image.id){
[$artist_img=$images[$r];
echo "<img src=\"gallery/".$artist_img."\" alt=\"\"></a>";
$r++;
}
//
i don't like to echo to much html because very easy to make a mistake that way i prefer it is to stay in html and just echo my variable like the but that is just me
<html>
<body>
<a><img src="gallery/<? php echo $artist_img; ?>" alt=""></a>
</body>
</html>
do you see here you fetch the variable for the picture as single row
$artist_img = $row['artist_img'];
you need to make it a array because it is a array you must remember using inner generates a number of pieces of data, maybe for you it will be better to run 2 query second query loads the images.inner join is useful for some people but complex and dont really give any advantage because still searches the whole tables twice to get the results
something like this might work for for you
$artist_img2=array();
//because there are more then one piece of data in it
$artist_img2 = $row['images'];
//then you need to do another loop to put the data in variables or echo them out
// in the loop
//note the row refers to id and not image.id because in the inner array the key will be id
$artist_img3=row2['id'];
echo "<img src=\"gallery/".$artist_img3."\" alt=\"\"></a>";
//end loop

Two query at the same time

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.

Query in withing query using result

I've had some problems in regards to how to solve the problem of having a query based on the results of the within query, the looping one. I've tried all the ways I know, but it aborts the while loop and breaks.
It just shows the result from the query within, which tells me that the within-query DOES work, but it won't paste its information nor will it paste the other elements (such as reputation and its style).
$sql4 = mysqli_query($con, "SELECT * FROM as_sound_comments WHERE sound_id='$soundid'");
if(mysqli_num_rows($sql4) > 0){
while($row = mysqli_fetch_assoc($sql4)) {
echo "<div class='comment-content-full'>
<div class='comment-info'>
<table style='width: 98%'>
<td>
<table>
<td><p class='item-comment-info-name'><a class='blink' href='#'>";
$comment_poster_id = $row['poster_id'];
$sql5 = mysqli_query($con, "SELECT * FROM as_account WHERE id='$comment_poster_id'");
$innerrow = mysqli_fetch_assoc($sql5);
$comment_poster_username = $innerrow['username'];
$comment_poster_reputation = $innerrow['reputationlevel'];
echo $comment_poster_username;
"</a></p></td>
<td><div class='item-comment-info-reputation nlgreen'>8</div></td>
</table>
</td>
<td style='width: 20%'>
<p class='item-comment-info-posttime'><abbr class='timeago' title='< ". $row['added'] . "'></abbr></p>
</td>
</table>
</div>
<div class='comment-content-avatarandbox'>
<div class='comment-avatar'>
<img class='item-user-info-comment-avatar' src='images/test_avatar.jpg' alt='Avatar'>
<div class='arrow-left'> </div>
<div class='arrow-left-behind'> </div>
</div>
<div class='comment-box'>
<p class='comment-box-text'>" . $row['comments'] . "</p>
</div>
</div>
</div>";
}}else{ echo "sorry we didn't find no nothing";}
answering, based off comments, to close the question. Anytime that you are doing a query in a query loop, you could use a JOIN to do 1 query -
$sql4 = mysqli_query($con, "SELECT * FROM as_sound_comments LEFT JOIN as_account ON as_account.id = as_sound_comments.poster_id WHERE as_sound_comments.sound_id='$soundid'");

"if message/text exists in database", then show the message that the user created from latest to oldest

I'm working on this message system. The user has a form presented to him, where the user will type notes, in this case called a "message". When the user visits his profile page, he is shown the message that he had typed in the form earlier and pressed saved.
Screenshot:
http://i.stack.imgur.com/VEsDg.png
My database has a table called "messages" where I have columns pid, uid, message, time, and picture where the uid is the uid in my "users" table.
So the pid is a auto-incrementing and the uid is the user id of the user who posted the message.
(The user posts a URL to his picture in the form which is later saved to the picture column in the database.
My PHP code where I get data from the database:
<?php
$uid = $this->session->userdata('uid');
$this->db->limit(10); //Use this to limit the entires while you show the entires on the front page.
$query = $this->db->query("SELECT pid, message, time, picture FROM messages WHERE uid = '$uid';");
foreach ($query->result_array() as $row)
{
$message = $row['message'];
$time = $row['time'];
$picture = $row['picture'];
}
?>
My html code in my profile_body.php page in my views folder where I have the above's code included.
<div class="list-group list-group-breakout">
<a class="list-group-item" href="https://assembly.com/assemblycoins">
<div class="chip">
<div class="chip-icon">
<img alt="Coins" width="48" height="48" class="app-icon" src="<?php echo $picture ?>"/>
</div>
<div class="row">
<div class="col-sm-12">
<p class="omega"><?php echo $message;?></p>
</div>
<div class="col-sm-12 right-align">
<p class="gray-2 small omega">
Feb. 21, 2015 <!-- Not from database -->
</p>
</div>
</div>
</div>
How would I get only 10 results for the messages that the users typed in the database and display it? I have tried to paste the same html code again but it displays the same message and picture.
Thanks.
Here is some improvements to do :
$uid = $this->session->userdata('uid');
$result = $this->db->select('pid, message, time, picture')
->where(array('uid' => $uid))
->limit(10)
->get('messages')
->result_array();
$this->load->view('profile_body', $result);
In your view profile_body.php file :
<?php foreach($result as $row): ?>
<div class="list-group list-group-breakout">
<a class="list-group-item" href="https://assembly.com/assemblycoins">
<div class="chip">
<div class="chip-icon">
<img alt="Coins" width="48" height="48" class="app-icon" src="<?php echo $row['picture'] ?>"/>
</div>
<div class="row">
<div class="col-sm-12">
<p class="omega"><?php echo $row['message'];?></p>
</div>
<div class="col-sm-12 right-align">
<p class="gray-2 small omega">
Feb. 21, 2015 <!-- Not from database -->
</p>
</div>
</div>
</div>
<?php endforeach; ?>

Entire page repeating with PHP [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
I've ran an SQL query to obtain reviews relating to a specific film as part of a film review site.
I've managed to obtain the required results, however the entire top section of the webpage now duplicates in between every record.
I'm really not sure why this could be as I'm a complete beginner with PHP.
I assume it must be a simple syntax problem but I can't see it.
Any help would be great! Thank you.
<!DOCTYPE html>
<?php
session_start();
try{
$conn = new PDO('mysql:host=localhost;dbname=xxx','xxx','xxx');
} catch(PDOException $e) {
echo $e->getMessage();
}
$result = $conn->query("
SELECT *
FROM FILM F, GENRE G, REVIEW R, USER U
WHERE F.GENRE_ID = G.GENRE_ID
AND F.FILM_ID = R.FILM_ID
AND U.USER_ID = R.USER_ID
AND R.FILM_ID = 128
");
$result->setFetchMode(PDO::FETCH_ASSOC);
while ($row = $result->fetch()){
?>
<html>
<head>
<title><? echo $row['TITLE']; ?></title>
<link href="CSS.css" rel="stylesheet" type="text/css">
<link rel="shortcut icon" href="favicon.ico">
<meta name="description" content="A unique, ground-breaking website for all things relating to classical cinema. The Classic revolutionises the way that we see classic cinema, and provides the movie goer with an opportunity to find all the reviews they need!">
<meta name="author" content="Stefan Batterbee">
<meta charset="UTF-8">
</head>
<body>
<div id="page">
<header>
<?php
if(isset($_SESSION['Logged_In']))
{
echo '<br><br>';
echo 'You are logged in!<br>';
echo '<a href="logout.php">
Click here to log out.</a>';
}
else
{
echo '<br>';
echo 'You are not logged in!<br>';
echo 'Click here to log in,<br>';
echo 'or click here to register.';
}
?>
</header>
<nav>
<ul id="navigation">
<li>H O M E </li>
<li>F I L M R E V I E W S </li>
<li>A R T I C L E S</li>
<li>A B O U T U S</li>
</ul>
</nav>
<div id="breadcrumbs">
<a class="link" href="index.php">Home</a> > <a class="link" href="genres(list).php">Reviews</a> > <? echo '<a class="link" href="'.$row ['GENRE_TYPE'].'(list).php">'; echo $row ['GENRE_TYPE']; echo '</a>' ?> > <? echo '<a class="link" href="'.$row ['FILM_ID'].'.php">'; echo $row ['TITLE']; echo '</a>' ?>
</div>
<div id="filminfo">
<img class="greyshadow" src="Images/2001aspaceodyssey.jpg" width="200" height="200" alt="2001" longdesc="Images/2001aspaceodyssey.jpg">
<div id="filminfotext">
<h1><span itemprop="itemreviewed"><? echo $row['TITLE']; ?></span></h1>
<br>
<table width="500" height="80" border="0">
<tr>
<td>Genre:</td>
<td><? echo $row['GENRE_TYPE']; ?></td>
</tr>
<tr>
<td>Release Year:</td>
<td><? echo $row['RELEASE_YEAR']; ?></td>
</tr>
<tr>
<td>Starring:</td>
<td><? echo $row['LEAD_ACTOR']; ?></td>
</tr>
<tr>
<td>Directed by:</td>
<td><? echo $row['DIRECTOR']; ?></td>
</tr>
<tr>
<td>Running Time:</td>
<td><? echo $row['RUNNING_TIME']; ?></td>
</tr>
</table>
</div>
</div>
<div id="viewreviews">
<h2>P R E V I O U S U S E R R E V I E W S</h2>
<span itemscope itemtype="http://data-vocabulary.org/Review-aggregate">
<span itemprop="itemreviewed">2001: A Space Odyssey</span> - Classic Film Reviews
<span itemprop="rating" itemscope itemtype="http://data-vocabulary.org/Rating">
Rating:
<span itemprop="average">9.2</span> out of
<span itemprop="best">10</span>
</span>
based on
<span itemprop="count">6</span> reviews.
</span>
</div>
<div id="synopsis">
<?php
echo '<table width="760" border="0">';
echo '<tr>';
echo '<td rowspan="2">';
echo $row['RATING'];
echo "</td>";
echo '<td>';
echo $row['USERNAME'];
echo '</td>';
echo '</tr>';
echo '<tr>';
echo '<td>'; echo $row['COMMENTS']; echo '</td>';
echo '</tr>';
echo '</table>';
}
$conn = null; ?>
<center><img src="Images/review_button.jpg"></center>
</div>
<footer>
<p class="textleft">Created by Stefan Batterbee (2013)</p>
<p class="textright">Click <a class="link" href="https://www.facebook.com/the.classic.cinema.reviews">HERE</a> to access our Facebook page.</p>
</footer>
</div>
</body>
</html>
You are starting your while loop before rendering the header. Move the PHP where you start the loop to the location where you want to start the loop.
Replace this part of your code:
$result->setFetchMode(PDO::FETCH_ASSOC);
while ($row = $result->fetch()){
to:
$row = $result->fetchAll(PDO::FETCH_ASSOC);
There is no reason to loop through the query results and this is the answer to your question of why it's duplicates in between every record.
You have a query that returns data from tables with different items count, this is influencing the results returned. To understand better, var_dump($row) and forget the html for now: you're getting:
TITLE - RELEASE_YEAR - GENRE_TYPE - RATING - USERNAME - COMMENT
all in the same row, repeated times the number of comments. And you're using the single $row to output the whole page. Incidentally, should you have no reviews, you wouldn't even see the page once.
You should
remove the REVIEW table from the first query, and remove the while loop: this is a movie page, you only want to render the movie information once
run another query just before the table where you outupt the RATING (the one 760px wide), and query only the REVIEW table. Then run a while loop to print out all the ratings.
Use different variable names ($ratingrow for example) so you don't lose your mind.

Categories