Angel Angel -4 years ago 34
PHP Question

How to save using array? codeigniter

I am currently doing a airline reservation. I have field no. of passengers, what ever you have entered it, it will be use to determine how many tickets will be given and I also make it session so that I can use it in my another page. After selecting a flight that is available

Example: I put 2 in no of passengers. then there will be a 2 tickets. The fields that I've for tickets are: pass_type(Adult, senior, child), name, birthday and gender

NOTE: I also tried to use the explode(); - explode means if it found a ,/-(first delimiter) it will separate it right? In my case there's no comma, / or dash

When I tried to var_dump this is the result:

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"
}
}


Controller

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);
}

}


View

<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>

Answer Source

It will be easier to construct the $traveler_info array to use the structure acceptable to db->batch_insert(). It requires an associative array of field_names => field_values for each insert to be performed.

//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);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download