Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-12-2010, 04:25 PM   PM User | #1
htcilt
Regular Coder

 
Join Date: Sep 2007
Posts: 238
Thanks: 9
Thanked 0 Times in 0 Posts
htcilt is an unknown quantity at this point
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..
htcilt is offline   Reply With Quote
Old 01-12-2010, 04:36 PM   PM User | #2
JAY6390
Regular Coder

 
Join Date: Dec 2009
Location: UK
Posts: 495
Thanks: 0
Thanked 58 Times in 58 Posts
JAY6390 is on a distinguished road
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;
    }

__________________
My site: JayGilford.com
Resources:
PHP Pagination Class | Getting all page links | Handling PHP Errors properly
If you like a users help, show your appreciation with the rep and thanks buttons :)
JAY6390 is offline   Reply With Quote
Old 01-12-2010, 04:38 PM   PM User | #3
angst
Senior Coder

 
angst's Avatar
 
Join Date: Apr 2004
Location: Toronto, Ontario
Posts: 2,112
Thanks: 15
Thanked 122 Times in 122 Posts
angst is on a distinguished road
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";
    }

angst is offline   Reply With Quote
Old 01-12-2010, 04:39 PM   PM User | #4
tomws
Senior Coder

 
tomws's Avatar
 
Join Date: Nov 2007
Location: Arkansas
Posts: 2,644
Thanks: 29
Thanked 330 Times in 326 Posts
tomws will become famous soon enoughtomws will become famous soon enough
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] == $js '').'/>['.$checkbox[0].']'.$checkbox[2].'</label><br />';

Something like that, anyway.

Ha! 3 answers at once. Take your pick!
__________________
Are you a Help Vampire?
tomws is offline   Reply With Quote
Old 01-12-2010, 04:40 PM   PM User | #5
angst
Senior Coder

 
angst's Avatar
 
Join Date: Apr 2004
Location: Toronto, Ontario
Posts: 2,112
Thanks: 15
Thanked 122 Times in 122 Posts
angst is on a distinguished road
Quote:
Originally Posted by tomws View Post
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] == $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
angst is offline   Reply With Quote
Old 01-12-2010, 04:43 PM   PM User | #6
JAY6390
Regular Coder

 
Join Date: Dec 2009
Location: UK
Posts: 495
Thanks: 0
Thanked 58 Times in 58 Posts
JAY6390 is on a distinguished road
Indeed it is
I would recommend restructuring the array though tbh
__________________
My site: JayGilford.com
Resources:
PHP Pagination Class | Getting all page links | Handling PHP Errors properly
If you like a users help, show your appreciation with the rep and thanks buttons :)
JAY6390 is offline   Reply With Quote
Old 01-12-2010, 04:52 PM   PM User | #7
htcilt
Regular Coder

 
Join Date: Sep 2007
Posts: 238
Thanks: 9
Thanked 0 Times in 0 Posts
htcilt is an unknown quantity at this point
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
htcilt is offline   Reply With Quote
Old 01-12-2010, 04:57 PM   PM User | #8
angst
Senior Coder

 
angst's Avatar
 
Join Date: Apr 2004
Location: Toronto, Ontario
Posts: 2,112
Thanks: 15
Thanked 122 Times in 122 Posts
angst is on a distinguished road
I don't really see any issues with the array, though you don't really need to number them, php does that for you.
angst is offline   Reply With Quote
Old 01-12-2010, 05:21 PM   PM User | #9
JAY6390
Regular Coder

 
Join Date: Dec 2009
Location: UK
Posts: 495
Thanks: 0
Thanked 58 Times in 58 Posts
JAY6390 is on a distinguished road
I like it when checkboxes are more ordered and have useful keys
PHP Code:
$checkboxes = array(
    
=> array(
        
'name' => 'box1',
        
'text' => 'Select All',
        
'tags' => "onClick='checkUncheckAll(this);'"
    
),
    
=> array(
        
'name' => 'box2',
        
'text' => 'Monday',
    ),
    
=> array(
        
'name' => 'box3',
        
'text' => 'Tuesday',
    ),
    
=> array(
        
'name' => 'box4',
        
'text' => 'Wednesday',
    ),
    
=> 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>
__________________
My site: JayGilford.com
Resources:
PHP Pagination Class | Getting all page links | Handling PHP Errors properly
If you like a users help, show your appreciation with the rep and thanks buttons :)
JAY6390 is offline   Reply With Quote
Old 01-12-2010, 05:24 PM   PM User | #10
JAY6390
Regular Coder

 
Join Date: Dec 2009
Location: UK
Posts: 495
Thanks: 0
Thanked 58 Times in 58 Posts
JAY6390 is on a distinguished road
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>
__________________
My site: JayGilford.com
Resources:
PHP Pagination Class | Getting all page links | Handling PHP Errors properly
If you like a users help, show your appreciation with the rep and thanks buttons :)
JAY6390 is offline   Reply With Quote
Users who have thanked JAY6390 for this post:
htcilt (01-14-2010)
Old 01-14-2010, 02:50 PM   PM User | #11
htcilt
Regular Coder

 
Join Date: Sep 2007
Posts: 238
Thanks: 9
Thanked 0 Times in 0 Posts
htcilt is an unknown quantity at this point
Thanks everyone for your posts. Its been a huge help
htcilt is offline   Reply With Quote
Old 01-15-2010, 09:20 AM   PM User | #12
htcilt
Regular Coder

 
Join Date: Sep 2007
Posts: 238
Thanks: 9
Thanked 0 Times in 0 Posts
htcilt is an unknown quantity at this point
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?
htcilt is offline   Reply With Quote
Old 01-15-2010, 01:00 PM   PM User | #13
JAY6390
Regular Coder

 
Join Date: Dec 2009
Location: UK
Posts: 495
Thanks: 0
Thanked 58 Times in 58 Posts
JAY6390 is on a distinguished road
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

__________________
My site: JayGilford.com
Resources:
PHP Pagination Class | Getting all page links | Handling PHP Errors properly
If you like a users help, show your appreciation with the rep and thanks buttons :)
JAY6390 is offline   Reply With Quote
Old 01-17-2010, 12:42 AM   PM User | #14
htcilt
Regular Coder

 
Join Date: Sep 2007
Posts: 238
Thanks: 9
Thanked 0 Times in 0 Posts
htcilt is an unknown quantity at this point
Good idea JAY6390. Never thought of doing it that way
htcilt is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:43 AM.


Advertisement
Log in to turn off these ads.