jeff5656 Posted April 5, 2010 Share Posted April 5, 2010 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 More sharing options...
mikesta707 Posted April 5, 2010 Share Posted April 5, 2010 <?php echo $row['f'.$f.'_name']; ?> Link to comment https://forums.phpfreaks.com/topic/197591-escaping-quotes-inside-an-array/#findComment-1036980 Share on other sites More sharing options...
jeff5656 Posted April 5, 2010 Author Share Posted April 5, 2010 Oh is that all? That's easy. Thanks! Link to comment https://forums.phpfreaks.com/topic/197591-escaping-quotes-inside-an-array/#findComment-1036981 Share on other sites More sharing options...
ignace Posted April 5, 2010 Share Posted April 5, 2010 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 https://forums.phpfreaks.com/topic/197591-escaping-quotes-inside-an-array/#findComment-1037064 Share on other sites More sharing options...
trq Posted April 5, 2010 Share Posted April 5, 2010 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 https://forums.phpfreaks.com/topic/197591-escaping-quotes-inside-an-array/#findComment-1037065 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.