...

View Full Version : Help with conditional array



htcilt
01-12-2010, 05:25 PM
Hi all,

I'm new to PHP and a bit stuck with a multidimensional array.
I have an array as follows (made up data just to get my problem across):


$checkboxes = array(
array(0,"box1","Select All"),
array(1,"box2","Monday"),
array(2,"box3","Tuesday"),
array(3,"box4","Wednesday"),
array(4,"box5","Thursday")
);

I need to echo the following and in this order:

1) Checkbox1 with added Javascript function
2) Checkbox 2
3) Checkbox 3 with opening DIV
4) The remaining x number of boxes.

So in HTML looks something like this:


<label><input type='checkbox' name='days[]' value='box1' onClick='checkUncheckAll(this);' />[0]Select All Days</label><br />
<label><input type='checkbox' name='days[]' value='box2' />[1]Monday</label><br />
<label><input type='checkbox' name='days[]' value='box3' />[2]Tuesday</label><br /><div id='mydiv'>"
<label><input type='checkbox' name='days[]' value='box4'' />[3]Wednesday</label><br />"
<label><input type='checkbox' name='days[]' value='box5'' />[4]Thursday</label><br />"

The box names may change so I need to try and use keys where possible.

Is anyone able to help?
Thanks in advance :)

JAY6390
01-12-2010, 05:36 PM
Could you show an example of the output you want?
you could use something like the following format

foreach($checkboxes as $val) {
switch($val[0]) {
case 0:
echo 'checkbox 0 code here<br />';
break;
case 1:
echo '<strong>checkbox 1 code here</strong><br />';
break;
case 2:
echo '<div>checkbox 2 code here</div><br />';
break;
}
}

angst
01-12-2010, 05:38 PM
try this;



$checkboxes = array(
array(0,"box1","Select All"),
array(1,"box2","Monday"),
array(2,"box3","Tuesday"),
array(3,"box4","Wednesday"),
array(4,"box5","Thursday")
);

foreach($checkboxes As $Array){
if($Array[0] == 0){
echo "<input type='checkbox' name='days[]' value='" . $Array[1] . "' onClick='checkUncheckAll(this);' />" . $Array[2] . "<br />\n";
} elseif($Array[0] == 2){
echo "<input type='checkbox' name='days[]' value='" . $Array[1] . "' " . $Extra . " />" . $Array[2] . "<br /><div id='mydiv'></div>\n";
} else {
echo "<input type='checkbox' name='days[]' value='" . $Array[1] . "' />" . $Array[2] . "<br />\n";
}
}

tomws
01-12-2010, 05:39 PM
Wouldn't a foreach work?


$js = ' onClick="blah();"';
$output = '';
foreach ($checkboxes as $checkbox)
{
$output .= '<label><input type="checkbox" name="days[]" value="'.$checkbox[1].'"'.($checkbox[0] == 0 ? $js : '').'/>['.$checkbox[0].']'.$checkbox[2].'</label><br />';
}
Something like that, anyway.

Ha! 3 answers at once. Take your pick!

angst
01-12-2010, 05:40 PM
Wouldn't a foreach work?


$js = ' onClick="blah();"';
$output = '';
foreach ($checkboxes as $checkbox)
{
$output .= '<label><input type="checkbox" name="days[]" value="'.$checkbox[1].'"'.($checkbox[0] == 0 ? $js : '').'/>['.$checkbox[0].']'.$checkbox[2].'</label><br />';
}
Something like that, anyway.

Ha! 3 answers at once. Take your pick!

I'd say that foreach is the one thing we could all agree on :thumbsup:

JAY6390
01-12-2010, 05:43 PM
Indeed it is :)
I would recommend restructuring the array though tbh

htcilt
01-12-2010, 05:52 PM
all give foreach a go and get back to you.
Thanks everyone with your suggestions so far.

JAY6390, I'm all ears if you have a better structure :)

angst
01-12-2010, 05:57 PM
I don't really see any issues with the array, though you don't really need to number them, php does that for you.

JAY6390
01-12-2010, 06:21 PM
I like it when checkboxes are more ordered and have useful keys


$checkboxes = array(
0 => array(
'name' => 'box1',
'text' => 'Select All',
'tags' => "onClick='checkUncheckAll(this);'"
),
1 => array(
'name' => 'box2',
'text' => 'Monday',
),
2 => array(
'name' => 'box3',
'text' => 'Tuesday',
),
3 => array(
'name' => 'box4',
'text' => 'Wednesday',
),
4 => array(
'name' => 'box5',
'text' => 'Thursday',
)
);

foreach($checkboxes as $id => $data) {
echo '
<label><input type="checkbox" name="days[]" value="'.$data['name'].'"'.(isset($data['tags']) ? ' '.$data['tags'] : '').'/>['.$id.']'.$data['text'].'</label>';
if($id == 2)
echo '<div id="myDiv">';
}
This outputs



<label><input type="checkbox" name="days[]" value="box1" onClick='checkUncheckAll(this);'/>[0]Select All</label>
<label><input type="checkbox" name="days[]" value="box2"/>[1]Monday</label>
<label><input type="checkbox" name="days[]" value="box3"/>[2]Tuesday</label><div id="myDiv">
<label><input type="checkbox" name="days[]" value="box4"/>[3]Wednesday</label>
<label><input type="checkbox" name="days[]" value="box5"/>[4]Thursday</label>

JAY6390
01-12-2010, 06:24 PM
Oops, a couple of things. Change

'tags' => "onClick='checkUncheckAll(this);'"
// to
'tags' => 'onClick="checkUncheckAll(this);"'

and also add <br /> to the end of the echo statement after </label>

htcilt
01-14-2010, 03:50 PM
Thanks everyone for your posts. Its been a huge help :)

htcilt
01-15-2010, 10:20 AM
I've run into a slight problem. When the "Select All" option is selected, all checkboxes get selected using the checkUncheckAll function. The problem with this is the value of each checkbox is posted, whereas I just need the value "all" from the 1st checkbox.

A way around this would be to disable all other checkboxes on the 1st checkbox is ticked.

Is it possible to integrate this in the code we already have?

JAY6390
01-15-2010, 02:00 PM
You could just check that the first checkbox is set, or alternatively give that checkbox a new value such as
name="checkALL"
Then in your php check to see if that value is set before checking the other values

if(isset($_POST['checkALL'])) {
// code for all checked here
}else{
// code for individual checkboxes here
}

htcilt
01-17-2010, 01:42 AM
Good idea JAY6390. Never thought of doing it that way :thumbsup:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum