Jump to content

Multidimensional checkbox array help


dmacman

Recommended Posts

Hi everyone,

 

I opted to do this as checkboxes vs a select list because I could not come up with a 'working' option that did not fall apart at some point [hopefully, this avenue will not either] so please bear with me.

 

I have a series of checkboxes that are dynamically created from a previous form, and are named Class, and contain all the possible 'classes' (88 total subjects taught, and I need to let them choose multiple classes, IE, thats why I opted for checkboxes) that a teacher may have used for each of the video they chose on the previous form.

 

So on this form, I will give this example. (Here I will show 2 videos selected from the previous form, creating 2 sets of checkboxes for the 'Class' checkboxes, so for so good?

 

----------------------------------------------------

$zz=1;
$xx=1;
foreach($_SESSION['products'] as $key => $value)
    {
           // build the other elements of the form -- working fine
//---------- make a dummy array for the Class loop to use as Rating[x] names for checkbox array -----//
            $Rate = array();
            $Rate[$zz] = "Class[" . $key . "]"; //for the Class loop - check box array names
            $zz++;
//------------------------------------------------------------------------------------//
            foreach($Class_array as $key1=> $value)
                    {
                    echo '<label for=' . $xx . '_' . $key1 . ' style=\'padding-right:3px;display:block;\'><input name=\'' . $Rate[$xx] . '\' value=\'' . $key1 . '\' type=\'checkbox\' id=' . $xx . '_' . $key1 . ' onclick=\'highlight_div(this);\'>' . $value . '</label>';
            }
        $xx++;    
    } 

 

But this will not give me a multidimensional array.

 

And I believe I need to change this...

 

echo '<label for=' . $xx . '_' . $key1 . ' style=\'padding-right:3px;display:block;\'><input name=\'' . $Rate[$xx][$key] . '\' value=\'' . $key1 . '\' type=\'checkbox\' id=' . $xx . '_' . $key1 . ' onclick=\'highlight_div(this);\'>' . $value . '</label>'; 

 

...but that doesnt give me a nested array in the first array either.

 

I am not an array 'guru', I am missing that gene.

 

I would appreciate anyone with that gene's help.

 

Thanks,

 

Don

Link to comment
https://forums.phpfreaks.com/topic/101357-multidimensional-checkbox-array-help/
Share on other sites

I can't make sense of your code, you should have the same name for the checkbox so it will come in the multi-dimensional array... eg. "name='checkbox[]'" it will come as a multi-dimensional array in your post and the key name fill be "checkbox"

 

hopes its helpful!

 

 

 

I am sorry, but I am not making it clear. You are correct, that is what I want from the loops, but I need help nesting the array coding.

 

Let me try to simplify my code (I am not showing the entire page for readability)

 

I am building this page dynamically, from a previous page where the user picked a certain number of videos. That choice dictates how many sets of checkboxes will be displayed on this page.

 

So in this example, I am showing 2 sets of checkboxes, and I need them to be named: ( I am assuming this is the correct naming convention for a multidimensional checkbox array)

 

Set 1:

name='checkbox[0][0]'

name='checkbox[0][1]'

name='checkbox[0][2]'

 

 

Set 2

name='checkbox[1][0]'

name='checkbox[1][1]'

name='checkbox[1][2]'

 

So , I define the array name, and increment it like this, before the loop that displays the checkboxes.

 

$zz=1;
$xx=1;
foreach($_SESSION['products'] as $key => $value)
    {
           // build the other elements of the form -- working fine
//---------- make a dummy array for the Class loop to use as Rating[x] names for checkbox array -----//
            $Rate = array();
            $Rate[$zz] = "Class[" . $key . "]"; //for the Class loop - check box array names
            $zz++;
//------------------------------------------------------------------------------------//
            foreach($Class_array as $key1=> $value)
                    {
                    echo '<label for=' . $xx . '_' . $key1 . ' style=\'padding-right:3px;display:block;\'><input name=\'' . $Rate[$xx] . '\' value=\'' . $key1 . '\' type=\'checkbox\' id=' . $xx . '_' . $key1 . ' onclick=\'highlight_div(this);\'>' . $value . '</label>';
            }
        $xx++;    
    } 

 

Then I am trying( Here is my attempt..

$Rate[$xx][$key]

... to display the checkboxes and get the names to be like the example above (IE Set 1, Set 2)

 

in this code snippet...

 

echo '<label for=' . $xx . '_' . $key1 . ' style=\'padding-right:3px;display:block;\'><input name=\'' . $Rate[$xx][$key] . '\' value=\'' . $key1 . '\' type=\'checkbox\' id=' . $xx . '_' . $key1 . ' onclick=\'highlight_div(this);\'>' . $value . '</label>'; 

 

This is probably incorrect, but do you see what I am attempting to do?

 

I appreciate your patience and help,

 

Don

 

BINGO Craygo!

 

$Rate[$xx].'['.$key1.']' 

 

Now gives me...

 

<input name='Class[0][1]' value='1' type='checkbox' id=0_1 onclick='highlight_div(this);'>Accounting</label>

<input name='Class[0][2]' value='2' type='checkbox' id=0_2 onclick='highlight_div(this);'>Agriculture</label>

//etc

 

 

And I end up with my arrays the way I need them...

[Class] => Array

        (

            [ 0] => Array

                (

                    [1] => 1

                    [2] => 2

                )

 

            [1] => Array

                (

                    [3] => 3

                    [4] => 4

 

Sweet, thanks Craygo, perfect answer, owe ya one!

 

Don

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.