Jump to content

Recommended Posts

Hello freaks

 

I have dynamic checkboxes with values populated from a database table and want to set the boxes to selected based on values from another table.

Basically, I have a table of users, a table of categories (populates the checkboxes) and a table that is populated when multiple categories are selected per user (the table used to set the checkboxes to selected)..hope this is clear..Its for an 'Edit user' page, displaying the info for the user.

 

Is this possible?

 

This what I currently have:

<?php do { ?>

<input <?php if (!(strcmp($row_userCats['dlcatID'],$row_engineerCat['dlCatID'] ))) {echo "checked=\"checked\"";}  ?> type="checkbox" name="checkbox[]" value="<?php echo $row_engineerCat['dlCatID']; ?>" />

 

          <span class="formLBL"><?php echo $row_engineerCat['dlCat']; ?></span>

          </label>

          <br />

          <?php } while ($row_engineerCat = mysql_fetch_assoc($engineerCat)); ?>

 

(yes it is doctored dreamweaver code  :o )

 

But this only selects the first returned value from the userCats recordset.

 

Any help would be greatly appreciated.

 

Mitch

This should get you started.

 

Table structure for my example;

 

categories table:
cat_id
cat_name

users table:
user_id
user_name

user_categories table:
cat_id
user_id

 

This will display all the users and categories as checkboxes, if the user already has that category, it's already checked...

 

$query = "SELECT * FROM categories";

$result = mysql_query($query);

while ($row = mysql_fetch_assoc($result)) {
$cats[$row['cat_id']] = $row['cat_name'];
}

$query = "SELECT user_name, cat_name " .
	 "FROM user_categories uc " .
	 "  LEFT JOIN users u ON u.user_id = uc.user_id " .
	 "  LEFT JOIN categories c ON c.cat_id = uc.cat_id " .
	 "ORDER BY user_name, cat_name";

$result = mysql_query($query);

while ($row = mysql_fetch_assoc($result)) {
$user_data[$row['user_name']][] = $row['cat_name'];
}

echo '
<form method="post">
<table>';

foreach ($user_data as $user => $user_cats) {
echo '
	<tr>
		<th colspan="' . count($cats) . '">' . $user . '</th>
	</tr>
	<tr>';

foreach ($cats as $cat) {
	echo '
		<td>
			<input type="checkbox" name="user_cats[' . $user . '][]" value="' . $cat . '" ' . (in_array($cat, $user_cats) ? 'checked="checked"' : '') . '>
		</td>';
}

echo '
	</tr>';
}

echo '
	<tr>
		<td colspan="' . count($cats) . '"><input type="submit" name="submit" value="Save"></td>
	</tr>
</table>
</form>';

 

When the form is posted it will create a multidimensional array that can be looped through and the data saved to the database...in this example, since all users are displayed and updated at the same time, you could truncate the user_categories table, then just do inserts.  Alternatively, you'd have to check each users categories, then do updates / deletes as necessary.

 

Do a print_r on $_POST to see what it looks like, which should help with how to do the looping.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.