njdirt Posted August 13, 2009 Share Posted August 13, 2009 Hello, I think this is a simple enough problem and I hope somebody knows how to do it. I have a page that has five form options which correspond to a value in a database. If all five options were selected, the value would be '12345' and if the first second and fifth were selected, the value would be "125". I need some way of determining from the value which options are to be selected. Optimally, I'd like something along the lines of: if $value contains "1" then ... if $value contains "2" then ... Any ideas? Thanks in advance! Quote Link to comment Share on other sites More sharing options...
Jessica Posted August 13, 2009 Share Posted August 13, 2009 It might help to put a marker in between the digits, so you can use explode. Such as a . 1.2.3.4.5 or 1.2.4 you can explode on the . and then you'll have an array of all the digits. Quote Link to comment Share on other sites More sharing options...
JonnoTheDev Posted August 13, 2009 Share Posted August 13, 2009 What you should have done is give the field a name of an array type to hold multiple values i.e. If I have 3 checkboxes <input type="checkbox" name="foobar[]" value="1" /> <input type="checkbox" name="foobar[]" value="2" /> <input type="checkbox" name="foobar[]" value="3" /> Then I can get at the selected values using array functions <?php print_r($_POST['foobar']); ?> Quote Link to comment Share on other sites More sharing options...
Jessica Posted August 13, 2009 Share Posted August 13, 2009 neil.johnson I think he's past that part and has stored the values in the database already. Quote Link to comment Share on other sites More sharing options...
njdirt Posted August 13, 2009 Author Share Posted August 13, 2009 Hello, Thanks for the quick replies! I have not yet stored anything in the database. Basically, I'm storing 5 boolean switches in one field in the db. I'm going to see if I can make it work better with the suggestions you gave me Neil. Thanks, I'll let you know how it turns out! Quote Link to comment Share on other sites More sharing options...
JonnoTheDev Posted August 13, 2009 Share Posted August 13, 2009 neil.johnson I think he's past that part and has stored the values in the database already. Nothing mentioned about storing post data but data correspondence I have a page that has five form options which correspond to a value in a databas You mentioned exploding a string to an array by using a separator. This requires modification to the form to include the separator as the value. How can I split the string 123? does it consist of three digits 1,2,3 or two 12,3 ? By using the correct data type in the first place will eliminate the need to split anything. Quote Link to comment Share on other sites More sharing options...
Jessica Posted August 13, 2009 Share Posted August 13, 2009 How do you store an array in a database? He's said database like 5 times, so yes, storing data was mentioned... Quote Link to comment Share on other sites More sharing options...
JonnoTheDev Posted August 13, 2009 Share Posted August 13, 2009 How do you store an array in a database? He's said database like 5 times, so yes, storing data was mentioned Really? The word 'database' means storing data? LOL Quote Link to comment Share on other sites More sharing options...
Jessica Posted August 13, 2009 Share Posted August 13, 2009 Not sure where you're going with that, but alright. Good luck OP. Quote Link to comment Share on other sites More sharing options...
njdirt Posted August 13, 2009 Author Share Posted August 13, 2009 Thanks, I think I figured out a way to make it work for me. The stored value is a 5 digit number composed of zeros and ones, with each digit acting as an operator for a specific option. Then for each switch, I look to the first, second,...,fifth digit to see if it is true or false, like so: $ch_email = substr($preferred_contact,0,1); $ch_phone1 = substr($preferred_contact,1,1); $ch_text1 = substr($preferred_contact,2,1); $ch_phone2 = substr($preferred_contact,3,1); $ch_text2 = substr($preferred_contact,4,1); Thanks again, hope this helps somebody out. I'm trying to add more options to a db with thousands of entries already, so I can't add fields as I please... 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.