单行重复而不是显示

时间:2014-10-07 20:29:56

标签: php mysql

我是PHP / MySQL的新手,所以请耐心等待我。

我正在尝试获取表格中的成员列表以显示在页面上。现在,它显示第一个成员大约10次,而不显示任何其他人的名字。我有它工作,但我不知道发生了什么。我只想让它一次显示每个人的名字。这是我的代码:

<?php $select = mysql_query("SELECT * FROM `member_staff` WHERE `username`='$_SESSION[USR_LOGIN]' AND `status`='Active'");
$row = mysql_fetch_array($select);
$rows = mysql_num_rows($select);
$teaching = $row[teaching];
if ($rows==0){echo "Sorry, you don't appear to be a professor.";}
else { ?>

<?php }
    $select2 = mysql_query("SELECT * FROM `classes_enrolled` WHERE `course`='" . $teaching . "' ORDER BY `student_name`") or die(mysql_error());
    $count = mysql_num_rows($select2);
    $row2 = mysql_fetch_array($select2);
    $student=$row2[student_name];
    if($count==NULL) {
        echo "<table width=\"80%\">\n";
        echo "<tr><td><center>Nobody has registered for your class yet!</center></td></tr>\n";
        echo "</table>\n";
        echo "<br /><br />\n\n";
    }

    else {
        echo "<center><font size=\"3\"><b>YEAR 1, TERM 2</b></font></center>";
        echo "<table width=\"80%\" class=\"table-stripes\">\n";
        echo "<tr><td width=\"50%\"><b>STUDENT</b></td></tr>\n";
    $select3 = mysql_query("SELECT * FROM `members` WHERE `username`='" . $student . "'") or die(mysql_error());
    $row3 = mysql_fetch_array($select3);
        while($row2 = mysql_fetch_array($select2)) {
        $house=$row3[house];
        echo "<tr><td><strong class=\"$house\">$student</strong></td></tr>";
        }
    echo "</table>"; }
?>

1 个答案:

答案 0 :(得分:0)

我想念你的代码,因为它很乱,但是忽略了mysqli和mysql的事情,你想要显示老师的课程中有多少学生。

<?php $select = mysql_query("SELECT * FROM `member_staff` WHERE `username`='$_SESSION[USR_LOGIN]' AND `status`='Active'");
$row = mysql_fetch_array($select);
$rows = mysql_num_rows($select);
$teaching = $row[teaching];  <--- This only get first row of the course, if you want multiple course under same username, you need to loop it.
if ($rows==0){echo "Sorry, you don't appear to be a professor.";}
else { ?>

<?php }
    $select2 = mysql_query("SELECT * FROM `classes_enrolled` WHERE `course`='" . $teaching . "' ORDER BY `student_name`") or die(mysql_error());
    $count = mysql_num_rows($select2);
    $row2 = mysql_fetch_array($select2);
    $student=$row2[student_name];  <----- This only get the first row of the student name, if you want multiple student under a course, you need to loop it.
    if($count==NULL) {
        echo "<table width=\"80%\">\n";
        echo "<tr><td><center>Nobody has registered for your class yet!</center></td></tr>\n";
        echo "</table>\n";
        echo "<br /><br />\n\n";
    }

    else {
        echo "<center><font size=\"3\"><b>YEAR 1, TERM 2</b></font></center>";
        echo "<table width=\"80%\" class=\"table-stripes\">\n";
        echo "<tr><td width=\"50%\"><b>STUDENT</b></td></tr>\n";
    $select3 = mysql_query("SELECT * FROM `members` WHERE `username`='" . $student . "'") or die(mysql_error());
    $row3 = mysql_fetch_array($select3);
        while($row2 = mysql_fetch_array($select2)) {
        $house=$row3[house];  <----This only show the first row of $house under same student, so you need to loop it too.
        echo "<tr><td><strong class=\"$house\">$student</strong></td></tr>";
        }
    echo "</table>"; }
?>

所以你真正想做的是

<?php 
$select = mysql_query("SELECT * FROM `member_staff` WHERE `username`='$_SESSION[USR_LOGIN]' AND `status`='Active'");
$rows = mysql_num_rows($select);
if ($rows==0){echo "Sorry, you don't appear to be a professor.";}
else { ?>

<?php }
    while( $row = mysqli_fetch_array( $select ) ) {
      $teaching = $row[teaching];
      $select2 = mysql_query("SELECT * FROM `classes_enrolled` WHERE `course`='" . $teaching . "' ORDER BY `student_name`") or die(mysql_error());
      $count = mysql_num_rows($select2);
      if($count==NULL) {
        echo "<table width=\"80%\">\n";
        echo "<tr><td><center>Nobody has registered for your class yet!</center></td></tr>\n";
        echo "</table>\n";
        echo "<br /><br />\n\n";
      } else {

        while( $row2 = mysql_fetch_array($select2) ) {
           $student=$row2[student_name]; 
           echo "<center><font size=\"3\"><b>YEAR 1, TERM 2</b></font></center>";
           echo "<table width=\"80%\" class=\"table-stripes\">\n";
           echo "<tr><td width=\"50%\"><b>STUDENT</b></td></tr>\n";
           $select3 = mysql_query("SELECT * FROM `members` WHERE `username`='" . $student . "'") or die(mysql_error());
           while($row3 = mysql_fetch_array($select3)) {
              $house=$row3[house];
              echo "<tr><td><strong class=\"$house\">$student</strong></td></tr>";
           }
           echo "</table>"; 
        }
    }  // END ELSE
 }
} // END ELSE
?>