Page 1 of 1

array checkbox?

Posted: Tue May 10, 2005 12:57 pm
by total
i have a table with 3 columns, where the number of rows is in a while
loop based on a SELECT query... so, depending on the result of the
query, you could have 1 row, or 3 rows, or 5 rows, or 100 rows. the
1st column is a checkbox, the 2nd and 3rd columns have a value from
the SELECT. now what i want the user to be able to do, is go down
through there and check whichever checkbox(s) they want. the thing
is, since this is done in a loop, the checkbox always has the same
name. when they submit, how do i determing which box(s) they checked
because right now, all i'm getting is the value of the 1st checkbox?

NOTE: there is a place where i do an echo $htmlmessage, as well as a submit button, i figure that's a given - but just wanted to make a note.

//sample code to create the table with checkbox

Code: Select all

<?php
$id = $_REQUEST['someID'].

$getval = OCIParse($Link, "SELECT COLUMN1, COLUMN2 FROM TABLE WHERE ID = '$id'");

OCIDefineByName($getval, "COLUMN1", $col1);
OCIDefineByName($getval, "COLUMN2", $col2);

$htmlmessage = "<table border='1'><tr><td>CHECKBOX</td><td>COLUMN1</td><td>COLUMN2</td></tr>";

    while (OCIFetch($getval)) {
      $htmlmessage .= "<tr><td align='center'><input type='checkbox' name='chkbox' value='1'></td><td>" . $col1 . "</td><td>" . $col2 . "</td></tr>";
    }

    $htmlmessage .= "</table>";
?>

Posted: Tue May 10, 2005 1:01 pm
by John Cartwright
To determine which row has been checked, you should have some sort of id that correlates it back to the row in your database -- usually done with auto incrementing an id. Then, when the time comes to do whatever with the checkbox, explode your checkbox names

Code: Select all

//checkbox names would be something along checkbox_uniqueid
$checkbox = 'checkbox_4'; 
$checkid  = explode('_',$checkbox);
now you can relate which row has been checked on the database

Sorry if I misunderstood.

Posted: Tue May 10, 2005 1:26 pm
by total
there is a keyid for each row from the query, i put it in the checkbox value... so, when it is checked, the value will be the unique identifier for that row. but it's still only getting the value for the last box. how do i get them all?

Posted: Tue May 10, 2005 2:04 pm
by total
here's what i've done

1) append [] onto the name of the checkbox (to make it an array)
2) make the checkbox value = keyid (from the SELECT)
3) $chkbox = implode($_REQUEST['chkbox']);
4) echo $chkbox gives me the keyid values for all the boxes that i
want... but the only thing is -- they are all run together :(

-- now, how do i insert a comma or space or something between each checkbox value to seperate the identifier???

thankx -- you put me going in the right direction :)

Posted: Tue May 10, 2005 2:05 pm
by John Cartwright
You could have the checkbox as an array

Code: Select all

echo "<input type='checkbox' name='chkbox[$unique_id]' value='1'>";
and then to see which have been checked

Code: Select all

if (!empty($_POST['chkbox']))
{
     foreach ($_POST['chkbox'] as $checkbox => $value)
     {
          echo 'name: ' $checkbox .'. Value: '. $value;
     }
}

Posted: Tue May 10, 2005 2:11 pm
by total
already did that... now there's another problem :?

Posted: Tue May 10, 2005 2:18 pm
by total
i figured it out... i can't believe how easy this was.

on number 2, where i made the checkbox value = keyid, i just did this:

<input type='checkbox' name='chkbox[]' value='" . $keyid . ", '>

now when i echo, there is a comma/space after each one. i know what to do from here.