I want to use while loop in html. I have the following php code. But it does not work. How can i solve this problem? Please any one help me.

while($row = mysql_fetch_array($result))
            {
                echo "<tr>";
                echo "<td>" . $row['ID'] . "</td>";
                echo "<td>"; echo "<input name="txtf" type="text">"; echo "</td>";
                echo "<td>" . <input name="txtf1" type="text" /> . "</td>";
                echo "<td>" . <input name="txtf2" type="text" /> . "</td>";
                echo "</tr>";
            }

";

<? while($row = mysql_fetch_array($result)) {  ?>
    <tr>
        <td><?php echo $row['ID']; ?></td>
        <td><input name="txtf" type="text"></td>
        <td><input name="txtf1" type="text" /></td>
        <td><input name="txtf2" type="text" /></td>
    </tr>
<? } ?>
commented: Thankyou +0

There is no error in ur code...
Check if the table is returning any value or not(any data correspnding to ur query).....

The code has errors. You are missing quotes arround strings in lines 6 and 7:

echo "<td>" . '<input name="txtf1" type="text" />' . "</td>";
echo "<td>" . '<input name="txtf2" type="text" />' . "</td>";

Since you do not know whether you are getting all the fields from the database as you would expect it is a good practice to first check for the existence of values first. And you can actually join some echo statements into one. Here is the optimized version:

while($row = mysql_fetch_array($result))
{
    // if $row['ID'] is set assign it to a variable
    // otherwise assign a default value to the variable
    if(isset($row['ID'])) {
        $id = $row['ID'];        
    } else {
        $id = 'Error: No ID!';
    }

    // use the variable directly in double quoted string
    echo "<tr><td>$id</td>";
    // use single quotes elsewhere so you can use double quotes for html attributes
    echo '<td><input name="txtf" type="text"></td>';
    echo '<td><input name="txtf1" type="text" /></td>';
    echo '<td><input name="txtf2" type="text" /></td></tr>';
}
commented: Thankyou +0

hey, there are many errors in your code.
you have to modify it to be like this

    <?php while($row = mysql_fetch_array($result))
    {
    echo "<tr>"; //this one is good
    echo "<td>" . $row['ID'] . "</td>"; // this one is good
    echo "<td>"; echo "<input name=\"txtf\" type=\"text\">"; echo "</td>";
    echo "<td>" . "<input name=\"txtf1\" type=\"text\" />" . "</td>";
    echo "<td>" . "<input name=\"txtf2\" type=\"text\" />" . "</td>";
    echo "</tr>"; // this one is good
    }
    ?>

just copy and paste this and you will be fine :)

@mamdouh ramadan
Your corrections are OK but there is a less complicated way of doing it (without escaping and concatenation). Just use single quotes and no concatenation of strings (as in my previous post):

echo '<td><input name="txtf" type="text"></td>';
echo '<td><input name="txtf1" type="text" /></td>';
echo '<td><input name="txtf2" type="text" /></td></tr>';

It is easier to debug and maintain.

@Shougat
I dont know if you are familiar with many ways of quoting strings. In case you are not, see this link:

http://php.net/manual/en/language.types.string.php

When you are familiar with all this stuff about strings it helps a lot :-)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.