mysql - PHP ERROR : Notice: Undefined index: January in -
i have code count news posted in month:
$months = array("january","february","march","april", "may","june","july","august","september","october", "november","december"); $mews = db->fetch("select id, year(from_unixtime(date)) year, monthname(from_unixtime(date)) month, count(id) total article year(from_unixtime(date)) = 2014 group year, month order year, month"); // index article counts month , year easy lookup $indexednewsdata = array(); foreach ($mews $news) { $indexednewsdata[$news['month']] = $news['total']; } // print output foreach($mews $news){ foreach ($months $month) { $total = intval($indexednewsdata[$month]); echo '<li>'.$month.' '.$total.'</li>'; } }
but,for each month 0
post see error :
notice: undefined index: january in......
how can fox error?!
you should initialise array properly.
$indexednewsdata = array(); foreach($months $month) $indexednewsdata[$month] = 0; foreach($mews $news) ...
Comments
Post a Comment