Jump to content

[SOLVED] Code to recognize digits within a number string


Recommended Posts

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!

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']);
?>

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!

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.

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

 

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.