0

I have a mysql query in which I group each post by the date is was posted. I want to be able to echo the amount of posts that happened each day, know a way?

Here is what I've written so far which does post a new <div> for each day it was posted.

$trendQuery = "
SELECT DATE(timestamp) AS ForDate,
    COUNT(*) AS NumPosts
 FROM   trends
 WHERE `trend` = '" . $_GET['graph'] . "'
 GROUP BY DATE(timestamp)
 ORDER BY ForDate
";

$result = mysql_query($trendQuery);
    while ($row = mysql_fetch_array($result)) {
        echo'
        <div>
            <p>Here is where I want to say how many posts that happened!</p>
    </div>
    ';
}
McDan Garrett
  • 183
  • 1
  • 3
  • 15

2 Answers2

1

You have your query pretty much set up. To echo the results, you simply need to refer the columns with the alias name, in your case ForDate and NumPosts

$trendQuery = "
SELECT timestamp AS ForDate,
    COUNT(*) AS NumPosts
 FROM   trends
 WHERE `trend` = '" . $_GET['graph'] . "'
 GROUP BY timestamp
 ORDER BY ForDate
";

$result = mysql_query($trendQuery);
    while ($row = mysql_fetch_array($result)) {
        echo'
        <div>
            Date: '.$row['ForDate'].' ---- Number of Posts: '.$row['NumPosts'].'<br />
    </div>
    ';
asprin
  • 9,579
  • 12
  • 66
  • 119
0

Create a view of your trend query and inner join it with "Select (Id or day that you want to merge) From trend"

i hope that helps

  • Sorry Actually I think that You Want To fetch another day or id column that you want to print with this group column – Nisarg Desai Nov 18 '13 at 12:25