简体   繁体   中英

PHP outputs the wrongly formatted html table

I'm having trouble echoing data into a HTML table.

It comes out like that:

错了

But it should be:

纠正一个

Here's the code. What am I doing wrong?

<?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";
        }

    }
?>

The problem is that you keep outputting a new table for each iteration.

Your code should look like this:

<?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";
    }

}
?>

just put echo "<table>"; and first tr creation statment out side of while loop and also put closing of table after finishing of while loop and see i am sure it will work.

Try

<?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>";
?>

use this code, you have to first start the table, use while loop to iterate the result, and then close the table.

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>";

Remove table code from while loop and put outside.

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>";

replace your if block, use the following code , hope it may help you

if(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>";
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM