Amr Magdy Amr Magdy - 1 month ago 22
HTML Question

adding category and its subcategories in html select

i am trying to add father categories then its subcategories as options in select , so i did the following

<select class="form-control" name="select">
<?php foreach($father as $father) : ?>
<option value="<?php echo $father->id; ?>">
*Category: <?php echo $father->name." :-"; ?>
</option><?php foreach($child as $child) : ?><?php if($child->category_id==$father->id){?>
<option value="<?php echo $child->id; ?>">
-Sub-Category: <?php echo $child->name; ?>
</option><?php } endforeach; ?><?php endforeach; ?>

here is my controller

public function addg()
$data['userdata'] = $this->session->all_userdata();
$data['father'] = $this->catagories_model->get_catagories();
$data['child'] = $this->catagories_model->get_catagories_child();
$this->load->view('catagories/grand/add', $data);

but it loops into the first father category and display its children and just displays the other father categories without going into the if statement what could be wrong ?

Answer Source

This line isn't doing anything:

<?php foreach($child as $child) : ?>

It should be:

<?php foreach($childs as $child) : ?>

Assuming $childs is the array where you're storing all the subcategories.