By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,537 Members | 1,471 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,537 IT Pros & Developers. It's quick & easy.

Array into scalar

P: n/a
Hi,

I have an array derived from a set of radio buttons that I want to
break down into a single variable for use with a database. How can I
do this? $gender is the array, and I want $gender2 to be a scalar.
Unfortunately, $gender2 shows up as null in the database.

Mountain Man
// I've tried this:
if ($gender[0] != null) { $gender2 = $gender[0]; }
elseif ($gender[1] != null) { $gender2 = $gender[1]; }

// And this:
if (in_array('M', $gender)) { $gender2 = M; }
elseif (in_array('F', $gender)) { $gender2 = F; }
// Here's the function that requires the creation of the array.
// It provides sticky multivalued parameters.
function make_radio ($name, $query, $options) {
foreach ($options as $value => $label) {
printf('<label><input type="radio" name="%s[]" value="%s"',
$name, $value);
if (in_array($value, $query)) { echo "checked "; }
echo "/> $label</label><br />\n";
}
}

// Here's where the array is created.
$genderOptions = array(
'M' => 'Male',
'F' => 'Female',
);

make_radio (gender, $gender, $genderOptions);
Jul 17 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
["Followup-To:" header set to comp.lang.php.]
[Please do not use alt.* groups; I do not have access to php.general]
Mountain Man wrote (edited):
I have an array derived from a set of radio buttons that I want to
break down into a single variable for use with a database. How can I
do this? $gender is the array, and I want $gender2 to be a scalar.
Unfortunately, $gender2 shows up as null in the database.

// Here's where the array is created.
$genderOptions = array(
'M' => 'Male',
'F' => 'Female',
);
the array $genderOptions has two elements; their indexes are 'M' and 'F'
and their values are, respectively, 'Male' and 'Female'.
// I've tried this:
if ($gender[0] != null) { $gender2 = $gender[0]; }
elseif ($gender[1] != null) { $gender2 = $gender[1]; }
if $gender is the same arrays as $genderOptions

both $gender[0] and $gender[1] *are* null; you have $gender['M'] and
$gender['F']
// And this:
if (in_array('M', $gender)) { $gender2 = M; }
elseif (in_array('F', $gender)) { $gender2 = F; }


'M' and 'F' are *not* any of the values in $gender; its values are
'Male' and 'Female'
So you never assign anything to $gender2.

Review the way you are doing the checks, remember that arrays have
indexes (which in your case are 'M' and 'F') and values ('Male' and
'Female') and they're two *very* different things.
--
--= my mail box only accepts =--
--= Content-Type: text/plain =--
--= Size below 10001 bytes =--
Jul 17 '05 #2

P: n/a
On 3/6/04 2:09 AM, in article
78**************************@posting.google.com, "Mountain Man"
<ei*************@yahoo.com> wrote:
// Here's the function that requires the creation of the array.
// It provides sticky multivalued parameters.
function make_radio ($name, $query, $options) {
foreach ($options as $value => $label) {
printf('<label><input type="radio" name="%s[]" value="%s"',
$name, $value);
if (in_array($value, $query)) { echo "checked "; }
echo "/> $label</label><br />\n";
}
}

The other option is to make the form return a scalar, not an array. Since
this is a radio button (not a checkbox) an array doesn't provide you any
advantage over a scalar. Swap in this line:

printf('<label><input type="radio" name="%s" value="%s"',

To create a form that returns a scalar value instead of an array.

Cheers,
Robert

--
Robert Peake

Peake Professional Consulting
Web Hosting, Design, Marketing, and More

Ro****@PeakePro.com
http://www.peakepro.com

Jul 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.