I am new in codeigniter framework. I am making an array within array and within array using foreach loop for getting record from 3 tables. I have make like this way array
Array ( [devices] => Array ( [0] => Array ( [id] => 1 [device_name] => Smartphone [device_image] => device-images/1d57a675edbb2ff97c5bc35fce27517c.png [device_icon] => device-images/325af5a3587cdfc8f35ae345a2d432a3.png [all_manufacturers] => Array ( [0] => Array ( [id] => 1 [fk_device_id] => 1 [manufacture] => Apple [models] => Array ( [0] => Array ( [id] => 1 [fk_deviceid] => 1 [fk_manufactureid] => 1 [model_name] => iPhone 11 Pro Max ) [1] => Array ( [id] => 2 [fk_deviceid] => 1 [fk_manufactureid] => 1 [model_name] => iPhone 11 Pro ) [2] => Array ( [id] => 3 [fk_deviceid] => 1 [fk_manufactureid] => 1 [model_name] => iPhone 11 ) ) ) [1] => Array ( [id] => 2 [fk_device_id] => 1 [manufacture] => Samsung [models] => Array ( ) ) [2] => Array ( [id] => 3 [fk_device_id] => 1 [manufacture] => huawei [models] => Array ( ) ) [3] => Array ( [id] => 5 [fk_device_id] => 1 [manufacture] => Sony ) ) ) [1] => Array ( [id] => 2 [device_name] => Tablet [device_image] => device-images/fd9e3f3192547a89c07e30fbcb7e202b.png [device_icon] => device-images/9f39fb4583174c71e3b7f92ec3e5ed86.png [all_manufacturers] => Array ( [0] => Array ( [id] => 6 [fk_device_id] => 2 [manufacture] => Apple [models] => Array ( [0] => Array ( [id] => 4 [fk_deviceid] => 2 [fk_manufactureid] => 6 [model_name] => iPad 3 (2012) ) [1] => Array ( [id] => 6 [fk_deviceid] => 2 [fk_manufactureid] => 6 [model_name] => iPad 2 (2011) ) [2] => Array ( [id] => 7 [fk_deviceid] => 2 [fk_manufactureid] => 6 [model_name] => iPad 4 (2012) ) ) ) [1] => Array ( [id] => 7 [fk_device_id] => 2 [manufacture] => Microsoft ) ) ) [2] => Array ( [id] => 3 [device_name] => Laptop [device_image] => device-images/60042e62d2fa440d3c467114538a1293.png [device_icon] => device-images/2044d96ba9274b5b1e3155103fa90506.png [all_manufacturers] => Array ( [0] => Array ( [id] => 11 [fk_device_id] => 3 [manufacture] => Apple ) ) ) [3] => Array ( [id] => 4 [device_name] => Watch [device_image] => device-images/8f10aad91a793372e50a3275db132e4e.png [device_icon] => device-images/788dc55b9b6e0f805d63a42fd888e1bb.png [all_manufacturers] => Array ( [0] => Array ( [id] => 9 [fk_device_id] => 4 [manufacture] => Apple ) ) ) [4] => Array ( [id] => 6 [device_name] => Camera [device_image] => device-images/88929629884491fd080505b56f1b2b0d.png [device_icon] => device-images/88fa1d78fe127e757c1cfc90ec925a14.png ) ) )
Issue in view page because showing notice error of Undefined index: all_manufacturers view page code is below
<div class="row"> <div class="col-lg-3 col-md-6"> <div class="sidebar"> <?php if($devices) { foreach($devices as $dkey => $device) { $all_manufacturers = $device["all_manufacturers"];?> <div class="dropdown cust-dropdown"> <button class="btn-dropdown cust-btn-dropdown" type="button"><div class="box-shop-icons"><img src="<?= base_url('assets/uploads/'.$device["device_icon"])?>" ></div> <?= $device['device_name']?> </button> <div class="dropdown-menu-custom"> <div class="radio-btns cust-height"> <!-- inner radio button --> <?php if ($all_manufacturers) { foreach ($all_manufacturers as $manufacture) { ?> <div class="dropdown"> <button class="btn btn-primary dropdown-toggle" type="button" data-toggle="dropdown"><?= $manufacture['manufacture']?> <span class="caret"></span></button> <ul class="dropdown-menu"> <li><a href="#">iPhone 11 pro max</a></li> <li><a href="#">iPhone 11 pro</a></li> <li><a href="#">iPhone 11</a></li> <li><a href="#">iPhone Xs max</a></li> <li><a href="#">iPhone Xs</a></li> <li><a href="#">iPhone X</a></li> <li><a href="#">iPhone 8plus</a></li> </ul> </div> <?php } } ?> </div> </div> </div> <?php } } ?> </div>
These 3 tables are linked with eachother
Advertisement
Answer
The last array doesn’t have “all_manufacturers” set on it, so the error is that you are trying to set $all_manufacturers without checking if it is set or not.
Remove this line at the top of the loop for devices:
$all_manufacturers = $device["all_manufacturers"];
Then change this code:
<?php if ($all_manufacturers) { foreach ($all_manufacturers as $manufacture) { ?>
To this:
if (!empty($device["all_manufacturers"])) { foreach ($device["all_manufacturers"] as $manufacture) {