0

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

It comes out like that:

Wrong one

But it should be:

Correct one

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

    }
?>

5 Answers 5

2

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

}
?>
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

0

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

Comments

0

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.