如何使用php中的循环计算学习同一分支、年份、学期的学生数

7gyucuyw  于 2021-06-18  发布在  Mysql
关注(0)|答案(2)|浏览(317)
<?php
$link = mysqli_connect("localhost", "root", "", "college");

if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

$query = "SELECT branch,year,semester FROM students ORDER by branch,year,semester";
$result = mysqli_query($link,$query);
$rt= mysqli_fetch_all($result,MYSQLI_ASSOC);

$depCount = [];

foreach($rt as $s) {
    if ($depCount["$s['branch']-$s['year']-$s['semester']-"]) 
    {
        $depCount["$s['branch']-$s['year']-$s['semester']-"]++;
    } else {
        $depCount["$s['branch']-$s['year']-$s['semester']-"] = 1;
    }
}
var_dump($depCount);

?>

这是我的“分年级学期”表

name branch year semester
    name1   cse 1   1
    name9   cse 1   1
    name13  cse 1   1
    name5   cse 1   1
    name26  cse 2   1
    name6   cse 2   2
    name10  cse 2   2
    name4   cse 2   4
    name2   cse 3   2
    name8   cse 3   3
    name11  cse 3   3
    name3   cse 4   3
    name8   cse 4   4
    name12  cse 4   4
    name20  ece 1   4
    name25  ece 1   4
    name14  ece 2   2
    name21  ece 2   2
    name17  ece 2   3
    name18  ece 3   1
    name22  ece 3   3
    name15  ece 3   3
    name19  ece 4   2
    name24  ece 4   2
    name23  ece 4   4
    name16  ece 4   4
    name42  eee 1   1
    name34  eee 1   4
    name29  eee 2   1
    name43  eee 2   2

现在我想要像这样的输出

cse 1 1 count 4
    cse 2 1 count 1
    cse 2 2 count 2
    so on...

我想显示学习相同“分支、年份、学期”的学生数。在php中使用循环。
数据库students表由n个记录组成

l0oc07j2

l0oc07j21#

您不想使用查询的具体原因是什么?

SELECT branch, year,semester, COUNT(*) total FROM students GROUP BY branch,year,semester;
+--------+------+----------+-------+
| branch | year | semester | total |
+--------+------+----------+-------+
| cse    |    1 |        1 |     4 |
| cse    |    2 |        1 |     1 |
| cse    |    2 |        2 |     2 |
| cse    |    2 |        4 |     1 |
| cse    |    3 |        2 |     1 |
| cse    |    3 |        3 |     2 |
| cse    |    4 |        3 |     1 |
| cse    |    4 |        4 |     2 |
| ece    |    1 |        4 |     2 |
| ece    |    2 |        2 |     2 |
| ece    |    2 |        3 |     1 |
| ece    |    3 |        1 |     1 |
| ece    |    3 |        3 |     2 |
| ece    |    4 |        2 |     2 |
| ece    |    4 |        4 |     2 |
| eee    |    1 |        1 |     1 |
| eee    |    1 |        4 |     1 |
| eee    |    2 |        1 |     1 |
| eee    |    2 |        2 |     1 |
+--------+------+----------+-------+
jgovgodb

jgovgodb2#

<?php

$students[0] = array("name" => 'a', "branch" => 'x', "semester" => 1, "year" => 1);
$students[1] = array("name" => 'b', "branch" => 'y', "semester" => 2, "year" => 1); 
$students[2] = array("name" => 'c', "branch" => 'y', "semester" => 2, "year" => 1);

$depCount = [];

foreach ($students as $s) {    
    $key = $s["branch"] . '-' . $s["year"] . '-' . $s["semester"];
    if (isset($depCount[$key])) {
        $depCount[$key]++;
    } else {
        $depCount[$key] = 1;
    }
}

foreach ($depCount as $k => $d) {
    $cat = explode('-', $k);
    echo $cat[0] . ' ' . $cat[1] . ' ' . $cat[2] . ' count ' . $d . '<br>';
}

相关问题