dongsheng6056 2014-10-07 20:29
浏览 56
已采纳

单行重复而不是显示

I'm pretty new at PHP/MySQL, so please be patient with me.

I am trying to get a list of members in a table to show up on a page. Right now it's showing the first member about 10 times and not displaying anyone else's name. I DID have it working, but I don't know what happened. I just want it to display everyone's name once. Here is my code:

<?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%\">
";
        echo "<tr><td><center>Nobody has registered for your class yet!</center></td></tr>
";
        echo "</table>
";
        echo "<br /><br />

";
    }

    else {
        echo "<center><font size=\"3\"><b>YEAR 1, TERM 2</b></font></center>";
        echo "<table width=\"80%\" class=\"table-stripes\">
";
        echo "<tr><td width=\"50%\"><b>STUDENT</b></td></tr>
";
    $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条回答 默认 最新

  • dpxw7293 2014-10-07 20:39
    关注

    I miss look on your code, since it is mess, but disregard the mysqli and mysql thing, you want to show how many student in the teacher's classes.

    <?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%\">
    ";
            echo "<tr><td><center>Nobody has registered for your class yet!</center></td></tr>
    ";
            echo "</table>
    ";
            echo "<br /><br />
    
    ";
        }
    
        else {
            echo "<center><font size=\"3\"><b>YEAR 1, TERM 2</b></font></center>";
            echo "<table width=\"80%\" class=\"table-stripes\">
    ";
            echo "<tr><td width=\"50%\"><b>STUDENT</b></td></tr>
    ";
        $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>"; }
    ?>
    

    So what you really want to do is

    <?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%\">
    ";
            echo "<tr><td><center>Nobody has registered for your class yet!</center></td></tr>
    ";
            echo "</table>
    ";
            echo "<br /><br />
    
    ";
          } 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\">
    ";
               echo "<tr><td width=\"50%\"><b>STUDENT</b></td></tr>
    ";
               $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
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)