Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 14 of 14
  1. #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.

  • #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;
        }

    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 :)

  • #3
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    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";
        }


  • #4
    Senior Coder tomws's Avatar
    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] == $js '').'/>['.$checkbox[0].']'.$checkbox[2].'</label><br />';

    Something like that, anyway.

    Ha! 3 answers at once. Take your pick!
    Are you a Help Vampire?

  • #5
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    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

  • #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
    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 :)

  • #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

  • #8
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    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.

  • #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(
        
    => 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 :)

  • #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>
    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 :)

  • Users who have thanked JAY6390 for this post:

    htcilt (01-14-2010)

  • #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

  • #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?

  • #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

    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 :)

  • #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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •