my problem is that I want to display the messages between two users and order them by time.
I nmanaged to display the messages from each user but it displays first for the user George and the for user Niklakis as it shows in the image below...
What I want is to display both of the messages order by time.
This is part of my code...
$query = "SELECT * FROM messages WHERE recip='$view' ORDER BY time DESC";
$query2 = "SELECT * FROM messages WHERE recip='$username' ORDER BY time DESC";
$result = queryMysql($query);
$result2 = queryMysql($query2);
$num = mysql_num_rows($result);
$num2 = mysql_num_rows($result2);
for ($j = 0 ; $j < $num ; ++$j)
{
$row = mysql_fetch_row($result);
if ($row[3] == 0 || $row[1] == $username || $row[2] == $username)
{
echo date('M jS \'y g:ia:', $row[4]);
echo " <a href='messages.php?view=$row[1]'>$row[1]</a> ";
if ($row[3] == 0)
echo "wrote: "$row[5]" ";
else echo "private message: <span class='whisper'>" .
""$row[5]"</span> ";
if ($row[2] == $username)
echo "[<a href='messages.php?view=$view" ."&erase=$row[0]'>erase</a>]";
echo "<br>";
}
}
for ($j = 0 ; $j < $num2 ; ++$j)
{
$row2 = mysql_fetch_row($result2);
if ($row2[3] == 0 || $row2[1] == $username || $row2[2] == $username)
{
echo date('M jS \'y g:ia:', $row2[4]);
echo " <a href='messages.php?view=$row2[1]'>$row2[1]</a> ";
if ($row2[3] == 0)
echo "wrote: "$row2[5]" ";
else echo "private message: <span class='whisper'>" .
""$row2[5]"</span> ";
if ($row2[2] == $username)
echo "[<a href='messages.php?view=$view" ."&erase=$row2[0]'>erase</a>]";
echo "<br>";
}
}
I can understand that my problem happens because I have two different for loops and I probably need only ONE for loop to do this work. But I tried and I could find a way to do this.
Can you help?
Thank you.
You use the same code for both query results (tip for the future: PHP supports something like functions :D try it out) and your queries have the same structure so why don't you just fecht all entries for both users and sort them? Have you tried this out?
$query = "SELECT * FROM messages WHERE (recip='$username' AND auth='$view') OR (recip='$view' AND auth='$username') ORDER BY time DESC";
Related
i am retrieving results from MySQL with PHP based on user search
i want to add a automatically after every 2 s in the result
<?php
include_once("config.php");
isset( $_REQUEST['name'] ) ? $name=$_REQUEST['name'] : $name='';
$name = mysql_real_escape_string( $name );
if (strlen($name) >= 4) {
$sql = "select * from places where speciality like '%$name%'";
$rs = mysql_query( $sql ) or die('Database Error: ' . mysql_error
$num = mysql_num_rows( $rs );
if($num >= 1 ){
echo "<table id='result-table'><tr>";
while($row = mysql_fetch_array( $rs )){
echo "<td>row[cname]</td>"; //here i want to add </tr><tr> after 2 <td>s
}
}else{
echo "no records found</table>";
}
}
?>
Used $i = 1; if ($i % 2 == 0) and $i++;
<?php
include_once ("config.php");
isset ($_REQUEST['name']) ? $name = $_REQUEST['name'] : $name = '';
$name = mysql_real_escape_string($name);
if (strlen($name) >= 4) {
$sql = "select * from places where speciality like '%$name%'";
$rs = mysql_query($sql) or die('Database Error: '.mysql_error $num = mysql_num_rows($rs);
if ($num >= 1) {
$i = 1;
echo "<table id='result-table'><tr>";
while ($row = mysql_fetch_array($rs)) {
echo "<td>row[cname]</td>"; //here i want to add </tr><tr> after 2 <td>s
if ($i % 2 == 0)
echo "</tr><tr>";
$i++;
}
}
else {
echo "no records found</table>";
}
}
?>
echo "<table id='result-table'><tr>";
$currentCount = -1;
while($row = mysql_fetch_array($rs))
{
echo "<td>row[cname]</td>";
$currentCount = ($currentCount + 1) % 2;
if($currentCount == 1)
{
echo '</tr><tr>';
}
}
I suggest not putting them into a table at all; instead simply put them into <div> elements, with float:left and width:50% styles.
If you must put them into a table, and you want to do it the way you asked, you can use PHP's modulo operator (%) to divide the current record number by 2 and get the remainder. If it's 1, then add the <tr> tag:
if(++$rowNum % 2) {print "</tr><tr>";}
I am trying to add real-time notifications to my website using PHP, Ajax, JS.
It requests the PHP file fine and loads it onto my page so there is nothing wrong there, however the PHP is returning an incorrect number of rows and there doesn't seem to be any problems in the file so I am really confused.
Here is my PHP:
<?php
include("config.php");
$query = "SELECT * FROM notifications WHERE user = '$myUser' AND isread = '0'";
$result = mysql_query($query);
$num = mysql_num_rows($result);
if($num == 0){
print "";
} else if($num !== 0){
print "<span class='notification'> " . $num . " </span>";
}
?>
If there aren't any rows returned, then it echoes "", however there is a notification set for my user session to test and it is marked as unread. It loads perfectly with the exact same query if used on the page itself, so don't know what's going on at all here.
This line should be like:
else{
print "<span class='notification'> " . $nNum . " </span>";
}
?>
Or
else if($num != 0){
print "<span class='notification'> " . $num . " </span>";
}
?>
------------------EDITED---------------------------
Please do an echo to this:
<?php
include("config.php");
$query = "SELECT * FROM notifications WHERE user = '$myUser' AND isread = '0'";
$result = mysql_query($query);
$num = mysql_num_rows($result);
echo $query; //THIS and copy that and put this directly on your mysql manager
if($num == 0){
print "";
} else if($num != 0){
print "<span class='notification'> " . $num . " </span>";
}
?>
PS: Dont use mysql extension...go for mysqli or PDO...your code is vulnerable to sql injection
Now, the program is working fine to an extent, in that when mysql_affected_rows is more than 0, it does indeed add the data to the new table and print out the relevant echo message.
However, when mysql_affected_rows = 0, I get nothing, no error message, but absolutely no output at all.
I've stripped the code back, do any of you have any idea, i've looked at brackets, and closing conditions etc and can't work out why!
Code
$query10 = ("SELECT p.surname, p.passNo, p.activeUntil FROM PASSENGER p WHERE p.activeUntil < DATE_ADD(NOW(),INTERVAL -1 DAY)");
$result = mysql_query($query10);
while($row = mysql_fetch_array($result))
{
$surname = $row['surname'];
$passNo = $row['passNo'];
mysql_query("INSERT INTO ARCHIVED_PASSENGER (surname, passNo) VALUES ('$surname', '$passNo') ")
or die(mysql_error());
if (mysql_affected_rows()>0) {
echo '<p>';
echo "The number of rows affected by this update is: ";
echo mysql_affected_rows();
}
if (mysql_affected_rows()<1) {
echo '<p>';
echo "No records were affected. Taking you back to the control panel.";
}
}
I would advice to add a if($result != false && mysql_num_rows($result) > 0) just before the while loop. Like so:
$query10 = ("SELECT p.surname, p.passNo, p.activeUntil FROM PASSENGER p WHERE p.activeUntil < DATE_ADD(NOW(),INTERVAL -1 DAY)");
$result = mysql_query($query10);
if($result != false && mysql_num_rows($result) > 0)
{
while($row = mysql_fetch_array($result))
{
$surname = $row['surname'];
$passNo = $row['passNo'];
$query11 = mysql_query("INSERT INTO ARCHIVED_PASSENGER (surname, passNo) VALUES ('$surname', '$passNo') ")
or die(mysql_error());
if ($query11 != false && mysql_affected_rows()>0) {
echo '<p>';
echo "The number of rows affected by this update is: ";
echo mysql_affected_rows();
echo '</p>';
}
if ($query11 == false || mysql_affected_rows()<1) {
echo '<p>';
echo "No records were affected. Taking you back to the control panel.";
echo '</p>';
}
} else {
//nothing was retrieved, give some error!
}
}
I am working on a lead management system - and as the database for it grows the need for more bulk functions appears - and unfortunately I am getting stuck with one of them. The database stores many different leads - with each lead being assigned to a specific closer; thus the database stores for each lead the lead id, name, closer name, and other info. The main lead list shows a checkbox next to each lead which submits the lead id into an array:
<input type=\"checkbox\" name=\"multipleassign[]\" value=\"$id\" />
Now this all goes to the following page:
<?php
include_once"config.php";
$id = $_POST['multipleassign'];
$id_sql = implode(",", $id);
$list = "'". implode("', '", $id) ."'";
$query = "SELECT * FROM promises WHERE id IN ($list) ";
$result = mysql_query($query);
$num = mysql_num_rows ($result);
if ($num > 0 ) {
$i=0;
while ($i < $num) {
$closer = mysql_result($result,$i,"business_name");
$businessname = mysql_result($result,$i,"closer");
echo "$closer - $businessname";
echo"<br>";
++$i; } } else { echo "The database is empty"; };
echo "<select name=\"closer\" id=\"closer\">";
$query2 = "SELECT * FROM members ";
$result2 = mysql_query($query2);
$num2 = mysql_num_rows ($result2);
if ($num2 > 0 ) {
$i2=0;
while ($i2 < $num2) {
$username = mysql_result($result2,$i2,"username");
$fullname = mysql_result($result2,$i2,"name");
echo "<option value=\"$fullname\">$fullname</option>";
++$i2; } } else { echo "The database is empty"; }
echo "</select>";
?>
I want to be able to use the form on this page to select a closer from the database - and then assign that closer to each of the leads that have been selected. Here is where I have no idea how to continue.
Actually - i got it. I don't know why I didn't think of it sooner. First off I passed the original $list variable over to the new page - and then:
<?php
include_once"config.php";
$ids = $_POST['list'];
$closer = $_POST['closer'];
$query = "UPDATE `promises` SET `closer` = '$closer' WHERE id IN ($ids) ";
mysql_query($query) or die ('Error updating closers' . mysql_error());
echo "A new closer ($closer) was assigned to the following accounts:";
$query = "SELECT * FROM promises WHERE id IN ($list) ";
$result = mysql_query($query);
$num = mysql_num_rows ($result);
if ($num > 0 ) {
$i=0;
while ($i < $num) {
$businessname = mysql_result($result,$i,"business_name");
echo "<li>$businessname";
++$i; } } else { echo "The database is empty"; };
?>
The updated page before this:
$query = "SELECT * FROM promises WHERE id IN ($list) ";
$result = mysql_query($query);
$num = mysql_num_rows ($result);
if ($num > 0 ) {
$i=0;
while ($i < $num) {
$closer = mysql_result($result,$i,"business_name");
$businessname = mysql_result($result,$i,"closer");
echo "$closer - $businessname";
echo"<br>";
++$i; } } else { echo "The database is empty"; };
echo "<form name=\"form1\" method=\"post\" action=\"multiple_assign2.php\">";
echo "<input type=\"hidden\" name=\"list\" value=\"$list\" />";
echo "<select name=\"closer\" id=\"closer\">";
$query2 = "SELECT * FROM members ";
$result2 = mysql_query($query2);
$num2 = mysql_num_rows ($result2);
if ($num2 > 0 ) {
$i2=0;
while ($i2 < $num2) {
$username = mysql_result($result2,$i2,"username");
$fullname = mysql_result($result2,$i2,"name");
echo "<option value=\"$fullname\">$fullname</option>";
++$i2; } } else { echo "The database is empty"; }
echo "</select>";
echo "<input name=\"submit\" type=\"submit\" id=\"submit\" value=\"Reassign Selected Leads\">";
?>
After you select the leads and submit the form , your script should show them in a list with hidden inputs (with name=leads[] and value=the_lead's_id) and next to each lead there will be a dropdown box () which will be populated with all the closers.
After choosing and sending the second form your script will "run" all-over the leads' ids array and update each and every one of them.
Got the idea or you want some code?
I'm encountering the following problem:
I'd like to compare today's date against some dates in a database, then if it isn't expired yet, show something... but if all the dates in the table are expired, show something like 'No lecture scheduled at this time, return again'.
As for the first thing it's no problem, but I can't show the text where there aren't any future dates...
Here's the code,
Table:
id, dateposted, date_course, title, body
$sql = "SELECT *
FROM L
ORDER BY L.dateposted DESC;";
$result = mysql_query($sql) or die(mysql_error());
while($row = mysql_fetch_assoc($result))
{
$exp_date = $row['date_course'];
$todays_date = date("Y-m-d");
$today = strtotime($todays_date);
$expiration_date = strtotime($exp_date);
if ($expiration_date >= $today)
{
echo "<a href='courses.php'>" . $row['title']. "</a>";
echo "</br>";
}
}
I'll assume you're using MySQL. A couple of small changes to your query and code should make this work. You should definitely do this kind of filtering in the query and not the code.
$sql = "SELECT *
FROM L
WHERE date_course < NOW() AND dateposted < NOW()
ORDER BY L.dateposted DESC;";
$result = mysql_query($sql) or die(mysql_error());
if (mysql_num_rows($result) > 0)
{
while ($row = mysql_fetch_assoc($result))
{
echo "<a href='courses.php'>" . $row['title']. "</a>";
echo "</br>";
}
}
else
{
echo "No results available";
}
Several ways to do it. One would be to make the date comparison part of the query. If no rows are selected, show your special message.
Otherwise, you can set a flag like
$has_courses = false;
while ($row = fetch() {
$has_courses = true;
...
}
if (!$has_courses) { echo 'No courses'; }
Although you could do this far more efficiently by improving your query, here is the specific fix you request:
$sql = "SELECT *
FROM L
ORDER BY L.dateposted DESC;";
$result = mysql_query($sql) or die(mysql_error());
$out = false;
while($row = mysql_fetch_assoc($result))
{
$exp_date = $row['date_course'];
$todays_date = date("Y-m-d");
$today = strtotime($todays_date);
$expiration_date = strtotime($exp_date);
if ($expiration_date >= $today)
{
$out = true;
echo "<a href='courses.php'>" . $row['title']. "</a>";
echo "</br>";
}
}
if (!$out)
echo 'Nothing found.';