0

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.

3
  • It should be GROUP by Year then class i.e. GROUP BY Year, Class Commented Aug 10, 2014 at 10:54
  • If you think you have a problem with your SQL query, you should export your student table, so we could try a test. Commented Aug 10, 2014 at 10:58
  • It might be helpful to provide a SQL Fiddle here, so we can see your test data. Some people will even fork it for you and offer a suggested query. Commented Aug 10, 2014 at 11:08

3 Answers 3

2

Let's try this :

while($row = mysql_fetch_array($quar)){
    $myarray[$row['Year']][$row['Class']] = $row['stdNum'];
}

Enjoy :)

Sign up to request clarification or add additional context in comments.

2 Comments

You did not mention how I output. My output did not show Year array.
Ah, I commented on this as having a syntax error, but I see what you are doing now. I'd create intermediate variables for $year and $class for that reason, but +1.
0

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>";

Comments

0
$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'];
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.