php - nested foreach in codeigniter view -
i have 4 tables: process, country, process_country , category. each process belongs category. country may have several processes available , process may available in several countries. below database structure:
category table process table country table process_country -------------- -------------- ------------- --------------- categoryid processid countryid countryid categoryname processname countryname processid categoryid countryid
i have created functions available processes each country wanted display processes under corresponding categories dont know how implement. able list processes , categories separately don't know how nest them. achieve page list each country:
> 1. category 1 > process 1 > process 2 > 2. category 2 > process 3
i thinking nested foreach loop:
<?php foreach ($category $c) { echo "<li>" . $c->categoryname . "</li>"; foreach ($process $r) { echo "<li><a href=\"" . base_url() . "index.php/process/id/" . $r->processid . "\" target=\"_blank\">" . $r->processname . "</a></li>"; } } ?>
but not know how implement foreach loop processes under category.
controller:
$data['process'] = $this->getprocesses(); //function gets process available country $data['category'] = $this->getcategories(); //function gets categories $this->load->view('includes/template', $data);
inside $data['process']
need have category id
, inside $data['category']
then can match both ,for example foreach
loop following
<?php foreach ($category $c) { echo "<li>" . $c->categoryname . "</li>"; foreach ($process $r) { if($r['cat_id']==$c['id']){ echo "<li><a href=\"" . base_url() . "index.php/process/id/" . $r->processid . "\" target=\"_blank\">" . $r->processname . "</a></li>"; } } } ?>
if both id
matches print them !
happy coding !
Comments
Post a Comment