简体   繁体   中英

Codeigniter Category and Sub Categories Only Show Last Category and Sub Categories

I want to show room type and rooms number inside it example:

room type #1

  • room number 1
  • room number 2
  • room number 3
  • room number 4

room type #2

  • room number 1
  • room number 2
  • room number 3
  • room number 4

and so on.

this is my table

room_type
-------
room_type_id (int)
room_type_name (varchar)
room_price (float)
room_detail (text)

room
-------
room_e_id (int)
room_type_id (int)
room_number (int)
room_status (varchar)

Here's my Model

function get_room_type() {
    $query = $this->db->get('room_type');
    $return = array();
    foreach ($query->result() as $room) {
        $return['$room->room_type_id'] = $room;
        $return['$room->room_type_id'] -> subs = $this->get_room_list($room->room_type_id);
    } return $return;
}

public function get_room_list($room_type_id) {
    $this->db->where('room_type_id', $room_type_id);
    $query = $this->db->get('room');
    return $query->result();
}

And this is my Controller

$data['room_list'] = $this->m_fo->get_room_type();

And view

<ul>
<?php foreach ($room_list as $row) { ?>
 <li>
  <?php echo $row->room_type; ?> <?php
   if (!empty($row->subs)) {
   echo '<ul>';
   foreach ($row->subs as $sub) {
    echo '<li>' . $sub->room_no . ' - ' . $sub->room_status . '</li>';
    } echo '</ul>';
   }
  ?>
 </li> <?php } ?>
</ul>

but it's only show the last room type and room number in my page as you can see here -- screenshot is anything wrong or less? Thanks You

Inside the single quotes everything consider as string so you should change these two lines like this .

 $return[$room->room_type_id]->rooms = $room;
 $return[$room->room_type_id]->subs = $this->get_room_list($room->room_type_id);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM