0

In my MySQL database I have a table like this:

Column      Value
----------------------------------
Id          int pk autoinc
Name        varchar(50)
Choices     set('a', 'b', 'c', ...)
...         ...

When this data is being updated, I'm looking to have lots of checkboxes displayed with each checkbox representing a member of Choices. With each checkbox being created in PHP similar to this:

echo "<input type=Checkbox name=$choiceName[i] value=$choiceName[i]>"

My question is how do I create these checkboxes from and populate them (by being checked or not) for a particular Id in the table.

Graham Laming
  • 1,213
  • 3
  • 14
  • 20
  • 1
    [What have you tried?](http://www.whathaveyoutried.com/) See [ask advice](http://stackoverflow.com/questions/ask-advice), please. – John Conde Feb 22 '13 at 20:04
  • have you even done any research? SO is not a place for "give me"s. It's for help when errors occur. – UnholyRanger Feb 22 '13 at 20:07
  • what database is this? how do you render HTML form checkboxes? There's no way to help you without details... – ducin Feb 22 '13 at 20:07
  • I've been trying to use the SQL command DESCRIBE to get all the members of the set, but haven't been able to do so successfully (or found anything to help me) so far. – Graham Laming Feb 22 '13 at 20:08
  • 1
    You need explode a data on a Choice field from database. Check out maybe: http://stackoverflow.com/questions/2350052/how-can-i-get-enum-possible-values-in-a-mysql-database – Marin Sagovac Feb 22 '13 at 20:10
  • is the keyword you are looking for? – EGOrecords Feb 22 '13 at 21:01
  • No. Firstly I'm looking to get the all members of the Choice set so I can label all of the checkboxes. Secondly, use the `selected` keyword to select the checkboxes corresponding to the value of the Choice column for a row with a given Id. From a few links down @MarinSagovac's link, I can do the first stage by parsing the output of `SHOW COLUMNS FROM Table LIKE 'Choices'`. – Graham Laming Feb 22 '13 at 21:15

2 Answers2

4

I finally found a solution.

In order to get the allowed values in the column Choice which is of a set data type. You have to do something like the following:

  $result = mysql_query("SHOW COLUMNS FROM Table LIKE 'Choice'");
  $line = mysql_fetch_array($result);
  //Remove the unwanted characters from the Type
  $set = substr($line['Type'],5,strlen($line['Type'])-7);
  //An array containing all allowed members from the Choice set
  $choices = preg_split("/','/",$set);

  $result = mysql_query("SELECT * FROM Table WHERE Id=$someId");
  $row = mysql_fetch_row($result);
  $selected = explode(',', $row[$indexOfChoicesColumn]);
  $j=0;
  for($i=0; $i<count($days); $i++){
     if($j<count($selected) && $selected[$j] == $choices[$i]){
        echo $choices[$i]." <input type='checkbox' name='choices[]' value='". $choices[$i] ."' checked> ";
        $j++;
     }
     else echo $choices[$i]." <input type='checkbox' name='choices[]' value='". $choices[$i] ."'> ";
  }
Graham Laming
  • 1,213
  • 3
  • 14
  • 20
0

Example:

//or you can obtain $choices = $row['Choices']; from  your DB, its the same 

$choices = "Example1,Example4,Example10";
$arrChoices = explode(',', $choices);

foreach ($arrChoices as $key => $value) {
echo "<input type='checkbox' name='$value' value='$value'/>";
}

Saludos ;)

Hackerman
  • 12,139
  • 2
  • 34
  • 45
  • I need to get the Choices directly from the set and not hard-code them. – Graham Laming Feb 22 '13 at 20:33
  • Show me an example of Choices from your data table ;) – Hackerman Feb 22 '13 at 20:34
  • But put an example....like: Choices field in my database looks like a,b,c,d,e,f or looks like A,B,C,D or looks like 'a','b','c' ....justo asking, you run the code i put like an example?? – Hackerman Feb 22 '13 at 20:39
  • The Choices column is defined by: `set('Example1','Example2','Example3',...)` etc. A value from that column might be `'Example1,Example4,Example10'`. – Graham Laming Feb 22 '13 at 20:52
  • i just edited the answer....but may be your problem it's caused because you dont know how to retrieve that data from your database??....is that correct?? – Hackerman Feb 22 '13 at 20:57
  • That data isn't in my database, it's in my Table schema. My question was asking how to get that data out of the schema. – Graham Laming Feb 22 '13 at 20:59
  • In my MySQL database I have a table like this:....that is how you start your question....i think you should edit your answer for better understanding – Hackerman Feb 22 '13 at 21:01