Category checkboxes problem

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

    Category checkboxes problem

    Newbie here. I have a page that uses a <select> list to assign a
    category to an itme, however it is possible for an item to have more
    than one category assigned. Right now I have to enter the item and one
    of the categories then go back to the edit item page to add a second or
    third category.

    What I'd like to do is to list the categories ias checkbox items
    instead of in a <select> list. The problem is the category keys and
    descriptions come from a MySQL atbel, and can be changed. So how do
    associate a name and value with each checkbox when I don't know ahead
    of time how many checkboxes there will be or what their category keys
    will be?

    I can do

    <INPUT TYPE="checkbox" NAME="cb123" VALUE="on">Cate gory Description
    <INPUT TYPE="checkbox" NAME="cb137" VALUE="on">Cate gory Description

    Where "123", and "137" in the example are the category keys from the
    SQL table, but then how would the page that recieves those values know
    which possible variable names to look for?

    Any ideas would be appreciated.

    --gary

  • Andy Jeffries

    #2
    Re: Category checkboxes problem

    On Fri, 24 Mar 2006 08:34:51 -0800, fiziwig wrote:[color=blue]
    > I can do
    >
    > <INPUT TYPE="checkbox" NAME="cb123" VALUE="on">Cate gory Description
    > <INPUT TYPE="checkbox" NAME="cb137" VALUE="on">Cate gory Description
    >
    > Where "123", and "137" in the example are the category keys from the SQL
    > table, but then how would the page that recieves those values know which
    > possible variable names to look for?[/color]

    foreach ($_POST as $key=>$value) {
    if (ereg("cb([0-9]+)", $key, $matches)) {
    print "You checked ID $matches[1]";
    }
    }

    Cheers,


    Andy


    --
    Andy Jeffries MBCS CITP ZCE | gPHPEdit Lead Developer
    http://www.gphpedit.org | PHP editor for Gnome 2
    http://www.andyjeffries.co.uk | Personal site and photos

    Comment

    • fiziwig

      #3
      Re: Category checkboxes problem

      Perfect! Thank you.

      As a newbie so far I've only used regexps to validate an email address.
      I hadn't really explored their full potential yet.

      --gary

      Comment

      • Ken Robinson

        #4
        Re: Category checkboxes problem

        > On Fri, 24 Mar 2006 08:34:51 -0800, fiziwig wrote:[color=blue][color=green]
        > > I can do
        > >
        > > <INPUT TYPE="checkbox" NAME="cb123" VALUE="on">Cate gory Description
        > > <INPUT TYPE="checkbox" NAME="cb137" VALUE="on">Cate gory Description[/color][/color]

        A different way to do this is

        <INPUT TYPE="checkbox" NAME="cb[123]" VALUE="on">Cate gory Description
        <INPUT TYPE="checkbox" NAME="cb[137]" VALUE="on">Cate gory Description

        Then in you PHP, you can do
        <?php
        if (isset($_POST['cb']))
        foreach $_POST['cb'] as $key=>$dmy)
        echo 'You chose catagory ' . $key . '<br>';
        ?>

        This works because only those checkboxes that are actually checked are
        sent to your script.

        Ken

        Comment

        • Geoff Berrow

          #5
          Re: Category checkboxes problem

          Message-ID: <1143225322.387 767.284600@u72g 2000cwu.googleg roups.com> from
          Ken Robinson contained the following:
          [color=blue][color=green][color=darkred]
          >> > <INPUT TYPE="checkbox" NAME="cb123" VALUE="on">Cate gory Description
          >> > <INPUT TYPE="checkbox" NAME="cb137" VALUE="on">Cate gory Description[/color][/color]
          >
          >A different way to do this is
          >
          ><INPUT TYPE="checkbox" NAME="cb[123]" VALUE="on">Cate gory Description
          ><INPUT TYPE="checkbox" NAME="cb[137]" VALUE="on">Cate gory Description
          >
          >Then in you PHP, you can do
          ><?php
          >if (isset($_POST['cb']))
          > foreach $_POST['cb'] as $key=>$dmy)
          > echo 'You chose catagory ' . $key . '<br>';
          >?>[/color]

          If he's getting the values from a db it may be slightly easier to do
          this:
          <INPUT TYPE="checkbox" NAME="cat[]" VALUE="cb123">C ategory Description
          <INPUT TYPE="checkbox" NAME="cat[]" VALUE="cb137">C ategory Description

          <?php
          if (isset($_POST['cat'])){
          foreach $_POST['cat'] as $value){
          echo 'You chose category ' . $value . '<br>';
          }
          }

          Values will only exist if checkboxes are checked.[color=blue]
          >?>[/color]
          --
          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

          • noone

            #6
            Re: Category checkboxes problem

            Ken Robinson wrote:
            [color=blue][color=green]
            >> On Fri, 24 Mar 2006 08:34:51 -0800, fiziwig wrote:[color=darkred]
            >> > I can do
            >> >
            >> > <INPUT TYPE="checkbox" NAME="cb123" VALUE="on">Cate gory Description
            >> > <INPUT TYPE="checkbox" NAME="cb137" VALUE="on">Cate gory Description[/color][/color][/color]
            [color=blue]
            > A different way to do this is[/color]
            [color=blue]
            > <INPUT TYPE="checkbox" NAME="cb[123]" VALUE="on">Cate gory Description
            > <INPUT TYPE="checkbox" NAME="cb[137]" VALUE="on">Cate gory Description[/color]
            [color=blue]
            > Then in you PHP, you can do
            > <?php
            > if (isset($_POST['cb']))
            > foreach $_POST['cb'] as $key=>$dmy)
            > echo 'You chose catagory ' . $key . '<br>';
            > ?>[/color]
            [color=blue]
            > This works because only those checkboxes that are actually checked are
            > sent to your script.[/color]
            [color=blue]
            > Ken[/color]

            or another way is:

            -----------begin cut here------------------
            <?php
            if (isset($_POST['cb']))
            {
            $cb = implode(",",$_P OST['cb']);
            echo 'You chose catagory ' . $cb . '<br>';
            }
            else {
            ?>
            <html><head><ti tle>test </title></head><body>
            <h2><center>tes t </h2></center><p>
            <form method=post>
            <INPUT TYPE="checkbox" NAME="cb[]" VALUE="123">CB 123<br>
            <INPUT TYPE="checkbox" NAME="cb[]" VALUE="133">CB 133<br>
            <INPUT TYPE="checkbox" NAME="cb[]" VALUE="143">CB 143<br>
            <INPUT TYPE="checkbox" NAME="cb[]" VALUE="153">CB 153<br>
            <INPUT TYPE="checkbox" NAME="cb[]" VALUE="167">CB 167<br>
            <input type=hidden name=flag value=1><input TYPE=submit value=Submit>
            <input TYPE=reset value=Reset></form>
            </body></html>
            <?php } ?>

            -----------end cut here------------------
            If you checked the appropriate boxes you get:

            You chose catagory 123,133,153


            Comment

            • fiziwig

              #7
              Re: Category checkboxes problem

              There's more than one way to skin a cat!

              These are all interesting, and food for future thought. To tell the
              truth I wasn't aware that you could send an arrray as a POST variable.

              I tried Andy's solution first, and it works fine. The number of
              categories is small, and will probably never grow to more than 15 or
              20.

              Thanks for all the suggestions.

              --gary

              Comment

              • Andy Jeffries

                #8
                Re: Category checkboxes problem

                On Fri, 24 Mar 2006 09:19:13 -0800, fiziwig wrote:[color=blue]
                > Perfect! Thank you.
                >
                > As a newbie so far I've only used regexps to validate an email address. I
                > hadn't really explored their full potential yet.[/color]

                No problem mate, more than welcome.


                Andy


                --
                Andy Jeffries MBCS CITP ZCE | gPHPEdit Lead Developer
                http://www.gphpedit.org | PHP editor for Gnome 2
                http://www.andyjeffries.co.uk | Personal site and photos

                Comment

                Working...