I currently have two arrays that look like this:
Swatches:
Array
(
[0] => Array
(
[swatch_id] => 8
[swatch_file] => orange_swatch.jpg
)
[1] => Array
(
[swatch_id] => 9
[swatch_file] => pink_swatch.jpg
)
[2] => Array
(
[swatch_id] => 10
[swatch_file] => green_swtach.jpg
)
)
Selected Swatches:
Array
(
[0] => Array
(
[swatches_has_products_id] => 18
[swatches_swatch_id] => 8
[products_product_id] => 19
)
[1] => Array
(
[swatches_has_products_id] => 19
[swatches_swatch_id] => 10
[products_product_id] => 19
)
)
I am trying to check a check box if $swatch['swatch_id'] is equal to $selected_swatches['swatches_swatch_id']. I am doing this with the following code:
<?php foreach ($swatches as $k => $swatch): ?>
<li>
<img src="<?php echo base_url(); ?>media/images/swatches/<?php echo $swatch['swatch_file']; ?>" height=""/>
<input type="checkbox" name="product_has_swatch[]" value="<?php echo $swatch['swatch_id']; ?>" <?php if($swatch['swatch_id'] == $selected_swatches[$k]['swatches_swatch_id']) : ?> checked="checked" <?php endif; ?> />
</li>
<?php endforeach; ?>
However I get the following error, if there are no matches:
A PHP Error was encountered
Severity: Notice
Message: Undefined offset: 2
Filename: products/create.php
Line Number: 137
Line 137 is the if to check if I have matches; Where am I going wrong?
One solution is:
Hope it helps