siamsam Posted September 3, 2010 Share Posted September 3, 2010 Hello, freaks. I need a little help with the best way to go about this. I have a field in mysql that contains a comma separated string of values. What I need to do is get those values out of the db and list them into editable text fields so the user can update the values in a html form. So far this is my code, I left out all the in betweens - validation, results, etc - for this post...but its there . SELECT servs FROM services WHERE id = $a_id $services = $row['servs']; $srvs = explode(',', $services); Then for the form: <?php foreach ($srvs as $service) { ?> <input type="text" name="servs[]" value="<?php echo $service ?>" /> <?php } ?> This gives me a text field for each value of servs, great! But the problem is, the user can enter up to 15 servs. Some currently have less than that saved in the db. I want to give them the ability to 1. edit any of the current servs value and 2. add more values if they have less than 15. My question is, how do I echo out 15 editable text fields for servs[] no matter the number of values they currently have? And when I go to implode the values, will blank text fields screw everything up? I can't find anything in the forums that relate to this. I believe it probably will involve $i, but I am not too familiar with counting in php yet, so I could really use some help. Thanks! Oh - and I am not stuck on using text fields, but I couldn't figure how to make this work using a select list or checkboxes. Any suggestions either way would help. Quote Link to comment https://forums.phpfreaks.com/topic/212427-edit-array-with-html-form-question/ Share on other sites More sharing options...
wildteen88 Posted September 3, 2010 Share Posted September 3, 2010 You'd use a for loop to generate the 15 text fields, rather than a foreach loop. An example <?php for($i = 0; $i < 15; $i++): $service = isset($srvs[$i]) ? $srvs[$i] : ''; ?> <input type="text" name="servs[]" value="<?php echo $service ?>" /> <?php endfor; ?> Quote Link to comment https://forums.phpfreaks.com/topic/212427-edit-array-with-html-form-question/#findComment-1106774 Share on other sites More sharing options...
siamsam Posted September 3, 2010 Author Share Posted September 3, 2010 Great thanks! That worked! Quote Link to comment https://forums.phpfreaks.com/topic/212427-edit-array-with-html-form-question/#findComment-1106925 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.