Jump to content

escaping quotes inside an array?


jeff5656

Recommended Posts

I have names called f1_name, f2_name all the way to 17 (I get these from a queried database).  I want to write a for loop so I don't have to write them all out in a form.  Here is my code.  Look at this array: $row['f1_name']

<?php for ($f=1;$f<18;$f++){
?>
<td bgcolor="#FFFFFF">F<?php echo $f;?></td>
<td >
<input name="f<?php echo $f.'_name';?>" type="text" size="20" value="<?php echo $row['f1_name']; ?>" />
</td>
</tr>
<?php } ?>

 

How do I fix that so that the For-loop inserts the correct number for $row['fx_name']

(where x is $ff)?

 

Hope that's clear! :-)

Link to comment
https://forums.phpfreaks.com/topic/197591-escaping-quotes-inside-an-array/
Share on other sites

There are functions called mysql_num_fields, mysql_field_name and mysql_fetch_field which returns all fields from the result use as:

 

function get_fields($result) {
    $fields = array();
    $field_count = mysql_num_fields($result);
    for ($i = 0; $i < $field_count; ++$i) {
        $fields[] = mysql_field_name($result, $i);
    }
    return $fields;
}

$query = 'SELECT field1, field2, field3 FROM table';
$result = mysql_query($query);
if ($result) {
    $fields = get_fields($result);
    while ($row = mysql_fetch_assoc($result)) {
        foreach ($fields as $field) {
            //$_POST[$field] == array()
            echo '<input type="text" name="', $field, '[]" class="', $field, '" value="', $row[$field], '">';
        }
    }
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.