2013-08-21 42 views
-2

我是新來的PHP和MySQL,並且對不起,如果這是一個新手的錯誤。如何從MySQL表中獲取行並將其填充到html表中

我有關於從MySQL中獲取數據,雖然時間和在HTML表格填充

所以我的腳本如下:

<html> 
<head> 

</head> 
<body> 
    <div class="well"> 
     <table class="table table-striped"> 
      <?php 
      $userDetail = mysql_query("SELECT * FROM uyePopup ORDER BY id LIMIT 100") or die(mysql_error()); 
      $useridTable = ""; 
      $userGenderTable = ""; 
      $userMailTable = ""; 
      while ($userRow = mysql_fetch_array($userDetail)) { 

       $useridTable.= '<td>' . $userRow['id'] . '</td>'; 
       if ($userRow['gender'] == 1) { 
        $userRow['gender'] = 'M'; 
       } else { 
        $userRow['gender'] = 'F'; 
       } 
       $userGenderTable.= '<td>' . $userRow['gender'] . '</td>'; 
       $userMailTable.= '<td>' . $userRow['email'] . '</td>'; 
      } 
      // echo $userTable; 
      ?> 
      <thead> 
      <tr> 
       <th>ID</th> 
       <th>Gender</th> 
       <th>E-mail</th> 
      </tr> 
      </thead> 
      <tbody> 
      <tr> 
       <?= $useridTable ?> 
      </tr> 
      <tr> 
       <?= $userGenderTable ?> 
      </tr> 
      <tr> 
       <?= $userMailTable ?> 
      </tr> 
      </tbody> 
     </table> 
    </div> 
</body> 
</html> 

,所以我有一個這樣的輸出:http://i.stack.imgur.com/FIvyj.png

我谷歌很長一段時間,但似乎沒有工作。

回答

1

這是未經測試,但這樣的事情應該工作:

<html> 
<head> 

</head> 
<body> 
    <div class="well"> 
     <table class="table table-striped"> 
      <thead> 
      <tr> 
       <th>ID</th> 
       <th>Gender</th> 
       <th>E-mail</th> 
      </tr> 
      </thead> 
      <tbody> 
      <?php 
      $userDetail = mysql_query("SELECT * FROM uyePopup ORDER BY id LIMIT 100") or die(mysql_error()); 
      while ($userRow = mysql_fetch_array($userDetail)) { 
       echo '<tr><td>' . $userRow['id'] . '</td>'; 
       echo '<td>'.($userRow['gender'] == 1 ? 'M' : 'F').'</td>'; 
       echo '<td>' . $userRow['email'] . "</td></tr>\n"; 
      } 
      ?> 
      </tbody> 
     </table> 
    </div> 
</body> 
</html> 
相關問題