Jump to content

edit array with html form question


siamsam

Recommended Posts

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.  :D

Link to comment
Share on other sites

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; ?>

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.