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); 2 2015
["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 =--
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Steven T. Hatton |
last post by:
I bought Josuttis's book on the repeated recommendations of people in this
newsgroup.
http://www.josuttis.com/libbook/
One of the first things I looked up was the std::valarray<>. And what I...
|
by: Sam |
last post by:
Hello all
I have a two dimensional array (the dimensions are not known) that
needs to be passed
to fortran from c++, allocate the dimensions of the array in fortran
code, do some filling up
of...
|
by: Petter Reinholdtsen |
last post by:
Is the code fragment 'char a = ("a");' valid ANSI C? The
problematic part is '("a")'. I am sure 'char a = "a";' is valid
ANSI C, but I am more unsure if it is allowed to place () around the...
|
by: Chris |
last post by:
I am clearly missing something simple here....
I have a function (fileTree) that recursively reads all the files in a
directory passed to it, and returns an array of the files, I have included...
|
by: TG |
last post by:
Hi there !
I'm just starting to use Numeric here, and I'm wondering : how can I
efficiently initialize every values of a N-dimensional array, given I
don't know the number of dimensions ?
I'm...
|
by: Adam Warner |
last post by:
Hi all,
One cannot return a pointer to an array type in C because C has no first
class array types. But one can return a pointer to a struct containing an
incomplete array via the illegal but...
|
by: Jess |
last post by:
Hi,
If I have an array of pointer like:
char* a = {"a","b","c"};
then it works fine. Since "a" is effectively "a" char**, I tried the
following, which doesn't work:
char** a =...
|
by: dmitrey |
last post by:
howto make Python list from numpy.array?
Thx, D.
|
by: Lighter |
last post by:
How to read "The lvalue-to-rvalue, array-to-pointer, and function-to-
pointer standard conversionsare not applied to the left expressions"?
In 5.18 Comma operator of the C++ standard, there is a...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
| |