Jump to content

[SOLVED] Count & display number of records per day


Recommended Posts

Hi,

I use a MySQL db where one of the fields in a table is 'date'.

How can I count and display the records of the same date in php  while

the output for the last 30 days will be like:

 

Date                      Hits

2009-03-08            35

2009-03-09              0

2009-03-10            14

2009-03-11            27

 

Thanks in advance.

Regards, Hans

The table has the fields 'date' 'host' 'page'

I also tried :

SELECT date, COUNT(host) AS rowtotal FROM `tablename` GROUP BY date ORDER BY rowtotal DESC LIMIT 30;

The problem is the script for printing the output, perhaps by using a 'while' or 'for each' loop.

Can somebody provide an example?

im usually terrible at off the top of my head coding examples but we'll see, how does this work

 

$res = mysql_query("SELECT date, COUNT(host) AS rowtotal FROM `tablename` GROUP BY date ORDER BY rowtotal DESC LIMIT 30");

echo "<table>";

while($arr = mysql_fetch_assoc($res)) {

echo "<tr><td>".$arr['date']."</td><td>".$arr['rowtotal']."</td></tr>";

}

echo "</table>";

Thanks for your help. It looks promising.

$res = mysql_query("SELECT date, COUNT(host) AS rowtotal FROM table GROUP BY date ORDER BY rowtotal DESC LIMIT 30");

 

Output:

2009-01-05 21:56:08 2

2009-02-02 02:29:26 2

2009-03-06 17:31:42 2

2009-03-10 15:41:57 1

2009-03-04 20:48:40 1

2009-03-01 13:48:48 1

2009-02-26 09:28:37 1

etc. .. ( 30 rows total)

  It looks like the query picked mixed dates.

 

When    ORDER BY date DESC LIMIT 30 in:

$res = mysql_query("SELECT date, COUNT(host) AS rowtotal FROM table GROUP BY date ORDER BY rowtotal DESC LIMIT 30");

 

Output:

2009-03-13 12:57:32 1

2009-03-13 12:57:23 1

2009-03-13 12:57:14 1

2009-03-13 10:14:12 1

2009-03-12 20:58:44 1

2009-03-12 20:58:35 1

2009-03-12 12:47:25 1

2009-03-11 21:54:22 1

2009-03-11 21:18:22 1

etc. .. (30 rows total)

 

Should be:

2009-03-13 12:57:32 4

2009-03-12 12:57:32 3

2009-03-11 12:57:32 2

etc. ..(30 rows)

Is this possible?

Solution found!

$res = mysql_query("SELECT DISTINCT(date(date)) AS day, count( * ) AS count FROM lacauserie_host GROUP BY day ORDER BY day DESC");

 

echo "<table>";

while($arr = mysql_fetch_assoc($res)) {

echo "<tr><td>".$arr['day']."</td><td> => </td><td>".$arr['count']."</td></tr>";

}

echo "</table>";

 

Thanks for hints. Regards

  • 11 months later...

 

Hi,

I use a MySQL db where one of the fields in a table is 'advertise_date'  is date time and other is "tender_title" it is Varchar becasue i have to enter their names but for generating report How can I count and display the records of the same date in php while if u have any confusion ask me

the output for the last 30 days will be like:

 

Date                      Tenders

2009-03-08            35

2009-03-09              0

2009-03-10            14

2009-03-11            27

 

Thanks in advance.

Regards, imran

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.