How can I get the newest message from the person who sends a user a message?
Right now it's showing...
first_name last_name This is the newest message I sent you 2012-08-11 14:23:38
first_name last_name test 2012-08-11 14:20:35
<?php
echo '<div id ="inbox_name">Name</div><div id="inbox_message">Message</div><div id="inbox_time">Time</div><div id="inbox_line"></div>';
$query = mysql_query("SELECT * FROM `private_messages` WHERE `to_id`=$session_user_id ORDER BY `time_sent` DESC") or die("Error connecting to database. Please try again later.");
while ($row = mysql_fetch_array($query)) {
$to_id = $row['to_id'];
$from_id = $row['from_id'];
$message = $row['message'];
$time= $row['time_sent'];
$message_information = mysql_query("SELECT * FROM `sentrl_users` WHERE `id`=$from_id LIMIT 1");
while ($row_information = mysql_fetch_array($message_information)) {
$first_name = $row_information['first_name'];
$last_name = $row_information['last_name'];
$username = $row_information['username'];
echo '<div id="inbox_information"><div id="recieved_name">'. $first_name . ' ' . $last_name .'</div><div id="recieved_message">'. $message .'</div><div id="recieved_time">'. $time .'</div></div>';
}
}
?>
I tried using limit 1, but that didnt work. I'm trying to just get the newest message WITHOUT duplicating the first_name and last_name.
If what you want is ONLY ONE row, instead of
while ($row = mysql_fetch_array($query)) {
//EVERYTHING INSIDE THE ORIGINAL WHILE LOOP
}
you should write
while($row=mysql_fetch_array($query)){
$array[$row["from_id"]]=$row;
}
foreach($array as $row){
//EVERYTHING INSIDE THE ORIGINAL WHILE LOOP
}
Related
Hi I am building a blog using html and php and have run into a problem with my sql. In my blog I would like to show all the comments that have been put in by users in the comments section that have the same article ID. In my database I am saving these parameters via $_POST and a query ID, ArticleID, Comments. However only the last comment that has been inserted in the database with that articleID is showing up.
this is the code that I am using. Can anyone help me please?
if(isset($_POST['submit']))
{
$comment = htmlentities($_POST["comment"]);
$articleID = $_GET['artId'];
$query = "INSERT INTO tbl_comments (comment, ArticleID) VALUES ('$comment', $articleID)";
$result = mysqli_query($connection, $query) or die("Error in query: ". mysqli_error($connection));
}
$query1 = "SELECT * FROM tbl_comments WHERE ArticleID = $artId";
$result1 = mysqli_query($connection, $query1) or die("Error in query: ". mysqli_error($connection));
while($row = mysqli_fetch_assoc($result1))
{
$articleId = $row['ArticleID'];
$comment = $row['comment'];
}
if(isset($comment))
{
echo "<div class='comments'>";
if (isset($comment))
{
echo "<div class='commentName'>";
echo $comment;
echo "</div>";
}
change
while($row = mysqli_fetch_assoc($result1))
{
$articleId = $row['ArticleID'];
$comment = $row['comment'];
}
to:
$comment = '';
while($row = mysqli_fetch_assoc($result1))
$comment .= $row['comment'] . '<br/>';
So I have this PHP code:
Note: I do use mysqli_connect() further up.
$result = mysqli_query($con,"SELECT * FROM `smf_messages` WHERE `id_board` = 18");
if(!$result) {
echo "<center><p>Couldn't fetch news posts. Error code 2.</p></center>";
mysqli_close($con);
} else {
$posts = array();
$topicbdy = array();
while($row = mysqli_fetch_array($result,MYSQLI_ASSOC))
{
$posts[$row['id_topic']] = $row['id_topic'];
$topicbdy[$row['id_msg']] = $row['id_msg'];
}
$display = max($posts);
$display2 = min($topicbdy);
$qry = "SELECT * FROM `smf_messages` WHERE `id_board` = 18 AND `id_topic` = " . $display . " AND `id_msg` = " . $display2;
$result2 = mysqli_query($con,$qry);
//echo $qry;
if(!$result2) {
echo "<center><p>Couldn't fetch news posts. Error code 3.</p></center>";
} else {
while($show = mysqli_fetch_array($result,MYSQLI_ASSOC))
{
echo "<center><h1>" . $show['subject'] . "</h1></center><br /><br />";
echo "<center>" . $show['body'] . "</center><br />";
}
}
mysqli_free_result($result);
mysqli_free_result($result2);
mysqli_close($con);
It's supposed to get the latest topic out of the database for my SMF-based forum from the news board, by getting the highest topic id, but the lowest post id. It seems to be doing the query just fine, as I don't get any errors, but it doesn't show the subject or body. What should I do?
Your $result variable is wrong for second query fetch. For your second query
while($show = mysqli_fetch_array($result,MYSQLI_ASSOC))
Should be
while($show = mysqli_fetch_array($result2,MYSQLI_ASSOC))
^
I have a database which looks like so -
I am trying to fetch the top 10 entries based on time (entries with top 10 values in time column). I have the following code.
<?php
include_once("connect.php");
$sql = "SELECT * FROM scores order by time desc limit 10";
$query = mysql_query($sql) or die("systemResult=Error");
$counter = mysql_num_rows($query);
if($counter>0)
{
print("systemResult=Success");
$array = mysql_fetch_array($query);
foreach($array as $data)
{
$athleteName = $data["athleteName"];
$email = $data["email"];
$time = $data["time"];
$timeStamp = $data["timeStamp"];
$country = $data["country"];
print "&athleteName=" . $athleteName;
print "&email=" . $email;
print "&time=".$time;
print "&timeStamp=".$timeStamp;
print "&country=".$country;
}
}
else
{
print("systemResult=Error");
}
?>
The output I am getting is
systemResult=Success&athleteName=7&email=7&time=7&timeStamp=7&country=7&athleteName=7&email=7&time=7&timeStamp=7&country=7&athleteName=4&email=4&time=4&timeStamp=4&country=4&athleteName=4&email=4&time=4&timeStamp=4&country=4&athleteName=G&email=G&time=G&timeStamp=G&country=G&athleteName=G&email=G&time=G&timeStamp=G&country=G&athleteName=n&email=n&time=n&timeStamp=n&country=n&athleteName=n&email=n&time=n&timeStamp=n&country=n&athleteName=2&email=2&time=2&timeStamp=2&country=2&athleteName=2&email=2&time=2&timeStamp=2&country=2&athleteName=I&email=I&time=I&timeStamp=I&country=I&athleteName=I&email=I&time=I&timeStamp=I&country=I
As can be seen, the output I am getting is not what is on the table in database. I am getting wierd values. What am I doing wrong?
You don't need to use for each in your case, and if so, just print $data, try to remove foreach loop, and if you want to get all records, then, use while:
while($data = mysql_fetch_array($query))
{
$athleteName = $data["athleteName"];
$email = $data["email"];
$time = $data["time"];
$timeStamp = $data["timeStamp"];
$country = $data["country"];
print "&athleteName=" . $athleteName;
print "&email=" . $email;
print "&time=".$time;
print "&timeStamp=".$timeStamp;
print "&country=".$country;
}
try
while($data = mysql_fetch_array($query)) {
$athleteName = $data["athleteName"];
//...
i have this code im working on im trying to output this :
{
title:"<?php echo $sender_fullname; ?>",
mp3:"link",
},
using this in php to display it in javascript
//database include
require_once "db.php";
//get email from session
$email = $_SESSION['username'];
//fetch user fullname and id based on session
$name_query = mysql_query("SELECT fullname,id FROM users WHERE email = '$email'");
$name = mysql_fetch_object($name_query);
//fecth sender id, receiver id, audioclip, fullname and email
$query = "SELECT m.sender,m.receiver, m.audioclip, u.fullname, u.email
FROM `users` AS u
JOIN `messages` AS m ON m.receiver = u.id
WHERE u.email = '".$email."'";
$result = mysql_query($query);
here's the loop what should i do to output the same
while(
$row = mysql_fetch_assoc($result)) {
$sender = $row['sender'];
$sender_name_query = mysql_query("SELECT fullname FROM users WHERE id = '$sender'");
$sender_name = mysql_fetch_object($sender_name_query);
$sender_fullname = $sender_name->fullname;
echo '{<br/>title:".'$sender_fullname'.",<br/>mp3:"link",<br/>},';
}
you can do some like :
while(
$row = mysql_fetch_assoc($result)) {
$sender = $row['sender'];
$sender_name_query = mysql_query("SELECT fullname FROM users WHERE id = '$sender'");
$sender_name = mysql_fetch_object($sender_name_query);
$sender_fullname = $sender_name->fullname;
echo '{<br/>title:".'$sender_fullname'.",<br/>mp3:"link",<br/>},';
print "<script>alert(\"$sender_fillname\")</script>";
}
and then the code into the script tag run like javascript code if you wants puts the value of a php variable into a javascript variable you can do :
<?
$mivar = "hola mundo";
print "<script>";
print "var mivar = \"$mivar\"";
print "</script>";
?>
The best way to get php into javascript is to use JSON via php's json_encode.
$rows = array();
while($row = mysql_fetch_assoc($result)) {
$sender = $row['sender'];
$sender_name_query = mysql_query("SELECT fullname FROM users WHERE id = '$sender'");
$sender_name = mysql_fetch_object($sender_name_query);
$sender_fullname = $sender_name->fullname;
$row['sender'] = $sender_fullname;
$rows[] = $row;
}
echo "<script type='text/javascript'>";
echo "var rows = " . json_encode($rows) . ";";
echo "</script>";
The error is caused by the last line:
echo '{<br/>title:".'$sender_fullname'.",<br/>mp3:"link",<br/>},';
It should be this:
echo '{<br/>title:"' . $sender_fullname . '",<br/>mp3:"link",<br/>},';
Note 1
You can write this:
$sender_fullname = $sender_name->fullname;
echo '{<br/>title:"' . $sender_fullname . '",<br/>mp3:"link",<br/>},';
easily as:
echo '{<br/>title:"' . $sender_name->fullname. '",<br/>mp3:"link",<br/>},';
Note 2
Please, don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO, or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.
from http://brightmeup.info/comment.html
I am making an email script in php. What happens is a mysql query is made, and the output of this is stored in the following strings :
$personal1 = $userinfo->salutation;
$personal2 = $userinfo->surname;
$business = $userinfo->businessname;
Next I have an if statement, this checks to see if the surname is blank, if it is, it then substitutes the salutation + surname with the business name. The problem I am having is that the emails keep being sent out with Dear, Business Name , even if the surname field is not blank, I am not sure what I am doing wrong with the following code for it to do this though ?.
if ($personal2=="") {
$name = $business; }
else {
$name = $personal1 . ' ' . $personal2;};
EDIT >>>>>>>>>>
If I echo out the contents of the strings I get :
personal1 = Mr
personal2 = Johnson
business = Hat Trick Media
Edit 2 >>>>>>>
This is some of the code, it is then passed onto the mailer.
<?php
$cf_uid = $_GET['token'];
$query = mysql_query("SELECT * FROM hqfjt_chronoforms_data_addupdatelead WHERE cf_uid = '$cf_uid'") or die(mysql_error());
$userinfo = mysql_fetch_object($query);
$personal2 = $userinfo->surname;
$personal1 = $userinfo->salutation;
$business = $userinfo->businessname;
?>
<?php
$result = mysql_query("SELECT * FROM hqfjt_chronoforms_data_addemailtemplate");
while ($row = mysql_fetch_object($result)) {
echo '<tr class="table-row">';
echo '<th class="template-name">';
echo '<div class="namerow">';
$id = $row->cf_uid;
$form_id = $row->form_id;
$query = mysql_query("SELECT `$form_id` FROM email_history WHERE cf_id = '$user_id'") or die(mysql_error());
$datesent = mysql_fetch_object($query);
$date = $datesent->$form_id;
if ($personal2=="") {
$name = $business; }
else {
$name = $personal1 . ' ' . $personal2;};
Is your code a valid statement? Your code structure is awful. Instead of...
if ($personal2=="") {
$name = $business; }
else {
$name = $personal1 . ' ' . $personal2;};
Use
if ($personal2=="") {
$name = $business;
}
else {
$name = $personal1 . ' ' . $personal2;
}
You seem to have an extra ; that you dont need.
You also dont seem to close the while loop in the code you posted...
Ok, I have found out what the problem was, $name was coming in the session from the previous page and overwriting $name on this page, I have now set it to destroy the session before it loads this page and it seems to have sorted it now, thanks for everyone's help :-)