tags:

views:

184

answers:

3

My problem is a little bit complicate. (I use PHP)

I have two arrays, (simple arrays array[0] = string, array[1] = string...) OK, now I will display the content of two arrays in a webpage. The first array contain names and the second images URL.

Images and names are already displayed (my problem isn't here).

But now I want to do something else, add a check box near every image, those checkbox r active by default. Ok, now the user can uncheck some inbox;

The final aim, is to get a new array containing only the values of the names and images that had been checked.

I have thought of something simple, crawl the keys (number) of unchecked checkboxes and unset them from my array. But the problem that I didn't know how to deal with the check boxes

Any suggestions

+5  A: 

To receive inputs as arrays in PHP, you have to set their name using brackets in HTML:

<label><input type="checkbox" name="thename[]" value="" /> The text</label>

Then, when you access $_REQUEST['thename'] you'll get an array. Inspect it to see its format and play with it :)

Seb
Great! I don't know that I should check it out :D
Omar Abid
A: 
<INPUT type="checkbox" name="chkArr[]" value="$num" checked/>

After the form is submitted, you'll have array $_REQUEST['chkArr'], in which you'll have numbers of the checkbox that are still checked.

To see which have been unchecked use array_diff($listOfAllNums, $chkArr)

vartec
+2  A: 

first of all i recomend having just one array:

$array = array (0 => array('name' => '....', 'url' => '....'))

i think this will make your life much easier. Also in the HTML you could also send the array key

foreach ($yourArray as $key=>$value) {
    ...
    <INPUT type="checkbox" name="chkArr[<?php echo $key ?>]" value="1" checked/>

then in your form action you itarate the intial array and remove the unchecked ones.

foreach ($yourArray as $key=>$value) {   
    if (!isset($_POST['chkArr'][$key]) OR $_POST['chkArr'][$key]!='1') {
        unset($yourArray[$key]);  
    }
}
solomongaby
Aye, the key is important. If not you will only have the value to judge which boxes are checked.
OIS