Search Webmaster Help and Solution

Populating HTML checkboxes from a database

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


Code: <?PHP $connection=mysql_connect ("localhost", "adsites_seth", "callie") or die ("I cannot connect to the database."); $db=mysql_select_db ("adsites_cerebra", $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>; ?> Im parsing the array and dumping the values as comma separated text into a database with this block of code.

Code: // dumping the type of job checkboxes $type = $_POST[type]; var_dump($type); // Setting up a blank variable to be used in the coming loop. $allStyles = ""; // For every checkbox value sent to the form. foreach ($type as $style) { // Append the string with the current array element, and then add a comma and a space at the end. $allTypes .= $style . ", "; } // Delete the last two characters from the string. $allTypes = substr($allTypes, 0, -2); // end dumping the type of job checkboxes
It works great. My problem is on the edit record page. Im generating the list as before, but this time I need to have the check boxes "checked" if that check box item had been entered into the database.

How do I alter the first block of code to do this? I am generating a group of check-boxes from a mySQL database with this code.


Code: <?PHP $connection=mysql_connect ("localhost", "adsites_seth", "callie") or die ("I cannot connect to the database."); $db=mysql_select_db ("adsites_cerebra", $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>; ?> Im parsing the array and dumping the values as comma separated text into a database with this block of code.

Code: // dumping the type of job checkboxes $type = $_POST[type]; var_dump($type); // Setting up a blank variable to be used in the coming loop. $allStyles = ""; // For every checkbox value sent to the form. foreach ($type as $style) { // Append the string with the current array element, and then add a comma and a space at the end. $allTypes .= $style . ", "; } // Delete the last two characters from the string. $allTypes = substr($allTypes, 0, -2); // end dumping the type of job checkboxes
It works great. My problem is on the edit record page. Im generating the list as before, but this time I need to have the check boxes "checked" if that check box item had been entered into the database.

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

View Complete Thread with Replies

Related Items

Query failed: connection to localhost:3354 failed (errno=111, msg=Connection refused).