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
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], '">';
        }
    }
}

Link to comment
Share on other sites

Or considering the fact that you have an associative array.....

 

if ($result) {
  while ($row = mysql_fetch_assoc($result)) {
    foreach ($row as $k => $v) {
      echo '<input type="text" name="', $k, '[]" value="', $v, '">';
    }
  }
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.