Я изучаю CodeIgniter, создавая систему бронирования. Когда результат показывался на другой странице, он все шел финтом, но когда он помещался на одну и ту же страницу (именно так я хочу это), $rooms_form_options
и $packages_form_options
становятся неопределенными после $rooms_form_options
формы
<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> <?php echo form_close(); ?> <?php if(isset($_POST["preview"])) { if($messages) { echo html_escape($messages); } else { ?> <div class="alert alert-success">Number of participants: <?php echo html_escape($guests); ?> </div> <div class="alert alert-success">Room: <?php echo html_escape($room); ?> </div> <div class="alert alert-success">Package: <?php echo html_escape($package); ?> </div> <div class="alert alert-success">Price per participant: <?php echo html_escape($price); ?> </div> <div class="alert alert-success">Total Price: <?php echo html_escape($guests*$price); ?> </div> <?php } }?>
Здесь я получаю $rooms_form_options
и $packages_form_options
:
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'); }
Я обрабатываю форму в методе, называемом preview
и здесь я указываю на страницу бронирования:
// load view with people, room and package values $this->load->view('bootstrap/header'); $this->load->view('booking', array( 'guests' => $guests, 'room' => $room_title, 'package' => $package_title, 'alias' => $package_alias, 'price' => $price, 'messages' => $messages, ));
Как я могу убедиться, что $rooms_form_options
и $rooms_form_options
определены после $rooms_form_options
формы?
Создайте другой отдельный метод для заполнения этих переменных:
private function get_options() { $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; } return array( 'packages_form_options' => $packages_form_options, 'rooms_form_options' => $rooms_form_options, ); }
Все будет выглядеть так:
public function index() { $this->load->helper('form'); $this->load->view('bootstrap/header'); $options = $this->get_options(); $this->load->view('booking', array( 'guests' => '', 'room' => '', 'package' => '', 'alias' => '', 'price' => '', 'messages' => '', 'rooms_form_options' => $options['packages_form_options'], 'packages_form_options' => $options['rooms_form_options'], )); $this->load->view('bootstrap/footer'); } public function preview() { $this->load->helper('form'); $this->load->view('bootstrap/header'); $options = $this->get_options(); $this->load->view('booking', array( 'guests' => $this->input->post('guests'), 'room' => $this->input->post('room_title'), 'package' => $this->input->post('package_title'), 'alias' => $this->input->post('package_alias'), 'price' => $this->input->post('price'), 'messages' => $this->input->post('messages'), 'rooms_form_options' => $options['packages_form_options'], 'packages_form_options' => $options['rooms_form_options'], )); $this->load->view('bootstrap/footer'); } private function get_options() { $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; } return array( 'packages_form_options' => $packages_form_options, 'rooms_form_options' => $rooms_form_options, ); }