Put values in a table from checkbox

66 Views Asked by At

I have a problem. I want to put dates into an array but when I make print_r() I get only the last value from checkbox. My code is:

$id = Input::get('id');
    $aObjects = Input::get('aObjects');
    $iCount = count($aObjects);
    for($i=0; $i < $iCount; $i++)
    {
        $test = array ($aGoupes = array(
                            'idGroupe' => $id,
                            'idObject' => $aObjects[$i]
                      ));
    }
    echo '<pre>';
        print_r($test);
    echo '</pre>';

The output is:

Array
(
 [0] => Array
    (
        [idGroupe] => 6
        [idObject] => 8
    )

)

So the problem is that only the last value checked from checkbox is put in this table. Please help me!! Thnx

1

There are 1 best solutions below

0
rjdown On BEST ANSWER

Your problem is that you're resetting $test each time.

Try this:

$id = Input::get('id');
$aObjects = Input::get('aObjects');
$iCount = count($aObjects);
$test = array();
for ($i = 0; $i < $iCount; $i++) {
    $test[] = array (
        'idGroupe' => $id,
        'idObject' => $aObjects[$i]
    );
}
echo '<pre>';
print_r($test);
echo '</pre>';

I'm not too sure what your code is supposed to do, but the idGroupe will always be the same in each array, as you're setting it to $id which is never changed. That may well be correct, though.