RE: [phplib] tpl_form, Checkboxes and multiple option. From: Jeffrey A. Stuart (jstuart1 <email protected>)
Date: 12/08/00

Hi Joel, this is what I do. I have a check box of categories that the user
has to fill in... So.. on the form, I use this code:

      $this->form_data->add_element(array(
                                          "type" => "checkbox",
                                          "name" => "sub_cat",
                                          "multiple" => 1,
                                          "value" => $category,
                                          ));

Note... the key here is to set the value of the checkbox to the array
category. Now the values for this array come from a mysql enum field so...
here's how I set up the array:

  if ($table <> "NONE")
  {
     $query =<<<EOSQL
         select * from $table where company_id = $company_id
EOSQL;

     $sub_category->query($query);
     while ($sub_category->next_record())
     {
        if (!strcmp($table, "partnerships"))
        {
           $category = explode(",", $sub_category->f("sub_category"));
        }
        else
        {
           $category[] = $sub_category->f("sub_category"); // THIS is the key...
this is where it creates the array used above!
        }
     }
  }

Couple of things to note here... :) Since this form is used to pull data
from different tables decided at run time, that's why I use the $table in my
sql statement... Now the partnership table is unique in that it's category
field is a set rather than an enum... therefore, the value of it in the
database is "cat1, cat2, cat3". That's why I explode it... Otherwise, I
simply append to the category array.

And finally, here's the code that I use in the template itself:

    if ($table != 'partnerships')
    {
       mysql_connect();
       mysql_select_db("wod_resources");
       $query = <<<EOSQL
       show fields from $table like 'sub_category'
EOSQL;

       $result = mysql_query($query);

       while ($row = mysql_fetch_array($result))
       {
           if ($table == 'partnerships')
           {
              $sub_cat = str_replace("'", "", str_replace(")", "",
str_replace("set(","", $row["Type"])));
           }
           else
           {
              $sub_cat = str_replace("'", "", str_replace(")", "",
str_replace("enum(","", $row["Type"])));
           }
          $categories = explode(",", $sub_cat);
       }
       mysql_close();
       if (sizeof($categories) > 1)
       {
          while (list ($key, $value) = each ($categories))
          {
             $cat[$value] = "No";
          }

          $sub_cat = new DB_WOD;
          $company_id = $q->f("company_id");
          $sub_cat_query = <<<EOSQL
              select * from $table where company_id = $company_id
EOSQL;

          $sub_cat->query($sub_cat_query);
          while ($sub_cat->next_record())
          {
             $cat[$sub_cat->f("sub_category")] = "Yes";
          }

          while (list ($key, $value) = each ($cat))
          {
             $this->form_data->show_element("sub_cat", $key);
             print " $key&nbsp;&nbsp;&nbsp;";
          }
       }
       else
       {
          print "<input name=\"sub_cat\" type=\"hidden\"
value=\"$categories[0]\">";
       }
    }

Now why I did this... I don't exactly remember... LOL... what happens here
is that since the categories change per table, I dynamically determine what
the categories are that I'm gonna display. Now, I do NOT remember why I was
setting everything to no and then only setting the cats that actually exist
to yes. :)

Hope this helps.

--
Jeff (FurBall)
WebOverdrive Newbie Tech Board
http://www.topniche.com/tech/
furball <email protected>

-----Original Message----- From: Joel Kucera [mailto:joel <email protected>] Sent: Tuesday, December 05, 2000 2:46 PM To: phplib <email protected> Subject: [phplib] tpl_form, Checkboxes and multiple option.

I (like many people I guess..) am having problems with checkboxes in PHPlib. I have a form, which adds an entry into a database, I then want to allow the user to edit this entry later if the wish. The form has 10 checkboxes, I am creating them with the "multiple" option set. Then I implode the resulting array from the checkboxes and store it in a database.

Now, to edit the entry, I explode the entry from the database into an array and I recreate the same form, with the intention to show all of the user's previous choices as checked. However, I cannot find a way to set the checkboxes as checked or unchecked. I know _which_ ones should be checked, that's not a problem. But the "checked"=>"1" option does not work when "multiple" is set, and if I use "extrahtml"=>" checked" it sets _all_ of the checkboxes to checked.

Now, in the file of_checkbox.inc in PHPlib, there is this function...

function of_checkbox($a) { <snip>

if ($this->multiple) { $n = $this->name . "[]"; $str .= "<input type='checkbox' name='$n' value='$val'"; if (is_array($this->value)) { reset($this->value); while (list($k,$v) = each($this->value)) { if ($v==$val) { $str .= " checked"; break; <snip>

it seems to me (maybe I'm wrong) that there should be some way to control if a checkbox is checked, even when the multiple option is set, check out the following two lines from the function...

if ($v==$val) { $str .= " checked";

Here is some of my code in case you need to see what I'm trying to do...

// this is in the add.inc , it creates the checkboxes from an array // $this->access_array contains the info for all my checkboxes

for($x = 0; $x < sizeof($this->access_array); $x++) { $this->form_data->add_element(array("type"=>"checkbox", "name"=>"accessories", "value"=>$this->access_array[$x]["id"], "multiple"=>"1" )); }

// this is in the edit.inc, it creates the same checkboxes, but it makes // a distinction between which checkboxes should be checked, and which // ones shouldn't // $this->access_array is the same as the one above // $old_accessories is the array from the database containing the user's // previous choices...

for($x = 0; $x < sizeof($this->access_array); $x++) { if(in_array($old = $this->access_array[$x]["id"], $old_accessories)) { $this->form_data->add_element(array("type"=>"checkbox", "name"=>"accessories", "value"=>$this->access_array[$x]["id"], "multiple"=>"1" // this one should be checked )); } else { $this->form_data->add_element(array("type"=>"checkbox", "name"=>"accessories", "value"=>$this->access_array[$x]["id"], "multiple"=>"1" // this one is not checked )); } }

// this is how I display the checkboxes in both add.ihtml and edit.ihtml

for($x = 0; $x < sizeof($this->access_array); $x++) { $this->form_data->show_element("accessories", $this->access_array[$x]["id"]); echo $this->access_array[$x]["name"] . "<br>"; }

I don't think something like this would have been overlooked in PHPLib, so either I'm not doing something right, or I'm going about this in the wrong direction. If you need to see more code or anything I'm doing, just ask. I would really appreciate any help.

Thanks

Joel Kucera

--------------------------------------------------------------------- To unsubscribe, e-mail: phplib-unsubscribe <email protected> For additional commands, e-mail: phplib-help <email protected>

--------------------------------------------------------------------- To unsubscribe, e-mail: phplib-unsubscribe <email protected> For additional commands, e-mail: phplib-help <email protected>