php - Loop through mysqli results array once to get all information -
php - Loop through mysqli results array once to get all information -
i querying database data, in order need, end looping through results array @ to the lowest degree 3 times. how of info need out of array without having loop many times? need info array based on results previous loops. code below way figure out in order query database once.
$recordsql = mysqli_query($link, $sqlstring); $resultmonths = array(); while($recordresult = mysqli_fetch_assoc($recordsql)) $resultmonths[] = $recordresult['date']; mysqli_data_seek($recordsql, 0); $uniquemonths = array_unique($resultmonths); foreach($uniquemonths $key => $date){ echo '</div><div class="current-month">'.translatedate($date, '').'</div>'; $resultcompanies = array(); while($companyresult = mysqli_fetch_assoc($recordsql)){ if($companyresult['date'] == $date) $resultcompanies[] = $companyresult['company']; } mysqli_data_seek($recordsql, 0); $uniquecompanies = array_unique($resultcompanies); $oldco = ''; foreach($uniquecompanies $key => $company){ $x = 0; while($typeresult = mysqli_fetch_assoc($recordsql)){ if($typeresult['date'] == $date && $typeresult['company'] == $company){ if($oldco != $typeresult['company']){ if($x != 0) echo '</div>'; echo '<div class="company-record">'.$typeresult['name'].' - '; } if($x > 0) echo ', '; echo translateid('type', $typeresult['type']).'('.translateid('section', $typeresult['section']).')'; $oldco = $typeresult['company']; $x++; } } echo '</div>'; mysqli_data_seek($recordsql, 0); } }
fyi, looping n**3 times. way:
$month_company_rows = array(); while ($row = mysqli_fetch_assoc($recordsql)) { $month_company_rows[$row['date']][$row['company']][] = $row; } foreach ($month_company_rows $date => $company_rows) { echo '</div><div class="current-month">'.translatedate($date, '').'</div>'; foreach ($company_rows $company => $rows) { echo '<div class="company-record">'.$company.' - '; foreach ($rows $x => $row) { if($x > 0) echo ', '; echo translateid('type', $row['type']).'('.translateid('section', $row['section']).')'; } echo '</div>'; } }
php mysql arrays multidimensional-array mysqli
Comments
Post a Comment