I have created a function to display the dates and the count of posts within those dates to be used as my blog menu, however it only partially works, any help with this would be appreciated.
Function being called:
function displayBlogMenu()
{
blogDBSelect();
$sql = "SELECT datePosted FROM blog_entries";
$results = mysql_query($sql);
while($post = mysql_fetch_array($results))
{
$startDate = date('Y-m-01', strtotime($post['datePosted']));
$endDate = date('Y-m-01', strtotime("+1 month", strtotime($post['datePosted'])));
$sql2 = "SELECT * FROM blog_entries WHERE datePosted >= '$startDate' AND datePosted < '$endDate'";
$results2 = mysql_query($sql2);
$count = mysql_num_rows($results2);
}
echo ''.date('F, Y', strtotime($startDate)).' ('.$count.' Posts)<br>';
}
I want the function to return the date as links such as:
June, 2011 (10 Posts)
July, 2011 (2 Posts)
It looks like you can do that by one single query
SELECT Date_format(dateposted, '%M, %Y'),
COUNT(*) AS cc
FROM blog_entries
GROUP BY YEAR(dateposted),
MONTH(dateposted)
ORDER BY COUNT(*) DESC,
dateposted DESC
Related
I need help with an query. It is to list only 3 dog shows, by the row postal_date. However, I do not want any results that are past the current date.
I have them listed but some do show past the current date.
<?php
$query = "SELECT shows.*, judges.* FROM `shows`
INNER JOIN `judges` ON shows.judge_id = judges.judge_id
ORDER BY shows.postal_close DESC
LIMIT 3";
$select_all_shows_query = mysqli_query($connection,$query);
while($row = mysqli_fetch_assoc($select_all_shows_query)) {
$show_id = $row['show_id'];
$show_title = $row['show_title'];
$postal_close = date('d F Y', strtotime($row['postal_close']));
$show_image = $row['show_image'];
$judge_name2 = $row['judge_name2'];
$judge_id = $row['judge_id'];
?>
$query = "SELECT shows.*, judges.* FROM `shows`
INNER JOIN `judges` ON shows.judge_id = judges.judge_id
WHERE DATE(shows.postal_close) >= CURDATE()
ORDER BY shows.postal_close DESC LIMIT 3";
I need to echoing all authors with articles older than 11 months.
But there is nothing as result.
My date column is like this - 2017-07-01 05:00:00
$d = date("F 1, Y", strtotime("-11 months"));
// also tried:
$d = mktime(0, 0, 0, date('n')-11, 1, date('y'));
$items = '';
$sql = "select * from posts where date < " . $d;
$st = $db->prepare($sql);
$db -> execute();
while ($row = $st->fetch()) {
$items .= "<div class='auth'>" . $row['auth'] . "</div>/n";
}
echo $items;
Here a query that returns a distinct list of authors with books published more than 11 months ago.
SELECT DISTINCT AUTH
FROM posts
WHERE `date`<DATE(NOW() - INTERVAL 11 MONTH);
Try this query in your PHP code:
$sql = "SELECT * FROM posts WHERE YEAR(date) = YEAR(CURRENT_DATE - INTERVAL 11 MONTH) AND MONTH(date) = MONTH(CURRENT_DATE - INTERVAL 11 MONTH)";
i want to display the total value for the current month. in my date field (entrydate), i am using the m/d/Y format; however, i have try using the following code but it's not working.please help
$query8 ="SELECT amount FROM pgoods WHERE MONTH(entrydate) = MONTH(CURDATE()) YEAR(entrydate) = YEAR(CURDATE())";
$res = mysqli_query($mysqli, $query8);
$amou = '';
while($row = mysqli_fetch_array($res)){
$amount1= "".$row["amount"]."";
$amou += $amount1;
}
try this it will give you only month
DATE_FORMAT(CURDATE(), '%M %Y')
Maybe it sounds crazy, but I couldn't come up with a better title.
I click a button and I send a date (Y-m-d) to a query, which looks up if data found and if data is found, it gives me the data back, if no data found, then it returns null.
Now I would like to do it so, that if no data available for the date I sent, the next date will be returned where data actually exists.
So practically I send a search for 30 August and if no data available for 30 August, the query should search for the next day in THE PAST for available data, so if it finds data on the 2nd August, then the data of 2nd August should be returned.
I tried with "date <= actual date" and "if result == 0" etc. but I am just tapping in the dark.
The return is always a bunch of data, not just a single row.
My code looks like this now:
$category = $_GET['category'];
$query = mysqli_query($bd, "SELECT MAX(datum) as max_datum, MIN(datum) as min_datum FROM macapps WHERE free = 1 AND category = '".$category."' ") or die(mysqli_error());
$date = mysqli_fetch_assoc($query);
$day = isset($_GET['date']) ? $_GET['date'] : 0;
$date = date('Y-m-d', strtotime($date['max_datum'] . ' ' . $day . ' day'));
$result = mysqli_query($bd, "SELECT appID, title, icon, category, datum FROM macapps WHERE datum = '".$date."' AND free = 1 AND category = '".$category."' ORDER BY title") or die(mysqli_error());
$rows = array();
while($count = mysqli_fetch_assoc($result)) {
$rows[] = $count;
}
echo json_encode($rows);
Try this query
Select
appID, title, icon, category, datum
From
macapps
Where datum = (Select
m.datum
From
macapps m
WHERE
m.datum <= '.$date.'
Order By m.datum Desc
LIMIT 1
)
And
Free=1
And
category = '.$category.'
Order By title;
Another Query
Select
appID, title, icon, category, datum
From
macapps
Where datum = (Select
Max(m.datum)
From
macapps m
Where
m.datum <= '.$date.'
)
And
Free=1
And
category = '.$category.'
Order By title
I am trying to get the records per current day, week and month in php mysql. The date column is of date type. The issue is here is that my week records and months records are getting same from below script. Here is my code, have a look.
public function getTodayComing(){
$connection = db::factory('mysql');
$sql = "select * from bookings,bookers where ";
$qualifier = ' bookings.booker_id = bookers.id
AND ((status ="'.AppGlobal::$bookingStatus['APPROVE'].'"
OR status ="'.AppGlobal::$bookingStatus['RESCHEDULED'].'"
OR status ="'.AppGlobal::$bookingStatus['RECONSULTED'].'")
AND date=DATE( NOW() ))
ORDER BY date ASC';
$sql. = $qualifier;
return $valuearray = $connection->getArray ($sql );
}
public function getWeekComing() {
$connection = db::factory('mysql');
$sql = "select * from bookings,bookers where ";
$qualifier = ' bookings.booker_id = bookers.id
AND ((status ="'.AppGlobal::$bookingStatus['APPROVE'].'"
OR status ="'.AppGlobal::$bookingStatus['RESCHEDULED'].'"
OR status ="'.AppGlobal::$bookingStatus['RECONSULTED'].'")
AND date > DATE_SUB(NOW(), INTERVAL 1 WEEK)) ORDER BY date ASC';
$sql. = $qualifier;
return $valuearray = $connection->getArray( $sql );
}
public function getMonthComing() {
$connection = db::factory('mysql');
$sql = "select * from bookings,bookers where ";
$qualifier = ' bookings.booker_id = bookers.id
AND ((status ="'.AppGlobal::$bookingStatus['APPROVE'].'"
OR status ="'.AppGlobal::$bookingStatus['RESCHEDULED'].'"
OR status ="'.AppGlobal::$bookingStatus['RECONSULTED'].'")
AND date > DATE_SUB(NOW(), INTERVAL 1 MONTH)) ORDER BY date ASC';
$sql. = $qualifier;
return $valuearray = $connection->getArray( $sql );
}
Aah, I just saw it I think
date > DATE_SUB(...)
should be
date BETWEEN CUR_DATE() AND DATE_ADD(CUR_DATE(), INTERVAL 1 ...)