输出ul中的每个数组

时间:2017-04-04 23:38:15

标签: php mysql arrays

所以我有一个程序可以获取存储在数据库中的所有团队的数据。当我获取数据时,我将信息存储在数组中。数据是leader_idstudent_id。所以我要做的就是在单独的ul内输出每个数组。

团队数据库

id      | leader_id | student_id
1       | 123       | 0987
2       | 123       | 1234345
3       | 97678     | 56766
4       | 97678     | 67558

因此,每个leader_id可以包含1个或多个student_id

此外,如果student_id具有相同的leader_id,则其与具有相同student_id <的所有其他leader_id相同的团队/ p>

我正在尝试做什么

<ul>

     <li>123</li>
     <li>0987</li>
     <li>1234345</li>         

</ul>

<ul>

     <li>97678</li>
     <li>56766</li>
     <li>67558</li>    

</ul>

我目前正在

<ul>

     <li>123</li>
     <li>0987</li>
     <li>1234345</li>
     <li>97678</li>
     <li>56766</li>
     <li>67558</li>

</ul>

我尝试过的事情

我尝试过这样做(将循环置于ul):

<ul>

    <?php 

        foreach ($students_arr as $student) {
            echo "
                <li>$student</li>
            ";
        }

     ?>

 </ul>

输出了这个(只有一支球队):

<ul>
    <li>123</li>
    <li>0987</li>
    <li>1234345</li>         
</ul>

我的PHP

<!DOCTYPE html>
<html>
<head>
    <style type="text/css">

        * {
            margin: 0;
            padding: 0;
        }

        ul {
            padding: 20px;
            display: inline-block;
        }

        li {
            list-style: none;
            padding: 10px;
        }

    </style>
</head>
<body>

    <ul>

        <?php 

            // GET TEAMS

            // turn error reporting off
            error_reporting(0);

            // get connect.php file
            require '../connect.php';

            // get all team data
            $team_data = mysqli_query($link, "SELECT leader_id, GROUP_CONCAT(student_id SEPARATOR ', ')
                AS students_id FROM teams GROUP BY leader_id;");

            // check to see if query works
            if($team_data) {

                while($team = mysqli_fetch_array($team_data)) {

                    // output the teams data
                    $leader_id = $team['leader_id'];
                    $students_id = $team['students_id'];

                    $students_arr = explode(", ",$students_id);
                    $leader_first = array_unshift($students_arr, $leader_id);

                    foreach ($students_arr as $student) {
                        echo "
                            <li>$student</li>
                        ";
                    }
            }

            } else {

                header("Location: ../../admin.php?message=Sorry we could not get the data");

            }

         ?>

    </ul>

</body>
</html>

1 个答案:

答案 0 :(得分:0)

<?php

// GET TEAMS

// turn error reporting off
error_reporting(0);

// get connect.php file
require '../connect.php';

// get all team data
$team_data = mysqli_query($link, "SELECT leader_id, student_id FROM teams order by leader_id, student_id;");

// check to see if query works
if(!$team_data) {
    header("Location: ../../admin.php?message=Sorry we could not get the data");
}

// build an associative array of data, keep your data rendering and sql separate
$data = [];

while($team = mysqli_fetch_array($team_data)) {
    $leader_id = $team['leader_id'];
    $student_id = $team['student_id'];
    $data[$leader_id][] = $student_id;
}


// no sql beyond here
?>
<!DOCTYPE html>
<html>
<head>
    <style type="text/css">

        * {
            margin: 0;
            padding: 0;
        }

        ul {
            padding: 20px;
            display: inline-block;
        }

        li {
            list-style: none;
            padding: 10px;
        }

    </style>
</head>
<body>

<?php foreach ($data as $leader_id => $students): ?>
<ul>
    <li><strong><?php echo $leader_id; ?></strong></li>
    <?php foreach ($students as $student_id): ?>
        <li><?php echo $student_id; ?></li>
    <?php endforeach; ?>
</ul>
<?php endforeach; ?>

</body>
</html>