Checked checkboxes from database

I’ve been struggling with this problem for the last few days so if anyone can help point me into the right direction I would be grateful. I want to populate a checkbox element in a form where selected checkbox may already be checked depending if the value is set. Before I begin I have to stress that I was handed this problem and would structure the database tables differently but I have to use the current table structure.

I have a product description (string) which contains a list of cartridges. I want to break that string into an array and then loop through and set any cartridge that is already selected to be “checked”.

The string is ‘Single black compatible Blogs BLG001 inkjet cartridge<br />Single colour compatible Blogs BLG002 inkjet cartridge<br />’. Ideally I would like to narrow this string down to just model numbers (BLG001, BLG002). I have split the string into pieces with this code:


<?php
$cartridge = explode("<br />", $setDesc);
?>

I now want to check to see if a Model number exists in the array piece and if it does then mark the checkbox as “checked”.


<?php
if (in_array($cart_model, $cartridge)) {
   echo '<input type="checkbox" name="cartridge[]" value="'. $cart_id .'" checked="checked">' . $cart_name.'<br />';
} else {
   echo '<input type="checkbox" name="cartridge[]" value="'. $cart_id .'">' . $cart_name.'<br />';
}
?>

now try as I might it will not check the checkboxes that are already in the product description.

Here’s my checkbox query and checkbox code



```php

&lt;div&gt;
       &lt;p&gt;&lt;strong&gt;Cartridges:&lt;/strong&gt;&lt;/p&gt;
       &lt;?php
                $result = mysql_query("SELECT p.products_id, pd.products_name FROM products_description pd LEFT JOIN products p ON p.products_id = pd.products_id WHERE pd.products_name LIKE '% Blogs%' ORDER BY pd.products_name ASC") or die(mysql_error());
                $cartridges = array();
                while(($row =  mysql_fetch_assoc($result))) {
                         $cartridges[] = $row['products_id'];
                }
                $cartridgeClause = implode("," ,$cartridges);
                $result2 = mysql_query("SELECT p.products_id, p.products_model, pd.products_name FROM products_description pd LEFT JOIN products p ON p.products_id = pd.products_id WHERE p.products_id IN($cartridgeClause) ORDER BY pd.products_name ASC") or die(mysql_error());

                while ($row = mysql_fetch_array($result2)) {
                         $cart_id = (int)$row['products_id'];
                         $cart_model = mysql_real_escape_string($row['products_model']);
                         $cart_name = $row['products_name'];

                         if (in_array($cart_model, $cartridge)) {
                                echo '&lt;input type="checkbox" name="cartridge[]" value="'. $cart_id .'" checked="checked"&gt;' . $cart_name.'&lt;br /&gt;';
                         } else {
                                echo '&lt;input type="checkbox" name="cartridge[]" value="'. $cart_id .'"&gt;' . $cart_name.'&lt;br /&gt;';
                         }
                }
         ?&gt;
&lt;/div&gt;

I am not seeing the logic of doing a query to get ID’s, building an array of these ID’s, then breaking it down and doing another query based on these ID’s. Seems to me you would do one query based on LIKE ‘% Blogs%’.

I don’t see $setDesc defined in your code.
I would not use mysql_real_escape_string() except when entering data into your database.

But as far as checking if $cart_model is in the array $cartridge, this all works fine.

Here’s a stripped down version with some sample results. Note: I just threw some data in database so products_id from products table may not match products_id of description table So image below doesn’t match $setDesc array but assuming your data is correct you should get expected matches.

<?php				
	$setDesc = 'Single black compatible Blogs BLG001 inkjet cartridge<br />Single colour compatible Blogs BLG002 inkjet cartridge<br />';				
	$cartridge = explode("<br />", $setDesc);                            
	
	$result2 = mysql_query("SELECT p.products_id, p.products_model, pd.products_name FROM products_description pd LEFT JOIN products p ON p.products_id = pd.products_id WHERE pd.products_name LIKE '% Blogs%' ORDER BY pd.products_name ASC") or die(mysql_error());                                          
	while ($row = mysql_fetch_array($result2)) {
		$cart_id = (int)$row['products_id'];
		$cart_model = $row['products_model'];
		$cart_name = $row['products_name'];  
		$checked = (in_array($cart_model, $cartridge) ?	" checked=\\"checked\\"" : '');
		echo '<input type="checkbox" name="cartridge[]" value="'.$cart_id.'" '.$checked.' />' . $cart_name.'<br />';
	}                                                                                      
?>

Thanks for taking the time to help. I’ve tried your suggestion, firstly I can see my code was bloated by querying to get the product id, I realise I don’t need to do that, thanks for pointing that out. After adding your suggested code it still doesn’t work.

$setDesc was the products_description string which was in fact ‘Single black compatible Blogs BLG001 inkjet cartridge<br />Single colour compatible Blogs BLG002 inkjet cartridge<br />’.

I’ve checked the query and it’s bring back the correct results and number of records. The array is correct, splitting the string at the ‘<br />’. It’s just when it’s matching up the model number with the array value. Any ideas on why it would work for you and not me?

Well it worked for me because what I entered into pd.products_name was the same as the p.products_model, which also was a match for an item in the $setDesc array.
Have you verified that p.products_model has an exact match to an item in the $setDesc array?

  1. What is the reasoning for having these two tables, one for products one for product descriptions?
    Can you provide sample data from these two DB tables or an sql file?
  2. Where does $setDesc come from?
    Can you provide full page where $setDesc is defined?

The php code you provided isn’t showing $setDesc and so must be part of a larger page. As you mentioned I would think there must be a better way of doing this than breaking a string of “models” apart. ID numbers would make more sense.