Connecting Tech Pros Worldwide Forums | Help | Site Map

make array out of checkboxes?

LRW
Guest
 
Posts: n/a
#1: Jul 17 '05
I did a Web search, and a deja.com search on this...and I'm finding
how to make checkboxes act like radiobuttons, and other interesting
behaviors, but nothing that quite answers my question. If someone has
a link or thread they know about, even just that would be great!

What I have is a page that generates a list of items from a
database...a list of rows.
I'm having each row in the WHILE array create a checkbox, so that
someone can put checks in any row they want to delete on submitting
the form.

Now, how do I set up where it collects which checkboxes were selected?
I'm sure if I can get that far, I can figure out an EACH method of
then deleting each id from the table.

Here's what I'm doing so far with the checkboxes:
while ($row = mysql_fetch_array($result)) {
echo "<tr ".$bgcolour."><td><input name=\"del\"
type=\"checkbox\" value=\"$iid\"></td><td>" . $quantity . "</td><td>"
.. $typename . "</td><td>" . $typesize . "</td><td>$" . $amount .
"</td><td></td><td></td><td>$".round(($amount*$taxmult),2)."</td></tr>";

The $iid is the table.id mentioned in the SELECT statment before this.
So that each checkbox, when checked, will have a value of that row's
ID.

Any suggestions, even just a one-word name of the method I should look
into, would be much appreciated!

Thanks!
Liam

Geoff Berrow
Guest
 
Posts: n/a
#2: Jul 17 '05

re: make array out of checkboxes?


I noticed that Message-ID:
<3a1d1813.0403181009.3d7bb1b9@posting.google.com > from LRW contained the
following:
[color=blue]
>while ($row = mysql_fetch_array($result)) {
> echo "<tr ".$bgcolour."><td><input name=\"del\"
>type=\"checkbox\" value=\"$iid\"></td><td>" . $quantity . "</td><td>"
>. $typename . "</td><td>" . $typesize . "</td><td>$" . $amount .
>"</td><td></td><td></td><td>$".round(($amount*$taxmult),2)."</td></tr>";
>
>The $iid is the table.id mentioned in the SELECT statment before this.
>So that each checkbox, when checked, will have a value of that row's
>ID.[/color]

The way I did this as to sequentially name the check boxes e.g.

$i=0;
while ($row = mysql_fetch_array($result)) {
echo "<tr ".$bgcolour."><td><input name=\"del$i\"
type=\"checkbox\" value=\"$iid\"></td><td>" . $quantity . "</td><td>"
.. $typename . "</td><td>" . $typesize . "</td><td>$" . $amount .
"</td><td></td><td></td><td>$".round(($amount*$taxmult),2)."</td></tr>";
$i++;
}

You know how many check boxes you have printed. When the form is
returned, do a loop to see which boxes are ticked.

for($i=0;$i<$number_of_boxes; $i++){
if(isset($_POST["del$i"])){
//do delete stuff
}
}

--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Ian Taylor
Guest
 
Posts: n/a
#3: Jul 17 '05

re: make array out of checkboxes?



"LRW" <deja@celticbear.com> wrote in message
news:3a1d1813.0403181009.3d7bb1b9@posting.google.c om...[color=blue]
> I did a Web search, and a deja.com search on this...and I'm finding
> how to make checkboxes act like radiobuttons, and other interesting
> behaviors, but nothing that quite answers my question. If someone has
> a link or thread they know about, even just that would be great!
>
> What I have is a page that generates a list of items from a
> database...a list of rows.
> I'm having each row in the WHILE array create a checkbox, so that
> someone can put checks in any row they want to delete on submitting
> the form.
>
> Now, how do I set up where it collects which checkboxes were selected?
> I'm sure if I can get that far, I can figure out an EACH method of
> then deleting each id from the table.
>
> Here's what I'm doing so far with the checkboxes:
> while ($row = mysql_fetch_array($result)) {
> echo "<tr ".$bgcolour."><td><input name=\"del\"
> type=\"checkbox\" value=\"$iid\"></td><td>" . $quantity . "</td><td>"
> . $typename . "</td><td>" . $typesize . "</td><td>$" . $amount .
> "</td><td></td><td></td><td>$".round(($amount*$taxmult),2)."</td></tr>";
>
> The $iid is the table.id mentioned in the SELECT statment before this.
> So that each checkbox, when checked, will have a value of that row's
> ID.
>
> Any suggestions, even just a one-word name of the method I should look
> into, would be much appreciated!
>
> Thanks!
> Liam[/color]

I've always done it this way:

while ($row = mysql_fetch_array($result)) {
print "<tr><td><input type='checkbox' name='del[]' value='del'><input
type='hidden'
name='values[]'value='$id'></td><td>$quantity</td><td>$typename</td><td>$typ
esize</td><td>$."$amount".</td><td></td><td></td><td>$".round(($amount*$taxm
ult),2)."</td></tr>";
}


then when you're processing the data do something like....


for ($i = 0; $i < count($values); $i++) {
if($del[$i] = 'del') {
deleteRecord($i);
}
}

Regards,

Ian.


LRW
Guest
 
Posts: n/a
#4: Jul 17 '05

re: make array out of checkboxes?


"Ian Taylor" <mx3@ocset.reverse.previous.word.net> wrote in message news:<405a072d$0$22984$cc9e4d1f@news.dial.pipex.co m>...

Actually, what I ended up figuring out was a lot easier than those:

<input name="del[]" type="checkbox" value="$iid">

Then on the PHP:

if ($del) {
foreach ($del as $v) {
$sqldel = "delete from orderscart where id = '".$v."' and
ordernum = '".$ordernum."'";
$result = @mysql_query($sqldel, $dbconn);
}
}

Works great!
Thanks for the tips to get me ther...
Liam
Geoff Berrow
Guest
 
Posts: n/a
#5: Jul 17 '05

re: make array out of checkboxes?


I noticed that Message-ID:
<3a1d1813.0403191410.56927226@posting.google.com > from LRW contained the
following:
[color=blue]
>if ($del) {
> foreach ($del as $v) {
> $sqldel = "delete from orderscart where id = '".$v."' and
>ordernum = '".$ordernum."'";
> $result = @mysql_query($sqldel, $dbconn);
> }
>}
>
>Works great![/color]

Yeah, the only thing I don't like about that is that it works too well.
I like to have an intermediate screen asking me if I'm sure I want to
delete these records.

--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
LRW
Guest
 
Posts: n/a
#6: Jul 17 '05

re: make array out of checkboxes?


"Geoff Berrow" <blthecat@ckdog.co.uk> wrote in message
news:kr0n505b7pst6o89ug4fat7amabbg2d2df@4ax.com...[color=blue]
> I noticed that Message-ID:
> <3a1d1813.0403191410.56927226@posting.google.com > from LRW contained the
> following:
>[color=green]
> >if ($del) {
> > foreach ($del as $v) {
> > $sqldel = "delete from orderscart where id = '".$v."' and
> >ordernum = '".$ordernum."'";
> > $result = @mysql_query($sqldel, $dbconn);
> > }
> >}
> >
> >Works great![/color]
>
> Yeah, the only thing I don't like about that is that it works too well.
> I like to have an intermediate screen asking me if I'm sure I want to
> delete these records.[/color]

Oh, good point. I'll take a closer look at the other methods; see if I can
suss them out.
Thanks,
Liam


Closed Thread