php - Monthly loop with missing rows -
i'm trying loop displays count of entries next month link, since count skips row when there no entries in query, count links not work. how work out since cannot simple array loop due missing rows. here i've been trying:
query results
count month 1 1 63 3 21 4 7 5 3 6 6 7 php
// new month count public function new_month_count() { $year = $this->chosen_year(); $main_sql = $this->month_count_sql() . " , year(exp_date) = " . $year . " group month(exp_date) order month(exp_date), month"; $res = $this->conn->query($main_sql); $array = array(); $array[0] = 0; $i = 1; while ($row = $res->fetch_assoc()) { $array[$i] = ($row['month'] == $i ? $row['count'] : 0); $i += 1; } return $array; } // create monthly links public function monthly_links() { $count = $this->new_month_count(); $months = array('','january','february','march','april','may','june','july','august', 'september','october','november','december'); ($i=1 ; $i <= 12 ; $i++) { $array[] = "<a href='monthly.php?month=" . $i . "&status=3'>" . $months[$i] . " " . $this->chosen_year() . "  ( " . $count[$i] . " )</a>"; } return $array; } the output works if there no skipped month in query result, if row skipped, 6th month show count of 6... strange.
if treat array dictionary , check if result contained value month:
// new month count public function new_month_count() {
$year = $this->chosen_year(); $main_sql = $this->month_count_sql() . " , year(exp_date) = " . $year . " group month(exp_date) order month(exp_date), month"; $res = $this->conn->query($main_sql); $array = array(); while ($row = $res->fetch_assoc()) { $array[$row['month']] = $row['count']; } return $array; }
// create monthly links public function monthly_links() {
$count = $this->new_month_count(); $months = array('','january','february','march','april','may','june','july','august', 'september','october','november','december'); ($i=1 ; $i <= 12 ; $i++) { $tempcount = 0; if(isset($count[$i]) ) { $tempcount = $count[$i] } $array[] = "<a href='monthly.php?month=" . $i . "&status=3'>" . $months[$i] . " " . $this->chosen_year() . "  ( " . $tempcount . " )</a>"; } return $array; }
note: isn't tested, hope helps
Comments
Post a Comment