1

i have MySql Table name 'category', in that table i have

id   catname   parent_id
1    animals
2    vegs        
3    dog        1
4    cat        1
5    carrot     2

i just wanna display this data in html nested 'ul' like

<ul>
  <li>Animals
      <ul>
         <li>dog</li>
         <li>cat</li>
      </ul>
  </li>
  <li>Vegs
     <ul>
         <li>Carrot</li>
      </ul>
 </li>
</ul>

with php, please help me to get this data with php(CodeIgniter) and display.

Kamaal ABOOTHALIB
  • 3,527
  • 1
  • 19
  • 25
  • 1
    This question has been made several times here. Please use the "search" feature of the site. – J. Bruni Feb 06 '12 at 07:19
  • For example: http://stackoverflow.com/questions/2871861/how-to-build-unlimited-level-of-menu-through-php-and-mysql/ - and see the "Related" questions in the right-side of this one to get lots of similar questions / answers. – J. Bruni Feb 06 '12 at 07:24

1 Answers1

1

Here are the three components for your question: controller, model and view...

<!-- THE VIEW -->

<ul>
<?php foreach($main_cat->result() as $rowm) : ?>

    <li><?php echo $rowm->catname ?>
        <ul>
        <?php
            $id = $rowm->id;
            $sec_cat = $this->yourmodel->get_secondary($id);
            foreach($sec_cat->result() as $rows) :
        ?>

            <li><?php echo $rows->catname ?></li>

        <?php endforeach; ?>
        </ul>
    </li>

<?php endforeach; ?>
</ul>



<!-- THE CONTROLLER -->

<?php

class Welcome extends CI_Controller {   
   function index()
   {
      $data['main_cat'] = $this->yourmodel->get_main();
      $this->load->view('welcome_view',$data);
   }
} 
?>



  <!-- THE MODEL -->

<?php
class Yourmodel extends CI_Model {

function get_main()
{
    $this->db->where('parent_id','');
    $query = $this->db->get('category');
    return $query;
}

function get_secondary($parent)
{
    $this->db->where('parent_id',$parent);
    $query = $this->db->get('category');
    return $query;
}
}
?>
Albux
  • 84
  • 2