Reputation: 11
I want to create array from multiple rows. In My table I have Year
field, regd
field for counting the number of students, and Class
field. I want to output the data like below.
$data = array(
'2012' => array(
'KG_I' => 87,
'KG_II' => 80,
'I' => 90,
'II' => 120,
'III' => 100,
'IV' => 110,
'V' => 98,
),
'2013' => array(
'KG_I' => 82,
'KG_II' => 84,
'I' => 92,
'II' => 110,
'III' => 120,
'IV' => 108,
'V' => 90,
),
'2014' => array(
'KG_I' => 90,
'KG_II' => 83,
'I' => 95,
'II' => 110,
'III' => 120,
'IV' => 81,
'V' => 95,
),
);
My attempt was like this:
$std=array();
$hms="SELECT COUNT(DISTINCT regd) as stdNum, Class, Year FROM
student GROUP by Class";
$quar=mysql_query($hms);
$myarray = array();
while($row = mysql_fetch_array($quar)){
$myarray[$row['Class']] = $row['stdNum'];
}
I have no idea how I output the Year array.
Upvotes: 0
Views: 89
Reputation: 3665
You opened two arrays which is not necessary. print_r()
may come handy when dealing with arrays. Hope this will help.
$hms="Select Year, Class, COUNT(DISTINCT regd) as stdNum
from student group by Year, Class";
$quar=mysql_query($hms);
$myArray = array();
while($row = mysql_fetch_assoc($quar)){
$mya[$row['Year']][$row['Class']] = $row['stdNum'];
}
echo "<pre>";
print_r($myArray);
echo "</pre>";
Upvotes: 0
Reputation: 87
$std=array();
$hms="Select Year,Class,count(regd) 'stdNum' from student group by Year,Class";
$quar=mysql_query($hms);
$myarray = array();
while($row = mysql_fetch_array($quar)){
$myarray[$row['Year']]$row['Class']] = $row['stdNum'];
}
Upvotes: 0
Reputation: 1238
Let's try this :
while($row = mysql_fetch_array($quar)){
$myarray[$row['Year']][$row['Class']] = $row['stdNum'];
}
Enjoy :)
Upvotes: 2