2013-02-28 90 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>"; 
}