tags:

views:

38

answers:

2

I am generating a group of check-boxes from a MySQL database with this code.

$connection=mysql_connect ("localhost", "foo", "bar") or die ("I cannot connect to the database.");
$db=mysql_select_db ("database", $connection) or die (mysql_error());
$query = "SELECT type FROM typelist ORDER BY type ASC";
$sql_result = mysql_query($query, $connection) or die (mysql_error());
i=1;
echo "<table valign="top"><tr><td>";
while ($row = mysql_fetch_array($sql_result)) {
$type = $row["type"];
if ($i > 1 && $i % 26 == 0) 
echo '</td><td>'; 
else if ($i) 
echo ''; 
++$i; 
echo "<input style='font-size:10px;' name='type[]' type='checkbox' value='$type'><span style='color:#000;'>$type</span></input><br/>";}
echo '</td></tr></table>';

I'm parsing the array and dumping the values as comma separated text into a database with this block of code.

$allTypes = $_POST['allTypes'];
  var_dump($allTypes);


  $allStyles = "";


  foreach ($type as $style) {

    $allTypes .= $style . ", ";
  }


  $allTypes = substr($allTypes, 0, -2);

It works great. My problem is on the edit record page. I'm generating the list as before, but this time I need to have the check boxes "checked" if that checkbox item had been entered into the database.

How do I alter the first block of code to do this?

A: 

The code you say generates a list of checkboxes.....well, doesn't generate a list of checkboxes. No where in your codes do I see an input tag. My question to you is, how are you making these checkboxes?

Zane Edward Dockery
This is my first time posting to stackoverflow. The echoed HTML is not displaying. This is where the checkbox code is located. How do you display a block of code in markdown so that it displays as entered. I have used <pre><code></code></pre> it get the code to display at all.
KingMob
A: 

You can do it in two ways (or more, but theese are the easyest ones).

Javascript method:

If you already have assigned id's to the checkboxes, this is the easyest way.

    document.getElementById('idOfCheckbox').checked='checked';

PHP method:

You have to modify the string $row[type]. Insert checked="checked" just before the closingtag. This can be done in various ways, but play a little around with it (or just use the code below (it's untested though)).

    $row[type] = substr($row[type], 0, strlen($row[type]) - 2) . 'checked="checked" />';

But you haven't done it easy for youself but storing the checkboxes in a database... Why have you even done that, it makes no sense??

Mathias Bak
I'm storing the checkboxes in a database is because there are about 30 of them and they are editable through a admin page. Once the job is entered the values have to be stored in the database so that others can retrieve the data. Looking at the Javascript above how does that work if the data is getting pulled from a database? Thanks for responding
KingMob
Depends on how you store the checkbox in the database. If you store each checkbox as a string and just output it to a html page, then you need to assign unique id's to all of them. This is easily done with php, and you can use the code i provided above, if you modify it a bit. After that you have to create a javascript array using php to tell your javascript function which id's to check. Then you loop through the array and check the checkboxes. I would personally go for the PHP solution!! It's a whole lot easyer ;)
Mathias Bak