php //我该怎么做才能显示所有课程?

时间:2017-05-12 22:01:58

标签: php

我需要一次显示所有课程

在我的代码中只显示一个课程我应该怎么做才能修复它? 当我点击删除它重新加载页面并显示下一个课程 我的代码::     

$course = $conn->query("select * from student_course where student_id = $id ") ;
if($course->num_rows > 0) { 
    ?> 
    <table class="table" >
    <tr> 
        <thead>
            <th> courseName </th>
            <th> courseID </th>
            <th> teacher </th>
            <th> DELETE </th>
        </thead> </tr>
    <?php 

while ($Scourse = $course->fetch_assoc()){ 
    $cid=$Scourse['course_id'];
    $tid=$Scourse['teacher_id']; 
        $teacher = $conn->query("select * from teacher where id = $tid ") ;
        if($teacher->num_rows > 0) {


        while($teacherC=$teacher->fetch_assoc()){

        $course = $conn->query("select * from course where id = '$cid' ") ;
        if($course->num_rows>0){
        while($Ncourse=$course->fetch_assoc()){ ;
 ?>  
 <tr> <td><?php echo $Ncourse['name']; ?> </td>
 <td><?php echo $Scourse['course_id']; ?> </td>
 <td> <?php echo $teacherC['name']; ?> </td>
 <td><form method="post"><button type="submit" name="<?php echo $cid ?>"> Delete </button></form> </td></tr>

<?PHP 

    if (isset($_POST["$cid"])){

    $dcourse = $conn->query("delete from student_course where course_id = '$cid' and 
        student_id=$id ") ;


    header("location:scourse.php");

}
}
} 
}
}
}
}

</table>  

我尝试删除老师和课程查询,因为我需要

1 个答案:

答案 0 :(得分:0)

您正在不正确地处理关联数组。 您将需要一个foreach循环来遍历关联数组中的每一行。

示例:

foreach($Scourse as $x) {
    echo $x['name'];
    echo $x['course_id'];
}

请在此处查看循环关联阵列: https://www.w3schools.com/php/php_arrays.asp

编辑:请参阅@Qirel评论