我通过预订系统学习codeIgniter。当结果显示在不同的页面上时,它全部都是fint,但是当它放在同一页面上时(这就是我想要的){I}} $rooms_form_options
和$packages_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
和$package_form_options
?
答案 0 :(得分:2)
创建另一个单独的方法来填充这些变量:
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,
);
}