Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Array issues

Status
Not open for further replies.

tyutghf

Technical User
Apr 12, 2008
258
GB
I have a dropdown and a checkbox within a form, the user needs to be able to select a value of the dropdown, and checkbox value(s) which are added to the database, but also show these values the next time the user visits the form.

Here's the code

Code:
<select name=‘whatlevels[]’>
<option value=''>No</option>
<option value='1' <?php if ($row['whatlevels'] == '1') { echo 'selected="selected"'; } ?>>1</option>
<option value='2' <?php if ($row['whatlevels'] == '2') { echo 'selected="selected"'; } ?>>2</option>
<option value='3' <?php if ($row['whatlevels'] == '3') { echo 'selected="selected"'; } ?>>3</option>
<option value='4' <?php if ($row['whatlevels'] == '4') { echo 'selected="selected"'; } ?>>4</option>
<option value='5' <?php if ($row['whatlevels'] == '5') { echo 'selected="selected"'; } ?>>5</option>
</select>
<span>
<label for='level_a'>a</label> <input type='checkbox' name='whatlevels[]' id='level_a' value='a' /> 
<label for='level_b'>b</label> <input type='checkbox' name='whatlevels[]' id='level_b' value='b' /> 
<label for='level_c'>c</label> <input type='checkbox' name='whatlevels[]' id='level_c' value='c' /> 
<label for='level_d'>d</label> <input type='checkbox' name='whatlevels[]' id='level_d' value='d' /> 
<label for='level_e'>e</label> <input type='checkbox' name='whatlevels[]' id='level_e' value='e' /> 
<label for='level_f'>f</label> <input type='checkbox' name='whatlevels[]' id='level_f' value='f' /> 
<label for='level_g'>g</label> <input type='checkbox' name='whatlevels[]' id='level_g' value='g' /> 
<label for='level_h'>h</label> <input type='checkbox' name='whatlevels[]' id='level_h' value='h' />
</span>

The response will be whether a level is selected (1-5), if not then leave blank. If it is, they can also select which values within that level are applicable. So a typical response will be

1, a, b, c

I need to enter this into the database column as 1a,1b,1c.

When coming back to the page, the form needs to see which number they previously selected and select it in the dropdown, and tick the correct a-h checkboxes.

Before I start to enter into the database, on submit I wanted to get the format of the response correct so tried

Code:
echo $_POST['whatlevels'];

But I just get back 'Array'.

I then tried to implode it

Code:
echo implode(',', $_POST['whatlevels']);

expecting something like 1,a,b,c (not quite what I want as I'd want 1a,1b,1c) but still just get 'Array'.

Can someone help please?

Thanks
 
The first thing is to see what exactly your POST variable looks like. Its definitely an Array since you are telling it to be an array by using the square brackets in its name.

To print out an array and any sub arrays it may have you can use print_r().

Code:
echo "<pre>" . print_r($_POST,1) . "</pre>";


Note however, that the quotes around the name of your select box are wrong. This is likely why you are getting strange array results.

Make sure they are actually single quotes, not as you have them there.

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
Thank you, now we get an array with print_r

[pre]
[whatlevels] => Array
(
[0] => 1
[1] => a
[2] => b
[3] => c
)
[/pre]

and the data went in to the DB as 1,a,b,c.

So now to have the dropdown and checkboxes show the responses the next time the user visits the form, would I use

Code:
<option value='1' <?php if (strpos($row['whatlevels'],'1' == 1)) { echo 'selected="selected"'; } ?>>1</option>
<option value='1' <?php if (strpos($row['whatlevels'],'2' == 2)) { echo 'selected="selected"'; } ?>>2</option>
<option value='1' <?php if (strpos($row['whatlevels'],'3' == 3)) { echo 'selected="selected"'; } ?>>3</option>
<option value='1' <?php if (strpos($row['whatlevels'],'4' == 4)) { echo 'selected="selected"'; } ?>>4</option>
<option value='1' <?php if (strpos($row['whatlevels'],'5' == 5)) { echo 'selected="selected"'; } ?>>5</option>
 
You could do that, or just explode the string back in to an array, and check it that way. Either way would work.

Code:
if($array[0] == 1)

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top