i need to have a function that will echo the post that has been posted today.
from 00.00 to 23.59 . ihave created a function but it is not giving the result i need
here is my function:
function todayNews() {
$id = mysql_real_escape_string ($id);
$sql = 'SELECT * FROM wp_posts WHERE post_status = "publish" AND post_date = CURRENT_DATE ORDER BY post_date DESC LIMIT 15';
$res = mysql_query($sql) or die (mysql_error());
if (mysql_num_rows($res) !=0):
while ($row = mysql_fetch_assoc($res)) {
$title = ($row['post_title']);
$old_date = $row['post_date']; // returns Saturday, January 30 10 02:06:34
$old_date_timestamp = strtotime($old_date);
$new_date = date('H:i', $old_date_timestamp);
$mycontent = ($row['post_content']);
$mycontent = strip_tags($mycontent);
$mycontent = substr($mycontent,0,350);
$mycontent = preg_replace("/\[caption.*\[\/caption\]/", '', $mycontent);
$first_img = '';
$my1content = $row['post_content'];
$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $my1content, $matches);
$first_img = $matches [1] [0];
if(empty($first_img)){ //Defines a default image
$first_img = "/img/default.png";
}
echo '
<ul class="li-sub">
<li>
'.$title.'
</li>
</ul>
';
}
else:
echo 'There are no posts for today !';
endif;
} // end
Thank you !
EDIT:
Post_date have this format : Y-m-d H:i
You are comparing POST_DATE which is a datetime with CURRENT_DATE which is a Date , it won't give any result.
you can try the following SQL
$sql = 'SELECT * FROM wp_posts WHERE post_status = "publish" AND DATE(post_date) = CURRENT_DATE ORDER BY post_date DESC LIMIT 15';
This will convert the post_date to only date before comparing with current date.
Related
I started my project March last year and have almost got it right.
Trying to get data from mysql and put it in highcharts.
This is my query
<?php
$con = mysqli_connect("localhost","root","root","manortsc_test");
if (!$con) {
die('Could not connect: ' . mysql_error());
}
$sth = mysqli_query($con,"
SELECT DateTime,max(T)
FROM alldata
WHERE DATE(DateTime) = CURDATE() - INTERVAL 1 DAY
GROUP BY hour(DateTime)
"
);
$rows = array();
$rows['name'] = 'Outside';
while($r = mysqli_fetch_array($sth)) {
$rows['data'][] = $r['max(T)'];
}
$result = array();
array_push($result,$rows);
print json_encode($result, JSON_NUMERIC_CHECK);
mysqli_close($con);
?>
The json output is missing the date and time
[{"name":"Outside","data":[17.5,16.3,15.6,15.1,14.4,14,14.1,16,18.5,21.7,24.1,26.9,28.3,29.6,30.6,31.1,31.8]}]
The graph shows okay (except datetime on x axis) and I cannot figure how to fix it. I have tried every way except the correct way. Any help would be appreciated.
Changed the query to this and now it works.
Thanks wergeld and Yuri for the assistance.
$sth = mysqli_query($con,"
SELECT DateTime,max(T)
FROM davisvp
WHERE DATE(DateTime) = CURDATE()
GROUP BY hour(DateTime)
"
);
$result = array();
$result['name'] = 'temperature';
while($row = mysqli_fetch_array($sth))
{
$date = strtotime($row['DateTime']);
$maxt = 1 * $row['max(T)'];
$result1 = array();
array_push($result1,$date);
array_push($result1,$maxt);
$result['data'][] = $result1;
}
echo json_encode($result, JSON_NUMERIC_CHECK);
I have one problem.
I make search function in php. I need limit searching only for 1 mounth ago.
ex.
I put 'android' word
In result show only titles who containing android openet in last mounth. Oldest no show.
Thank you
EDIT
$sql = '
SELECT aa_search_index.content_id, MATCH(aa_search_index.title, aa_search_index.metadata) AGAINST (?) AS score
FROM aa_search_index
LEFT JOIN aa_thread
ON (aa_thread.thread_id = aa_search_index.content_id)
WHERE MATCH(aa_search_index.title, aa_search_index.metadata) AGAINST (?)
AND content_type = ?
AND content_id != ?
';
$params = array($query, $query, 'thread', $excludedThreadId);
if (!Search_query::get('options')->showResultsGlobal)
{
$params[] = $forum['node_id'];
$sql .= 'AND aa_thread.node_id = ?
';
}
$sql .= 'LIMIT 5';
$results = $this->fetchAllKeyed($sql, 'content_id', $params);
$threadIds = array_keys($results);
select * from your_table
where column_with_text = 'android'
and datetime_column >= curdate() - interval 1 month
I have the following update query but for some reason it's not working. I think it's something to do with the "id = '".$id."' but I've tried about three different ways and I cannot seem to get it to work. I've written update queries before with no problems but for some reason this one is being a pain. Thanks in advance.
$id = $_GET['id'];
$speaker = mysql_real_escape_string($_POST['speaker']);
$message = $_POST['message'];
$title = mysql_real_escape_string($_POST['title']);
$date = $_POST['date'];
$day = $_POST['day'];
$password = mysql_real_escape_string($_POST['password']);
$complete = $_POST['complete'];
$title = ucwords(strtolower($title));
if ($complete && ($password == "*****"))
{
$db = mysql_connect($hostname, $username, $password) or die(mysql_error());
mysql_select_db($dbname,$db) or die(mysql_error());
mysql_query("UPDATE sermons SET speaker = '$speaker', message = '$message', title = '$title', date = '$date', day = '$day' WHERE id = '$id'");
$num_rows = mysql_num_rows(mysql_query("SELECT speaker, message, title, date, day FROM sermons WHERE speaker = '$speaker' AND message = '$message' AND title = '$title' AND date = '$date' AND day = '$day'", $db));
if ($num_rows == 1)
echo "<script type='text/javascript'> alert('Sermon Information Entered Successfully!'); </script>";
else
echo "<script type='text/javascript'> alert('Error! Please Try Again.'); </script>";
}
else if ($complete && ($password != "*****"))
{
echo "<script type='text/javascript'> alert('Incorrect Password! Please Try Again.'); </script>";
}
Because of id is Integer so this is correct code try thus :
mysql_query("UPDATE sermons SET speaker = '$speaker', message = '$message', title = '$title', date = '$date', day = '$day' WHERE id =".$id);
I expected it will be works.
Writing this as an answer as providing code in comments is difficult to read
Try changing
mysql_query("UPDATE sermons SET speaker = '$speaker', message = '$message', title = '$title', date = '$date', day = '$day' WHERE id = '$id'");
To
mysql_query("UPDATE sermons SET speaker = '$speaker', message = '$message', title = '$title', date = '$date', day = '$day' WHERE id = '$id'") or die(mysql_error());
Then add the error message to your question please.
I would like a certain value(55) when there is no result in a MySQL SELECT Statement.
I tried this but it doesn't work.
<?php
include_once("JSON.php");
$json = new Services_JSON();
$con = mysql_connect("XXXXX.com", "XXXXX", "XXXXXX");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("XXXXX", $con);
$arr = array();
// The following statement runs but when i tried with IFNULL .. doesn't work...
//$rs = mysql_query("SELECT * FROM partie WHERE statut = 'en cours' ORDER BY date ASC LIMIT 1");
$rs = mysql_query("SELECT IFNULL((SELECT * FROM partie WHERE statut = 'en cours' ORDER BY date ASC LIMIT 1),55)");
while($obj = mysql_fetch_object($rs)) {
$arr[] = $obj;
}
Echo $json->encode($arr);
?>
You are getting an error, try echo mysql_error();
What you probably want is something like this:
$rs = mysql_query("SELECT * FROM partie WHERE statut = 'en cours' ORDER BY date ASC LIMIT 1");
if( mysql_num_rows($rs) == 0 ) {
echo 55;
}
i need to create a function that will display the articles of the current date only.
Here is what i have created:
function lastnews () {
$id = mysql_real_escape_string ($id);
$date = date('d');
$sql = 'SELECT * FROM posts WHERE post_status = "publish" AND post_date = "$date" ORDER BY post_date DESC LIMIT 3';
$res = mysql_query($sql) or die (mysql_error());
if (mysql_num_rows($res) !=0):
while ($row = mysql_fetch_assoc($res)) {
$title = $row['post_title'];
echo '<li>'.$title.'</li>';
}
endif;
} // end
But this is not working. the formt of date in database is :
Y-m-d H:i
Thank you for reading this
You need to specify the entire date (date('Y-m-d')) and the query should say AND post_date > '$date' because it includes a time. You can also say AND post_date > CURRENT_DATE.
Change
$date = date('d');
To:
$date = date('Y-m-d H:i');