如何使用php

时间:2018-11-08 11:14:26

标签: php mysql

    <?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中使用usings循环。

数据库学生表由n条记录组成

2 个答案:

答案 0 :(得分:1)

您是否有不想使用查询的特定原因?

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 |
+--------+------+----------+-------+

答案 1 :(得分:0)

<?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>';
}