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?!

demo : http://stackdemo.url.ph/test/

source: http://stackdemo.url.ph/test/test

you should initialise array properly.

$indexednewsdata = array(); foreach($months $month) $indexednewsdata[$month] = 0;  foreach($mews $news)    ... 

Comments

Popular posts from this blog

google api - Incomplete response from Gmail API threads.list -

Installing Android SQLite Asset Helper -

Qt Creator - Searching files with Locator including folder -