shane85 Posted April 2, 2010 Share Posted April 2, 2010 hey guys so I have a form that im submitting to my db and in that form there is a spot for "host locations" im doing this with check boxes so that the user can select 1 or multiple boxes, depending on what is required. This information is then submitted into a field called a_locations Now I also have it so that this record can be edited. So all the other info in the table (first name, last, email, etc) are put into txt fields and in the value im just echoing the $variable to show it, and then it is editable. However im not sure how I would do this for check boxes? If the information is stored into my db and seperated by - lets say, should I explode that, but into an array, and then and if/then statement so that if the variable exists, check the box? or am I getting too complex with this or going in the complete opposite direction? not sure how to accomplish this. Thanks in advance Quote Link to comment Share on other sites More sharing options...
coupe-r Posted April 2, 2010 Share Posted April 2, 2010 If you have everything in your DB already and we're just talking about check boxes, you can do this. $var = $row['column from DB']; <input name="NAME" type="checkbox" id="NAME" <?php if($var == 1) {echo 'checked="checked"';} ?> /> This will make the check box checked if the value equals 1. Does that work for you? Quote Link to comment Share on other sites More sharing options...
shane85 Posted April 2, 2010 Author Share Posted April 2, 2010 hmm it looks like it might....what about if theres multiple records entered into the same row though? for instance in my row a_locations if I have place1-place2-place5-place6 and I want to display 6 checkboxes, but I only want to have place1,place2,place5, and place6 checked Quote Link to comment Share on other sites More sharing options...
coupe-r Posted April 2, 2010 Share Posted April 2, 2010 Im not sure I follow. Each check box on your page should have their own database attribute / column. Could you explain more? Quote Link to comment Share on other sites More sharing options...
shane85 Posted April 2, 2010 Author Share Posted April 2, 2010 hmm maybe I should go about it a different way then??? basically in my database I have a column in my table called a_locations what I want to do is store locations in there, whether it be a single location or multiple ones. I dont ant to do a text field because I want them to choose from the locations I have on the page. A drop down wont work either cause then its just going to be the 1 location they can select. What I was thinking is checkboxes so that they could check off the ones they want. However the problem with that is now when they go to edit the record, how do I display what ones are checked off? Quote Link to comment Share on other sites More sharing options...
ignace Posted April 2, 2010 Share Posted April 2, 2010 Im not sure I follow. Each check box on your page should have their own database attribute / column. Could you explain more? No it doesn't have to. He explodes the value within the column so something like this will work: $checkboxes = array('value1', 'value2', 'value3', 'value4', ..); // values for your checkboxes $check = explode('-', $row['a_locations']); foreach ($checkboxes as $key => $checkbox) { echo '<input type="checkbox" id="', $checkbox . $key, '" name="a_locations[]" value="', $checkbox, '"', (in_array($checkbox, $check) ? ' checked="checked"' : ''), '>', '<label for="', $checkbox . $key, '">', $checkbox, '</label>'; } Quote Link to comment Share on other sites More sharing options...
shane85 Posted April 2, 2010 Author Share Posted April 2, 2010 ignace: I think were on the right track but still not all the way there. im still a little confused. I modified the code and tried using the following $checkboxes = array('$check[0]', '$check[1]', '$check[2]', '$check[3]'); // values for your checkboxes $check = explode('-', $arrEntry['a_locations']); foreach ($checkboxes as $key => $checkbox) { echo '<input type="checkbox" id="', $checkbox . $key, '" name="a_locations[]" value="', $checkbox, '"', (in_array($checkbox, $check) ? ' checked="checked"' : ''), '>', '<label for="', $checkbox . $key, '">', $checkbox, '</label>'; } now, my first problem as you will see is its not displaying the variable in the array, it just echos $check[0] $check[1] etc if I just echo not in the array $check[0] the explode works, and it just displays whatever is in the database field before the first - Now, initially you had it as value1, value2, value3, value4 etc. I only want to have as many values as I do entries in my database seperated by -. Sorry im still confused but I think were on the right track. Any more help is appreciated. Thanks Quote Link to comment 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.