make array out of checkboxes?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • LRW

    make array out of checkboxes?

    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_arr ay($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*$taxm ult),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

    #2
    Re: make array out of checkboxes?

    I noticed that Message-ID:
    <3a1d1813.04031 81009.3d7bb1b9@ posting.google. com> from LRW contained the
    following:
    [color=blue]
    >while ($row = mysql_fetch_arr ay($result)) {
    > echo "<tr ".$bgcolour.">< td><input name=\"del\"
    >type=\"checkbo x\" value=\"$iid\"> </td><td>" . $quantity . "</td><td>"
    >. $typename . "</td><td>" . $typesize . "</td><td>$" . $amount .
    >"</td><td></td><td></td><td>$".round (($amount*$taxm ult),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_arr ay($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*$taxm ult),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<$nu mber_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/

    Comment

    • Ian Taylor

      #3
      Re: make array out of checkboxes?


      "LRW" <deja@celticbea r.com> wrote in message
      news:3a1d1813.0 403181009.3d7bb 1b9@posting.goo gle.com...[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_arr ay($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*$taxm ult),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_arr ay($result)) {
      print "<tr><td><i nput type='checkbox' name='del[]' value='del'><in put
      type='hidden'
      name='values[]'value='$id'></td><td>$quantit y</td><td>$typenam e</td><td>$typ
      esize</td><td>$."$amou nt".</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.


      Comment

      • LRW

        #4
        Re: make array out of checkboxes?

        "Ian Taylor" <mx3@ocset.reve rse.previous.wo rd.net> wrote in message news:<405a072d$ 0$22984$cc9e4d1 f@news.dial.pip ex.com>...

        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($s qldel, $dbconn);
        }
        }

        Works great!
        Thanks for the tips to get me ther...
        Liam

        Comment

        • Geoff Berrow

          #5
          Re: make array out of checkboxes?

          I noticed that Message-ID:
          <3a1d1813.04031 91410.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($s qldel, $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/

          Comment

          • LRW

            #6
            Re: make array out of checkboxes?

            "Geoff Berrow" <blthecat@ckdog .co.uk> wrote in message
            news:kr0n505b7p st6o89ug4fat7am abbg2d2df@4ax.c om...[color=blue]
            > I noticed that Message-ID:
            > <3a1d1813.04031 91410.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($s qldel, $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


            Comment

            Working...