我目前正在预订机票。我有场地没有。对于乘客来说,无论你输入了什么,它都将用于确定将给出多少票,我也会将其设为会话,以便我可以在另一页中使用它。选择可用的航班后
示例:我没有乘客2。然后将有2张门票。我买票的字段是:pass_type(成人,老人,孩子),姓名,生日和性别
注意:我也试过使用explode(); - 爆炸意味着如果它找到了一个/ - (第一个分隔符)它会将它分开吗?在我的情况下,没有逗号,/或破折号
当我尝试var_dump时,这就是结果:
array(6) {
[0]=>
array(2) {
[0]=>
string(14) "Senior Citizen"
[1]=>
string(14) "Senior Citizen"
}
[1]=>
array(2) {
[0]=>
string(1) "X"
[1]=>
string(1) "X"
}
[2]=>
array(2) {
[0]=>
string(2) "XX"
[1]=>
string(2) "XX"
}
[3]=>
array(2) {
[0]=>
string(3) "XXX"
[1]=>
string(3) "XXX"
}
[4]=>
array(2) {
[0]=>
string(10) "1995-10-05"
[1]=>
string(10) "1995-10-05"
}
[5]=>
array(2) {
[0]=>
string(4) "Male"
[1]=>
string(4) "Male"
}
}
控制器
public function tickp()
{
$this->form_validation->set_error_delimiters('<div class="alert alert-danger" role="alert">', '</div>');
$this->form_validation->set_rules('pass_type[]', 'Traveler', 'required|trim');
$this->form_validation->set_rules('t_fname[]', 'First Name', 'required|trim');
$this->form_validation->set_rules('t_mname[]', 'Middle Name', 'required|trim');
$this->form_validation->set_rules('t_lname[]', 'Last Name', 'required|trim');
$this->form_validation->set_rules('t_bday[]', 'Birthday', 'required|trim');
$this->form_validation->set_rules('t_gender[]', 'Gender', 'required|trim');
if ($this->form_validation->run() == FALSE)
{
$this->selected_flight();
}
else
{
$traveler_info = array(
$pass_type = $_POST['pass_type'],
$t_fname = $_POST['t_fname'],
$t_mname = $_POST['t_mname'],
$t_lname = $_POST['t_lname'],
$t_bday = $_POST['t_bday'],
$t_gender = $_POST['t_gender'],
);
echo "<pre>";
var_dump($traveler_info);die;
echo "</pre>";
$this->CrudModel->insert('ticket',$traveler_info);
}
}
查看
<form method="post" action="<?= base_url() . 'User/tickp' ?>">
<?php for ($i = 0; $i < $this->session->userdata('pass_num'); $i++) { ?>
<!-- Code insert end-->
<div class="divTableRow">
<!-- Code replace-->
<div class="divTableCell">
<?= validation_errors();?>
Traveler<br>
<select name="pass_type[]" id="pass_type[]">
<option value="---" selected disabled>---</option>
<option value="Senior Citizen">Senior Citizen</option>
<option value="Adult">Adult</option>
<option value="Child">Child</option>
</select>
</div>
<div class="divTableCell">First Name<br> <input type="text" name="t_fname[]" id="t_fname[]"></div>
<div class="divTableCell">Middle Name <br> <input type="text" name="t_mname[]" id="t_mname[]"></div>
<div class="divTableCell">Last Name <br> <input type="text" name="t_lname[]" id="t_lname[]"></div>
<div class="divTableCell">
Date of Birth <br>
<input type="date" name="t_bday[]" id="t_bday[]">
</div>
<div class="divTableCell">
Gender<br>
<select id="t_gender[]" name="t_gender[]">
<!-- Code replace end-->
<option value="Male">Male</option>
<option value="Female">Female</option>
</select>
</div>
</div>
<!-- Code insert-->
<?php } ?>
<button type="submit" class="booksend">BOOK</button>
</form>
答案 0 :(得分:1)
构造$traveler_info
数组以使用db->batch_insert()
可接受的结构会更容易。它需要一个关联数组field_names =&gt;要执行的每个插入的field_values。
//add a sub-array to $traveler_info for each 'pass_type'
for($i = 0; $i < count($_POST['pass_type']); $i++)
{
$traveler_info[] = array(
`pass_type` => $_POST['pass_type'][$i],
't_fname' => $_POST['t_fname'][$i],
't_mname' => $_POST['t_mname'][$i],
't_lname' => $_POST['t_lname'][$i],
't_bday' => $_POST['t_bday'][$i],
't_gender' => $_POST['t_gender'][$i],
);
}
$this->db->insert_batch('mytable', $traveler_info);