Enjoy an ad free experience by logging in. Not a member yet?
Register .
01-12-2010, 04:25 PM
PM User |
#1
Regular Coder
Join Date: Sep 2007
Posts: 238
Thanks: 9
Thanked 0 Times in 0 Posts
Help with conditional array
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):
PHP Code:
$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:
Code:
<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
Last edited by htcilt; 01-12-2010 at 04:33 PM ..
01-12-2010, 04:36 PM
PM User |
#2
Regular Coder
Join Date: Dec 2009
Location: UK
Posts: 495
Thanks: 0
Thanked 58 Times in 58 Posts
Could you show an example of the output you want?
you could use something like the following format
PHP Code:
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; } }
01-12-2010, 04:38 PM
PM User |
#3
Senior Coder
Join Date: Apr 2004
Location: Toronto, Ontario
Posts: 2,112
Thanks: 15
Thanked 122 Times in 122 Posts
try this;
PHP Code:
$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" ; } }
01-12-2010, 04:39 PM
PM User |
#4
Senior Coder
Join Date: Nov 2007
Location: Arkansas
Posts: 2,644
Thanks: 29
Thanked 330 Times in 326 Posts
Wouldn't a foreach work?
PHP Code:
$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!
01-12-2010, 04:40 PM
PM User |
#5
Senior Coder
Join Date: Apr 2004
Location: Toronto, Ontario
Posts: 2,112
Thanks: 15
Thanked 122 Times in 122 Posts
Quote:
Originally Posted by
tomws
Wouldn't a foreach work?
PHP Code:
$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
01-12-2010, 04:43 PM
PM User |
#6
Regular Coder
Join Date: Dec 2009
Location: UK
Posts: 495
Thanks: 0
Thanked 58 Times in 58 Posts
Indeed it is
I would recommend restructuring the array though tbh
01-12-2010, 04:52 PM
PM User |
#7
Regular Coder
Join Date: Sep 2007
Posts: 238
Thanks: 9
Thanked 0 Times in 0 Posts
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
01-12-2010, 04:57 PM
PM User |
#8
Senior Coder
Join Date: Apr 2004
Location: Toronto, Ontario
Posts: 2,112
Thanks: 15
Thanked 122 Times in 122 Posts
I don't really see any issues with the array, though you don't really need to number them, php does that for you.
01-12-2010, 05:21 PM
PM User |
#9
Regular Coder
Join Date: Dec 2009
Location: UK
Posts: 495
Thanks: 0
Thanked 58 Times in 58 Posts
I like it when checkboxes are more ordered and have useful keys
PHP Code:
$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
Code:
<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>
01-12-2010, 05:24 PM
PM User |
#10
Regular Coder
Join Date: Dec 2009
Location: UK
Posts: 495
Thanks: 0
Thanked 58 Times in 58 Posts
Oops, a couple of things. Change
PHP Code:
'tags' => "onClick='checkUncheckAll(this);'" // to 'tags' => 'onClick="checkUncheckAll(this);"'
and also add
<br /> to the end of the echo statement after
</label>
Users who have thanked JAY6390 for this post:
01-14-2010, 02:50 PM
PM User |
#11
Regular Coder
Join Date: Sep 2007
Posts: 238
Thanks: 9
Thanked 0 Times in 0 Posts
Thanks everyone for your posts. Its been a huge help
01-15-2010, 09:20 AM
PM User |
#12
Regular Coder
Join Date: Sep 2007
Posts: 238
Thanks: 9
Thanked 0 Times in 0 Posts
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?
01-15-2010, 01:00 PM
PM User |
#13
Regular Coder
Join Date: Dec 2009
Location: UK
Posts: 495
Thanks: 0
Thanked 58 Times in 58 Posts
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
PHP Code:
if(isset( $_POST [ 'checkALL' ])) { // code for all checked here }else{ // code for individual checkboxes here }
01-17-2010, 12:42 AM
PM User |
#14
Regular Coder
Join Date: Sep 2007
Posts: 238
Thanks: 9
Thanked 0 Times in 0 Posts
Good idea JAY6390. Never thought of doing it that way
Jump To Top of Thread
Thread Tools
Rate This Thread
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
All times are GMT +1. The time now is 09:43 AM .
Advertisement
Log in to turn off these ads.