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.';
Related
Im trying to get the datetime to get a form of date.
<?php
$con = mysql_connect("******","******","*****");
if($con) {
$db = mysql_select_db("*****");
if($db) {
$sql = "SELECT title, longstory, author, published FROM cms_news ORDER BY id DESC LIMIT 3";
$result = mysql_query($sql);
if($result) {
while($row = mysql_fetch_array($result)) {
echo "<ul class='menu-list' id='news-list'>";
echo "<li><a href='{url}/news/".$row['id']."'>".$row['title']."<span>".$row['published']."</span></a> </li>";
echo "</ul>";
}
echo "</table>";
} else
echo "";
} else
echo "";
} else
echo "";
?>
http://prntscr.com/eedv2w
That inside the red is the code. But i wants it like that under.
You can use as below:
Instead of $row['published'] You can use date('l d. F Y', strtotime($row['published']))
If you store direct strtotime into the database then you need to replace with date('l d. F Y', $row['published'])
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";
I have this code:
<?php
include 'config.php';
date_default_timezone_set('America/Los_Angeles');
$d = date('Y-m-d');
$m = date("m");
$day = date("d");
$t = date("His");
$ip = $_SERVER['REMOTE_ADDR'];
$c = file_get_contents('http://api.wipmania.com/'.$ip);
echo "<h2>ALL RESULTS TODAY:</h2><table>";
$_GET['c'] = $c;
$sc = $_GET['sc'];
if($c === "key"){
if($sc === "t"){
$result = "SELECT * FROM main WHERE date = '$d' ORDER BY time";
while($row = mysqli_fetch_array($result))
{echo "<tr><td>".$row['key'] . "</td><td> " . $row['country']."</td><td>".$row['ip']."</td></tr>"; }
}
}
echo '</table>';
?>
i have tried without $con: mysqli_fetch_array($result), but it was the same...
But nothings appear...
no error no results...
Please help... Thanks!
You have not connected to the database or queried your results:
$conn = mysqli_connect($hostname,$username,$password,$dbname) or die(mysqli_error());
//...
$your_query = "SELECT * FROM main WHERE date = '$d' ORDER BY time";
$result = mysqli_query($conn, $your_query);
while ($row = mysqli_fetch_array($result)){
//...
}
you forgot mysqli_query.
replace this
$result = "SELECT * FROM main WHERE date = '$d' ORDER BY time";
by
$result =mysqli_query("SELECT * FROM main WHERE date = '$d' ORDER BY time");
You forgot to perform the query.
$result = mysqli_query($con, "SELECT * FROM main WHERE date = '$d' ORDER BY time");
I'm trying to use a row from a MySQLi result within a secondary query.
but im getting some unexpected results.
<?php
$mysqli = new mysqli('connection');
if ($mysqli->connect_error) {die('Connect Error: ' . $mysqli->connect_error);}
$today = date("Ymd");
$query = "SELECT course FROM dailytips WHERE date = 20130724 GROUP BY course";
$result = $mysqli->query($query);
while($row = $result->fetch_array())
{
$rows[] = $row; }
foreach($rows as $row)
{
echo $row['course'] . "<br/>";
$query2 = "SELECT horse, time, date FROM dailytips WHERE date = 20130724 and course ='{$row['course']}' ORDER BY time";
$result2 = $mysqli->query($query2);
$today_uk = " " . date("d/m/y");
while($row2 = $result2->fetch_array())
{
$rows2[] = $row2;
}
foreach($rows2 as $row2)
{
$date = $row2['date'];
$date = date("d/m/y", strtotime($date));
echo '<div style= "width:600px; font-family:verdana;"><div style="float:left; width:400px; margin-bottom:10px; margin-top10px;">'.$row2['time'] . "-" . $row2['horse'] .' </div>' ;
}
}
$result->close();
$mysqli->close();
?>
my page currently looks like -
ipswich
11:00-running
12:00-flamingo rider
14:00-lightning
norwich
11:00-running
12:00-flamingo rider
14:00-lightning
13:10-ed is back
14:05-redrum
17:05-pickle
whereas I want
ipswich
11:00-running
12:00-flamingo rider
14:00-lightning
norwich
13:10-ed is back
14:05-redrum
17:05-pickle
to be returned.
How can I free the result in the second for each query?
Good heavens what a mess.
Try this.
<?php
$mysqli = new mysqli('connection');
if ($mysqli->connect_error) {die('Connect Error: ' . $mysqli->connect_error);}
$today = date("Ymd");
$query = "SELECT course FROM dailytips WHERE date = 20130724 GROUP BY course";
$result = $mysqli->query($query);
$row = $result->fetch_array();
echo $row['course'] . "<br/>";
do {
$query2 = "SELECT horse, time, date FROM dailytips WHERE date = 20130724 and course ='{$row['course']}' ORDER BY time";
$result2 = $mysqli->query($query2);
$today_uk = " " . date("d/m/y");
while($row2 = $result2->fetch_array())
{
$date = $row2['date'];
$date = date("d/m/y", strtotime($date));
echo '<div style= "width:600px;font-family:verdana;"><div style="float:left; width:400px; margin-bottom:10px; margin-top:10px;">'.$row2['time'] . "-" . $row2['horse'] .' </div>' ;
}
} while ( $row = $result->fetch_array() );
$result->close();
$result2->close();
$mysqli->close();
?>
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!
}
}