Jump to content

Populate check boxes from a database.


sjslovechild

Recommended Posts

I have a set of 26 checkboxes that are stored in an array. The below code dumps the comma separated array values into a single record column.

 

 

// 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

 

 

 

This works great! But my needs go past this. The record can be edited. So I need to get the values back out of the database and populate the correct check boxes for editing.

 

The checkboxes are generated by this code.

 

 

<div class="text">Job Type (<a class="editlist" href="javascript:editlist('listedit.php?edit=typelist',700,400);">edit list</a>):</div>
<div class="field">

	<?PHP

		$connection=mysql_connect ("localhost", "name", "pass") 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>';

	?>

</div>

 

 

 

I am having a problem getting these checkboxes repopulated. I think that a way to do this is to:

 

Get the values out of the database in a array

Compare the values the list of all possible values.

Check the boxes of the values confirmed to be there.

 

I didn't build the application so I need help to work through this problem.

 

Link to comment
https://forums.phpfreaks.com/topic/217130-populate-check-boxes-from-a-database/
Share on other sites

// 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 = implode(', ', $type);

//does the same as the code you posted

See implode

 

<div class="text">Job Type (<a class="editlist" href="javascript:editlist('listedit.php?edit=typelist',700,400);">edit list</a>):</div>
<div class="field">

      <?PHP

         $connection=mysql_connect ("localhost", "name", "pass") 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>';
      
      ?>

</div>

Get the values out of the database in a arrayCompare the values the list of all possible values.Check the boxes of the values confirmed to be there.

 

Need more info to help with this, for example, a list of possible values.

 

Do you wish for the checkboxes that conform to the valid values to be checked on page load?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.