isedeasy Posted May 11, 2011 Share Posted May 11, 2011 I am trying to get data out of my database to display in a graph, the problem I have is that if there are no rows on a certain date then that date does not get placed into the array. I am thinking I will need to loop through previous dates and match the 'current working date' to the keys of my array to see the total number of rows my array of data from the database will look like this :- Array ( [2011 April 25th] => 32 [2011 April 26th] => 70 [2011 May 6th] => 86 [2011 May 7th] => 86 [2011 May 8th] => 87 [2011 May 9th] => 86 [2011 May 10th] => 86 [2011 May 11th] => 79 ) notice the gap between april 26th and may 6th, thats because there are 0 rows in the database that fall on these days. Whats the best way to loop through say the past month? Hope this makes sense, if there is a better method I would be glad to hear it. Quote Link to comment https://forums.phpfreaks.com/topic/236139-looping-through-dates/ Share on other sites More sharing options...
xyph Posted May 11, 2011 Share Posted May 11, 2011 I'd do something like this <?php $date = array(4,2011); $max = date('t',mktime(0,0,0,$date[0],1,$date[1]) ); $data = array( 4 => 'Some event on april 4th', 15 => 'Mid month maddddness', 23 => 'foobar', 51=> 'wut?' ); for($d=1;$d<=$max;$d++) { echo "$date[0]-$d has "; if( isset($data[$d]) ) echo "event {$data[$d]}"; else echo "no event"; echo "<br />"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/236139-looping-through-dates/#findComment-1214106 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.