insert batch data array? - php

I want insert following data by insert_batch as in following example in database table (mysql):
HTML:
<input name="u_id[0][0]" value="76">
<input name="un[0][0]" value="1">
<input type="text" name="ue[0][0]" value="11">
<input type="text" name="up[0][0]" value="111">
<input name="u_id[1][0]" value="77">
<input name="un[1][1]" value="2">
<input type="text" name="ue[1][1]" value="22">
<input type="text" name="up[1][1]" value="222">
<input name="un[1][2]" value="3">
<input type="text" name="ue[1][2]" value="33">
<input type="text" name="up[1][2]" value="333">
PHP:
$u_id = $this->input->post('u_id');
$un = $this->input->post('un');
$up = $this->input->post('up');
$ue = $this->input->post('ue');
$data = array();
foreach ($un as $idx => $name) {
$data[] = array(
'u_id' => $u_id[$idx],
'un' => $un[$idx],
'up' => $up[$idx],
'ue' => $ue[$idx],
);
};
$this -> db -> insert_batch('units', $data);
I want insert they as this:
How should change php code and html code? what do i do?

I am assuming you are using CodeIgniter and that the name of the database table that you want to insert to is called 'units' and that its 'id' column is autoincrement.
I am basing off my solution from CodeIgniter User Guide Version 2.0.3 using a call to a helper ($this->db->insert_string()) of the Database class.
foreach ($data as $row)
{
$error_code = $this->db->insert_string('units', $row);
}
Refer to http://codeigniter.com/user_guide/database/helpers.html
The insert_string function that the Database class provides appears to take an associative array, build an INSERT statement from the elements inside, execute it and then return a numerical error code.

LOL, it's not pretty, but it might work sometimes:
<input name="u_id[]" value="76">
<input name="un[]" value="1">
<input type="text" name="ue[]" value="11">
<input type="text" name="up[]" value="111">
<input name="u_id[]" value="77">
<input name="un[]" value="2">
<input type="text" name="ue[]" value="22">
<input type="text" name="up[]" value="222">
<input name="un[]" value="3">
<input type="text" name="ue[]" value="33">
<input type="text" name="up[]" value="333">
$u_id=$this->input->post('u_id');
$un=$this->input->post('un');
$up=$this->input->post('up');
$ue=$this->input->post('ue');
for($i=0;$i<count($u_id);$i++){
for($ii=0;$ii<count($un[$i]);$ii++){
(count($un[$i])>1)?$unn=$un[$i][$ii+1]:$unn=$un[$i][$ii];
(count($ue[$i])>1)?$uen=$ue[$i][$ii+1]:$uen=$ue[$i][$ii];
(count($up[$i])>1)?$upn=$up[$i][$ii+1]:$upn=$up[$i][$ii];
$this->db->insert('units', array(//use db insert here
'u_id'=>$u_id[$i][0],
'un'=>$unn,
'ue'=>$uen,
'up'=>$upn,
));
}
}
I'd go so far as to suggest you not use it. But perhaps it might inspire someone to offer a better solution.
Cheers.

Related

Laravel Blade some values in 2 dimensional input array missing

i have a form that has editable answers, which are built like the following:
#foreach ($question->answers as $answer)
<input type="text" name="answers[{{$loop->index}}][text]">
<input type="hidden" name="answers[{{$loop->index}}][id]" value="{{$answer->id}}">
#endforeach
Which generates the html
<input type="text" name="answers[0][text]" value="Somevalue">
<input type="hidden" name="answers[0][id]" value="1">
<input type="text" name="answers[1][text]" value="Somevalue">
<input type="hidden" name="answers[1][id]" value="2">
In my Controller, I try to get the values from the request like this:
$requestAnswers = $request->input('answers');
foreach($requestAnswers as $key => $value) {
$id = $value['id'];
$text = $value['text'];
// Some answer handling
}
Somehow, when I try to access my answers, even when I dd($request->input('answers')) only some values show up, sometimes not having a text, sometimes not appearing at all.
Is there an error in my code or is the problem related to something else?
Thanks in advance!

How to insert radio, checkbox and textbox value in same variable post method name using codigniter?

my view page
<form action="<?php echo base_url()?>Quiz/add_stu_ans" method="POST">
<label>Who is usa president</label>
<input type="text" name="answer[]">
<label>Dhoni is cricket player player</label>
<input type="radio" name="answer[]" value="True">True
<input type="radio" name="answer[]" value="False">False
<label>Which is asian country</lable>
<input type="checkbox" name="answer[]" value="newyork">newyork
<input type="checkbox" name="answer[]" value="india">india
<input type="checkbox" name="answer[]" value="srilanka">srilanka
</form>
My controller page
public function add_stu_ans()
{
$values['stu_answer'] = $this->input->post('ans');
$this->Common_model->insert_record('student',$values);
$this->session->set_flashdata('message_name' , 'Your Data is Inserted');
redirect('Quiz/question');
}
And this is model
public function insert_record($table,$values) {
$this->db->insert($table,$values);
return $this->db->insert_id();
}
please anyone can tell me how to customize the code in controller
Only possible if you take another hiddent text field where your 'qstn_id' will be in value. take this field in array.
in coding part while taking foreach loop you have to take qstn_id as in "[key]" format otherwise you cant find skipped records.
for eg. :
foreach ($que as $key => $ansValue) {
$val2 = $ansValue[$key];
}
// $val2 store as your output.
This is code to save the data using json_encode. Let me know you get any error I have not run this code.
View File
<form action="<?php echo base_url()?>Quiz/add_stu_ans" method="POST">
<label>Who is usa president</label>
<input type="text" name="usa_president">
<label>Dhoni is cricket player player</label>
<input type="radio" name="cricket_player" value="True">True
<input type="radio" name="cricket_player" value="False">False
<label>Which is asian country</lable>
<input type="checkbox" name="country[]" value="newyork">newyork
<input type="checkbox" name="country[]" value="india">india
<input type="checkbox" name="country[]" value="srilanka">srilanka
</form>
Controller
public function add_stu_ans()
{
$usa_president = $this->input->post('usa_president');
$cricket_player= $this->input->post('cricket_player');
$country= $this->input->post('country');
$final_data = array(
'usa_president' => $usa_president,
'cricket_player' => $cricket_player,
'country' => $country,
);
$json_encode_data = json_encode($final_data);
$this->Common_model->insert_record('student',$json_encode_data);
$this->session->set_flashdata('message_name' , 'Your Data is Inserted');
redirect('Quiz/question');
}
Please check this at your end and time of fetch the data from the database then you need to pass that return data from the function json_decode.
$json_decode_data = json_decode($return_data_from_data_table);

inserting multiple rows to table in database codeigniter

I am trying to insert multiple rows to my database using a single query. I saw lots of questions related to this but they dont work for me.
I was able to save the multiple rows but the values are all the same, its the last data that i inputted so i guess i missed something?
Here are the codes:
Controller
public function addgrade()
{
$recordid = $this->input->post('recordid');
$studentid = $this->input->post('studentid');
$compid = $this->input->post('compid');
$subcompid = $this->input->post('subcompid');
$grade = $this->input->post('grade');
$classid = $this->input->post('classid');
foreach($this->UserModel->students() as $student):
if ($student->classid==$classid) { //$classid==33
$i=0;
$data = array(
'recordid' =>$recordid,
'studentid' => $studentid,
'compid' => $compid,
'subcompid' =>$subcompid,
'grade' =>$grade
);
$this->Crud->addgrade($data);
}
endforeach;
$this->session->set_flashdata('success', 'Successfully created!');
redirect('instructor');
}
Model
public function addgrade($data)
{
$this->db->insert('grade', $data);
}
View
<td>
<strong>
<input class="form-control inputScore" type="hidden" name="classid" value="<?php echo $classid;?>"></input>
<input class="form-control inputScore" type="hidden" name="recordid" value="<?php echo $record->id;?>"></input>
<input class="form-control inputScore" type="hidden" name="studentid" value="<?php echo $student->studentid;?>"></input>
<input class="form-control inputScore" type="hidden" name="compid" value="<?php echo $subcomp->id;?>"></input>
<input class="form-control inputScore" type="hidden" name="subcompid" value="<?php echo $subcomp->id;?>"></input>
<input class="form-control inputScore" type="text" name="grade"></input>
</strong></td>
All that gave me is this output on my database:
Did you try this instead? There's more detailed info about it here... Codeigniter - Append rows in view then add those rows to database table
$this->db->insert_batch('grade', $data);
In your form code, for all of your "name" variables such as name="classid", be sure to add "[]" at the end... name="classid[]"

Problems with passing data to twlio

Controller
public function searchNew()
{
$sid = //account_sid;
$token = //auth_token;
$client = new Services_Twilio($sid, $token);
$country = $this->input->post('select_country');
$type = $this->input->post('type');
$params = array(
'Contains' => $this->input->post('contains'),
'SmsEnabled' => $this->input->post('SMS'),
'MmsEnabled' => $this->input->post('MMS'),
'VoiceEnabled' => $this->input->post('Voice'),
'ExcludeAllAddressRequired' => TRUE,
'ExcludeLocalAddressRequired' => TRUE,
'ExcludeForeignAddressRequired' => TRUE
);
$numbers = $client->account->available_phone_numbers->getList($country, $type, $params);
return $numbers;
}
View
<form action="<?php echo base_url('tenant/phone_numer/searhNew');?>" method="post">
<input type="text" name="contains" class="form-control mbottom1" id="user_firstname" placeholder="Enter a description name for your number">
<select name="select_country" id="select_country" class="form-control">
<option disabled selected>Country</option>
<option value="US">USA (+1)</option>
</select>
<input id="checkcap1" type="checkbox" name="Voice" class="radio1" value="True"/>Voice
<input id="checkcap2" type="checkbox" name="SMS" class="radio1" value="True"/>SMS
<input id="checkcap3" type="checkbox" name="MMS" class="radio1" value="True"/>MMS
<input id="checktype1" type="radio" name="type" class="radio2" value="Local"/>Local
<input id="checktype2" type="radio" name="type" class="radio2" value="Mobile"/>Mobile
<input id="checktype3" type="radio" name="type" class="radio2" value="TollFree"/>Toll-Free
<input id="checktype3" type="radio" name="type" class="radio2" value="National"/>National
<button type="Submit">Test Submit</button>
</form>
I need help in getting the $country,$type,$params from my view file. I have done a few testing and when I change $country = $this->input->post('select_country'); to $country = "US" and the other variables into a static value, the code works fine, but when I change it their respective inputs the error message I am receiving is
The requested resource
/2010-04-01/Accounts/acount_sid/AvailablePhoneNumbers//Tollfree.json
was not found
Thanks for any help.
Do it with step by step flow of codeigniter.. After submit form , In controller print your post data..
print_r($_POST);
Check all data come from Form.. And check all by isset and pass in params,type , category. It will working fine..One More thing , In twilio library all parameters passing are important e.g.
$params=array();
if(isset($this->input->post('Voice'))){
$params['VoiceEnabled']=$this->input->post('Voice');
}

Passing arrays from HTML form to PHP

This is the HTML:
<input type="text" name="shortcut[]" value="a"/> do <input type="text" name="ses[]" value="1" disabled/><br>
<input type="text" name="shortcut[]" value="b"/> do <input type="text" name="ses[]" value="2" disabled/><br>
<input type="text" name="shortcut[]" value="c"/> do <input type="text" name="ses[]" value="3" disabled/><br>
How do I pass the values to PHP but connect the indexes of both arrays?
i.e. put in database value 1 where something = a,
put in database value 2 where something = b
and so on ...
The indexes are connected automatically, since they're numeric arrays.
$nvals = count($_REQUEST['shortcut']);
for ($i = 0; $i < $nvals; $i++) {
// do something with $_REQUEST['shortcut'][$i] and $_REQUEST['ses'][$i]
}
Combined array: array_map(null,$_POST['shortcut'],$_POST['ses']);
But you could of course could foreach over one of the 2, and fetch the other by key.
Note that if you have elements which may or may not be sent (checkboxes for instance), the only way to keep groups together is to assign them a number beforehand (name=sess[1], name=sess[2], etc.)
You can specify shortcut value as the key and the ses value as the value attribute:
<input type="text" name="input[a]" value="1" />
<input type="text" name="input[b]" value="2" />
<input type="text" name="input[c]" value="3" />
On the server-side you could use a foreach loop to iterate over the array:
foreach ($_POST['input'] as $shortcut => $ses) {
// process $shortcut and $ses
}

Categories