2013-02-28 36 views
0

我無法將數據回顯到HTML表格中。PHP輸出格式錯誤的html表格

它出來這樣的:

Wrong one

但它應該是:

Correct one

下面的代碼。我究竟做錯了什麼?

<?php 
$query = $_POST['query']; 
$min_length = 1; 

if(strlen($query) >= $min_length){ // if query length is more or equal minimum length then 

    $query = htmlspecialchars($query); 
    $query = mysql_real_escape_string($query);   
    $raw_results = mysql_query("SELECT * FROM norse5_proov 
     WHERE (`model` LIKE '%".$query."%') OR (`year` LIKE '%".$query."%')") or die(mysql_error()); 

    if(mysql_num_rows($raw_results) > 0){ // if one or more rows are returned do following 

     while($results = mysql_fetch_array($raw_results)){ 
      echo "<table>"; 
      echo "<tr>"; 
        echo "<td>Model name</td>"; 
      echo "<td>Year</td>"; 
      echo "</tr>"; 
      echo "<td>".$results['mudeli_nimetus']."</td>"; 

      echo "<td>".$results['soetusaasta']."</td>"; 
      echo "<br>"; 
      echo "</table>"; 
     } 

    } 
    else{ 
     echo "No results"; 
    } 

} 

?>

回答

2

的問題是,你保持輸出新表中的每個迭代。

你的代碼看起來應該是這樣:

<?php 
$query = $_POST['query']; 
$min_length = 1; 

if(strlen($query) >= $min_length){ // if query length is more or equal minimum length then 

    $query = htmlspecialchars($query); 
    $query = mysql_real_escape_string($query);   
    $raw_results = mysql_query("SELECT * FROM norse5_proov 
     WHERE (`model` LIKE '%".$query."%') OR (`year` LIKE '%".$query."%')") or die(mysql_error()); 

    if(mysql_num_rows($raw_results) > 0){ // if one or more rows are returned do following 

     echo "<table>"; // Start the table 
     // Output the table headers 
     echo "<tr>"; 
     echo "<td>Model name</td>"; 
     echo "<td>Year</td>"; 
     echo "</tr>"; 

     while($results = mysql_fetch_array($raw_results)) { 
      echo "<tr>"; 
      echo "<td>".$results['mudeli_nimetus']."</td>"; 
      echo "<td>".$results['soetusaasta']."</td>"; 
      echo "<br>"; 
      echo "</tr>"; 
     } 

     echo "</table>"; // End the table 

    } 
    else{ 
     echo "No results"; 
    } 

} 
?> 
+1

謝謝!成功了! – 2013-02-28 09:40:09

+0

@ErlendAnderson沒問題。 :) – 2013-02-28 13:54:12

0

只是把echo "<table>";和第一tr創造出statment一邊循環,也放table收盤while循環完成後,看我敢肯定它會工作。

嘗試

<?php 
    echo "<table><tr><td>Model name</td><td>Year</td></tr>"; 

    while($results = mysql_fetch_array($raw_results)) 
    { 
     echo "<tr><td>".$results['mudeli_nimetus']."</td><td>".$results['soetusaasta']."</td></tr>"; 
    } 
    echo "</table>"; 
?> 
0

使用此代碼,您必須首先啓動的表,使用while循環迭代的結果,然後關閉表。

echo "<table>"; 
echo "<tr>"; 
echo "<td>Model name</td>"; 
echo "<td>Year</td>"; 
echo "</tr>"; 
while($results = mysql_fetch_array($raw_results)){ 
    echo "<tr>"; 
    echo "<td>".$results['mudeli_nimetus']."</td>"; 
    echo "<td>".$results['soetusaasta']."</td>"; 
    echo "</tr>";   
} 
echo "</table>"; 
0

從while循環刪除代碼,並把外面。

echo "<table>"; 
echo "<tr>"; 
echo "<td>Model name</td>"; 
echo "<td>Year</td>"; 
echo "</tr>"; 


while($results = mysql_fetch_array($raw_results)){ 
      echo "<tr>"; 
      echo "<td>".$results['mudeli_nimetus']."</td>"; 

      echo "<td>".$results['soetusaasta']."</td>"; 

      echo "</tr>"; 
} 

echo "</table>"; 
0

取代你,如果塊,使用下面的代碼,希望它可以幫助你

如果(mysql_num_rows($ raw_results)> 0){

 echo "<table>"; 
     echo "<tr>"; 
     echo "<td>Model name</td>"; 
     echo "<td>Year</td>"; 
     echo "</tr>"; 
    while($results = mysql_fetch_array($raw_results)){ 
     echo "<tr>"; 
     echo "<td>".$results['mudeli_nimetus']."</td>"; 
     echo "<td>".$results['soetusaasta']."</td>"; 
     echo "</tr>"; 
    }echo "</table>"; 
}