Jump to content

help plz...best way to do this...pulling info from db and checking check boxes


shane85

Recommended Posts

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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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?

 

Link to comment
Share on other sites

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>';
}

Link to comment
Share on other sites

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

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.