I've a HTML form with approx. 120 input text fields.
Then i've a PHP page with the same form where i've this code to show the value get from database.
<input type="text" id="field_1" name="field_1" value="<?php echo $myvalue1; ?>" />
Is there a way to retrieve data from database and fill the input text value for each field or doing it in a better way than specify more than 120 variables?
Thanks!
There's a trick in PHP. You can name your inputs using "[]" operator. like this:
<input type="text" id="field_1" name="field[]" value="<?php echo $myvalue1; ?>" />
As the result it will be treated as an array in the corresponding REQUEST array (POST or GET, depends of HTTP method for you form). To fill the inputs, simply use a loop.
You can always store the results in an array, then loop through that.
$q = mysql_query ( ... );
for ($i = 1; $r = mysql_fetch_row ($q); ++$i)
printf ('<input tyle="text" id="field_%s" name="field_%s" value="%s"/>', $i, $i, $r[0]);
Loop through your results from the query and generate textboxs
<?php
$sql = "SELECT column FROM table";
if ($result = $mysqli->query($sql)) {
while($obj = $result->fetch_object()){
echo "<input type='text' name='field[]' value='". $obj->column ."'> <br>"; // Here is the where you put your textbox in loop
}
}
$result->close();
?>