1

I'm trying to build a easy booking system in codeIgniter and using a database with a table called conference_rooms. I'm calling for this in my Booking_Model.phplike this:

public function __construct() { $this->load->database(); } public function get_room() { $query = $this->db->get('conference_rooms'); return $query->result_array(); } 

}

To display it I'm using my Booking.php class looking like this:

public function view() { $data['conference_rooms'] = $this->booking_model->get_room(); if (empty($data['conference_rooms'])) { show_404(); } $data['title'] = $data['conference_rooms']['title']; $this->load->view('templates/header', $data); $this->load->view('view', $data); $this->load->view('templates/footer'); } 

And my view.php:

<h3><?php echo $conference_rooms['title']; ?></h3> <div class="main"> <?php echo $conference_rooms['seats']; ?> </div> 

It won't find $room. What am I doing wrong?

UPDATE:

Basically changed the whole code, my view class now looks like this (changed to index in my Booking controller)

 public function index() { $this->load->helper('form'); $this->load->view('bootstrap/header'); $this->load->model('Booking_Model'); $rooms = $this->Booking_Model->get(); $rooms_form_options = array(); foreach ($rooms as $id => $room) { $rooms_form_options[$id] = $room->title; } $this->load->model('Package_Model'); $packages = $this->Package_Model->get(); $packages_form_options = array(); foreach ($packages as $id => $package) { $packages_form_options[$id] = $package->package_name; } $this->load->view('booking', array( 'rooms_form_options' => $rooms_form_options, 'packages_form_options' => $packages_form_options, )); $this->load->view('bootstrap/footer'); } 

And my booking.php;

<div> <?php echo form_label('Conference Room', 'id') ; ?> <?php echo form_dropdown('id', $rooms_form_options, set_value('id')); ?> </div> <div> <?php echo form_label('Package type', 'package_id') ; ?> <?php echo form_dropdown('package_id', $packages_form_options, set_value('package_id')); ?> </div> <div> <?php echo form_label('Antal deltagare', 'number_people') ; ?> <?php echo form_input('number_people', set_value('number_people')) ; ?> </div> <div> <?php echo form_submit('preview', 'Book'); ?> </div> 
13
  • Have you loaded model in controller file like $this->load->model('booking_Model', '', TRUE);? Commented Sep 15, 2015 at 6:51
  • Where are you setting the variables for title and seats inside the $conference_rooms Array Commented Sep 15, 2015 at 6:52
  • I've loaded it like $this->load->model('news_model'); and it works, because I forgot about in the beginning and got an error message for it that's gone now Commented Sep 15, 2015 at 6:53
  • I guess no where, so that might be the problem? How do I do it? @Amit Commented Sep 15, 2015 at 6:54
  • 1
    Did you figure out how to do it with the answers below? Commented Sep 15, 2015 at 7:03

2 Answers 2

2

Use this:

In Model

<? function __construct() { $this->load->database(); } function get_room() { $query = $this->db->get('conference_rooms'); $result = $query->result_array(); return $result; } ?> 

In controller

 function view() { $data['conference_rooms'] = $this->booking_model->get_room(); if (empty($data['conference_rooms'])) { show_404(); } else { $this->load->view('templates/header', $data); $this->load->view('view', $data); $this->load->view('templates/footer'); } } ?> 

in view

<h3><?php echo $conference_rooms[0]['title']; ?></h3> <div class="main"> <?php echo $conference_rooms[0]['seats']; ?> </div> 

Because of $conference_rooms[0] pointing 0 is we passing data with Objective array, so we need to point data. Check with print_r.

Sign up to request clarification or add additional context in comments.

Comments

1

MODEL

You need to row_array(); to return an array

public function get_room() { $query = $this->db->get('conference_rooms'); $rowcount = $query->num_rows(); if( $rowcount > 0 ){ return $row = $query->row_array(); } else { return FALSE; } } 

CONTROLLER

public function view() { $conference_rooms = $this->booking_model->get_room();// asing your array to variable if (empty($conference_rooms)) { show_404(); } else { $data['conference_rooms']=$conference_rooms;// pass your variable to data array to pass into view $this->load->view('templates/header', $data); $this->load->view('view', $data); $this->load->view('templates/footer'); } } 

VIEW

<h3><?php echo $conference_rooms['title']; ?></h3> <div class="main"> <?php echo $conference_rooms['seats']; ?> </div> 

6 Comments

It didn't work, it can't find $conference_rooms. Thinking that @ Kausha Mehta can be right and that the get_room doesn't work. How do I check that?
print_r( $conference_rooms) in controller and paste the value of it!!
Message: Undefined variable: conference_rooms
Can it have something to do with the router?
check updated answer for removing Undefined variable: conference_rooms for model file use num_rows()
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.