2014-01-12 23 views
0

我是新來的PHP,我試着解決這個很多次,但我不能。 這是我的php代碼 有人可以幫助我,這應該是一個PHP專家很容易。在PHP中的HTML表錯誤

<?php 
require_once 'connector.php'; 
$result = mysql_query('SELECT * FROM highscores ORDER BY score DESC'); 
$username = mysql_query('SELECT username FROM users WHERE id in(SELECT user_id FROM highscores)'); 
echo"<html> 
     <head> 
     <title>Highscores</title> 
     </head> 
    <body> 
       <table border='1'> 
     <tr> 
      <th>user</th> 
      <th>score</th> 
      <th>Date</th> 
     </tr> 
      "; 
      while ($name = mysql_fetch_array($username)) 
       { 
        echo "<tr> 
         <td>" . $name ['username'] . "</td>";    
       }  
      while($row = mysql_fetch_array($result)) 
       {  
        echo" 
        <td>" . $row ['score'] . "</td> 
        <td>" . $row ['date'] . "</td> 
        </tr>"; 
       } 
    echo" 
    </table> 
    </body> 
    </html> 
"; 

the table i want to take

+1

代替執行2個查詢試試,看看你是否能與一個使用連接 –

+0

做請儘量描述,你的挑戰是和顯示你已經嘗試解決它。還要描述,你想達到的結果是什麼。對於想要幫助的人來說,通過遵循外部鏈接總是很難首先找出問題;-) –

回答

0

你可以試試這個,

$result = mysql_query('SELECT hs.score, hs.date, u.username FROM highscores as hs, users as u where hs.user_id=u.id ORDER BY score DESC'); 
    echo "<html> 
      <head> 
      <title>Highscores</title> 
      </head> 
     <body> 
        <table border='1'> 
      <tr> 
       <th>user</th> 
       <th>score</th> 
       <th>Date</th> 
      </tr> 
       "; 

     while($row = mysql_fetch_array($result)) 
      {  
       echo"<tr> 
       <td>" . $row ['username'] . "</td> 
       <td>" . $row ['score'] . "</td> 
       <td>" . $row ['date'] . "</td> 
       </tr>"; 
      } 
+0

謝謝你的完美。 – user3187574

0

而不是把一切都在一個雙引號的,你可以用與(。)運算符字符串連接。例如,而不是寫這樣

echo"<html> 
    <head> 
    <title>Highscores</title> 
    </head> 
<body> 
      <table border='1'> 
    <tr> 
     <th>user</th> 
     <th>score</th> 
     <th>Date</th> 
    </tr> 
     "; 

的你可以這樣寫:

echo "<html>" . 
     "<head>" . 
     "<title>Highscores</title>" 
     "</head>" . 
     "<body>" . 
     "<table border='1'>" . 
     "<tr>" . 
     "<th>user</th>" . 
     "<th>score</th>" . 
     "<th>Date</th>" . 
     "</tr>" ; 

下面的代碼塊

echo "<tr> 
     <td>" . $name ['username'] . "</td>"; 

應該寫成

echo "<tr>" . 
    "<td>" . $name ['username'] . "</td>"; 

這樣代碼看起來像m礦石也是可讀的。

0

第二個循環需要在第一個循環中,並且關閉</tr>不應該在第二個循環中。

<? 
    while ($name = mysql_fetch_array($username)) { 
    echo "<tr>"; 
    echo "<td>" . $name["username"] . "</td>"; 

    while ($row = mysql_fetch_array($result)) { 
     echo "<td>" . $row["score"] . "</td>"; 
     echo "<td>" . $row["date"] . "</td>"; 
    } 

    echo "</tr>"; 
    } 
?> 
1

mysql_ *已棄用!使用mysqli_ *

<?php 
require_once 'connector.php'; 

$SQL = "SELECT u.username, h.score, h.date 
     FROM users AS u 
     JOIN highscores AS h ON (h.user_id = u.users_id)"; 

$result = mysql_query($SQL) or die(mysql_error()); 

echo "<html> 
     <head> 
     <title>Highscores</title> 
     </head> 
     <body>"; 


if(mysql_num_rows($result) > 0) 
{ 
    echo "<table border='1'> 
      <tr> 
      <th>user</th> 
      <th>score</th> 
      <th>Date</th> 
     </tr>"; 

    while ($row = mysql_fetch_array($result)) 
    { 
     echo "<tr>";  

     printf("<td>%s</td>", $row['username']); 
     printf("<td>%s</td>", $row['score']); 
     printf("<td>%s</td>", $row['date']); 

     echo "</tr>"; 
    } 

echo "</table> 
     </body> 
     </html>"; 
} 

mysql_free_result($result);