1

There are many students in my table with different classes. I want to print different class one time and print the number of students of that class. I can print the class. But couldn't print the number of students for each class. How can I do this? For example:

In my database:                       Desired Output:

Class     Name                         Class      Num. of Students
  2       John                           1             2
  2       Snow                           2             3
  3       Jara                           3             1
  1       Peter
  1       Nira
  2       Jerin

Here is my code:

        $q = "SELECT * FROM ipsc_student group by class";
        $rs = mysql_query($q);
        $numOfRows=mysql_num_rows($rs);

        $rt = "";

        $sl = 0;
            while($row=mysql_fetch_assoc($rs)){
                $sl++;
                $rt.="<tr>";
                $rt.="<td>$sl</td>";
                $rt.="<td>".$row['class']."</td>";
                $rt.="<td>$numOfRows</td>";
                $rt.="<td></td>";
                $rt.="<td></td>";
                $rt.="<td></td>";
                $rt.="<td></td>";
                $rt.="<td></td>";
                $rt.="<td></td>";
                $rt.="</tr>";
            }
            echo $rt;

1 Answer 1

1

Try this

    $q = "SELECT class, count(*) as NUM FROM ipsc_student GROUP BY class";

    $rs = mysql_query($q);
    $numOfRows=mysql_num_rows($rs);

    $rt = "";

    $sl = 0;
        while($row=mysql_fetch_assoc($rs)){
            $sl++;
            $rt.="<tr>";
            $rt.="<td>$sl</td>";
            $rt.="<td>".$row['class']."</td>";
            $rt.="<td>".$row['NUM']."</td>";
            $rt.="<td></td>";
            $rt.="<td></td>";
            $rt.="<td></td>";
            $rt.="<td></td>";
            $rt.="<td></td>";
            $rt.="<td></td>";
            $rt.="</tr>";
        }
        echo $rt;
Sign up to request clarification or add additional context in comments.

2 Comments

I have another ques. What is the problem with this query for getting same thing from different table. $q = "SELECT t1.class, count() as NUM, payment_group_id, count() as NUM1 from ipsc_student as t1 GROUP BY class, studentwise_group_info_general as t2 GROUP BY payment_group_id WHERE t1.e_id = t2.e_id";
count function required a param it should be like count(*)

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.