注mysqli_fetch_array()はPHP7に廃止され、FETCH_ASSOCとmysqliのクラスを使用することを検討して()
私はおそらくmIDによってあなたのクエリを注文したと推測します。 データを収集し、それをループしてテーブルを作成する配列を作成することをお勧めします(順番に記述しますが、クラスメソッドにスライスすることを検討する必要があります)。注:これはテストされていません。
// execute the query
$result = mysqli_query($handler, $query);
// create a stack to collect the data
$data = array();
// the table header, will also be used to store values in order
$metaData = array(
'mID' => 'MemberID(system)',
'mFirst' => 'First Name',
// other meta data (make sure you use the field names)...
);
$grades = array(
'gReading' => 'Reading',
'gMath' => 'Math',
'gScience' => 'Science',
'gDaysAbsent' => 'Absent'
);
// create a table header
$tableHeader = $metaData;
// create 3 fields for each subject
foreach ($grades as $fieldname => $headline) {
$tableHeader[$fieldname . 'Q2'] = $headline . ' Q2';
$tableHeader[$fieldname . 'Q1'] = $headline . ' Q1';
$tableHeader[$fieldname . 'Performance'] = $headline . ' Performance';
}
// read each row of the result
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
// we haven't handled this user before
if (!array_key_exists($row['mID'], $data)) {
// create a row with the meta data
$tableRow = array();
foreach ($metaData as $fieldname => $headline) {
$tableRow[$fieldname] = $row[$fieldname];
}
} else {
// fetch this users previous data
$tableRow = $data[$row['mID']];
}
// populate the grades for this quarter and calculate the performance
foreach ($grades as $gradename => $headline) {
// f.ex. gReadingQ1
$tableRow[$gradename . $row['gSemester']] = $row[$gradename];
// calculate the performance
if ($tableRow[$gradename . 'Q1'] && $tableRow[$gradename . 'Q2']) {
// TODO: do some magic here
// f.ex. calculate the number of the letter in the alphabet and get the average
$tableRow[$gradename . 'Performance'] = 'SOMETHING';
}
}
// store the row
$data[$row['mID']] = $tableRow;
}
// TODO: create the table in whatever format you want
print_r($data);
大部分はカバーしていますが、データを含む2つのクエリから2つの配列があります。各配列に対してこれを行い、次に両方を3番目の配列に処理しますか? – user3107710
なぜ2つのクエリがありますか?結果は同じテーブルから来ているように見えます。その場合、DBをあまり頻繁に叩くことを避けるために、より一般的なクエリ(f.ex. 'semester IN(" Q1 "、" Q2 ")')を行うべきです。 2つのクエリを本当に行う必要がある場合は、結果は配列になり、whileループの代わりに行をフェッチすると、結果スタックのforeachが実行されます。 – masterfloda