I have the following data being returned from my database query:
+---------------+-----------+------------------+--------------+-------+ | district_name | school_id | school_name | section | score | +---------------+-----------+------------------+--------------+-------+ | My ISD | 11 | My First School | English | 20 | | My ISD | 11 | My First School | Math | 23 | | My ISD | 11 | My First School | Reading | 24 | | My ISD | 11 | My First School | Science | 23 | | My ISD | 12 | My Second School | English | 11 | | My ISD | 12 | My Second School | Math 开发者_开发百科 | 19 | | My ISD | 12 | My Second School | Reading | 22 | | My ISD | 12 | My Second School | Science | 26 | +---------------+-----------+------------------+--------------+-------+
I need to put this data into an array to easily output a table of scores by school:
School English Math Reading Science ------------------------------------------------------- My First School 20 23 24 23 My Second School 11 19 22 26
I'm having trouble formatting this data into an array that accomplishes this. The ideal structure would be:
array(
$schoolName => array(
'results' => array(
'section' => $section_name
'score' => $score
),
),
);
I've tried several approaches but can't get them to work correctly.
while ($row = mysql_fetch_array($query)) { //you can define the query, right?
$array[$row['school_name']]['results'][$row['section']] = $row['score'];
}
Although I don't support the way you have your database set up, I'm not going to give you a lecture on it. I'd assume this is a school project or something.
$table_rows = array();
while ($row = mysql_fetch_assoc($mysql_result)) {
$school_name = $row['school_name'];
$table_rows[$school_name]['results'][$row['section']] = $row['score'];
// adding the other stuff to $table_rows...
}
I hope this helps.
精彩评论