Update batch using checkbox, failed - php

I want to update the data using array and checkbox. If the checkbox checked, status become "1". Else, leave it "0".
I have try something like this
<?php
foreach($report as $r){;
?>
<input type="checkbox" name="status[]" value="1" value="<?php echo $r->status;?>">
<input type="hidden" name="id_name[]" value="<?php echo $r->id_name;?>">
<input type="hidden" name="name[]" value="<?php echo $r->name;?>">
<?php } ?>
and this
<input type="checkbox" name="status[]" value="1">
<input type="hidden" name="id_name[]" value="<?php echo $r->id_name;?>">
<input type="hidden" name="name[]" value="<?php echo $r->name;?>">
But both of them update the first row even I check the third or the fourth row.
my controller is something like this
function update_approval() {
$status = $this->input->post('status');
$id_name = $this->input->post('id_name');
$name = $this->input->post('name');
for($a=0; $a< sizeof ($id_name); $a++) {
$data[$a] = array(
'status' => $status[$a],
'id_name' => $id_name[$a],
'name' => $name[$a]
);
}
$this->db->update_batch('tbl_m_name', $data, 'id_name');
}
Thanks in advance

I've solved it. Thanks,
I change the position, here is my views
<input type="checkbox" name="id_name[]" value="<?php echo $r->id_name;?>">
<input type="hidden" name="name[]" value="<?php echo $r->name;?>">
and here is my controller
function update_approval() {
$id_name = $this->input->post('id_name');
$name = $this->input->post('name');
$data[$a] = array();
for($a=0; $a< sizeof ($id_name); $a++) {
$data[] = array(
'status' => 1,
'id_name' => $id_name[$a],
'name' => $name[$a]
);
}
$this->db->update_batch('tbl_m_name', $data, 'id_name');
}
so if I check the box, it will post the id_name that I choose

thanks, but I had tried yours, I get undefinied variable $a. So, I changed the position and removed $data[$a]= array(); . Below is my code (after resolve my problem) based on yours:
function update_approval() {
$id_name = $this->input->post('id_name');
$name = $this->input->post('name');
for($a=0; $a< sizeof ($id_name); $a++) {
$data[$a] = array(
'status' => 1,
'id_name' => $id_name[$a],
'name' => $name[$a]
);
}
$this->db->update_batch('tbl_m_name', $data, 'id_name');
}

Related

Best way to submit many same-record? [duplicate]

I have a form like the one below which is posted to contacts.php, and the user can dynamically add more with jQuery.
<input type="text" name="name[]" />
<input type="text" name="email[]" />
<input type="text" name="name[]" />
<input type="text" name="email[]" />
<input type="text" name="name[]" />
<input type="text" name="email[]" />
If I echo them out in PHP with the code below,
$name = $_POST['name'];
$email = $_POST['account'];
foreach($name as $v) {
print $v;
}
foreach($email as $v) {
print $v;
}
I will get something like this:
name1name2name3email1email2email3
How can I get those arrays into something like the code below?
function show_Names($n, $m)
{
return("The name is $n and email is $m, thank you");
}
$a = array("name1", "name2", "name3");
$b = array("email1", "email2", "email3");
$c = array_map("show_Names", $a, $b);
print_r($c);
so my output is like this:
The name is name1 and email is email1, thank you
The name is name2 and email is email2, thank you
The name is name3 and email is email3, thank you
They are already in arrays: $name is an array, as is $email
So all you need to do is add a bit of processing to attack both arrays:
$name = $_POST['name'];
$email = $_POST['account'];
foreach( $name as $key => $n ) {
print "The name is " . $n . " and email is " . $email[$key] . ", thank you\n";
}
To handle more inputs, just extend the pattern:
$name = $_POST['name'];
$email = $_POST['account'];
$location = $_POST['location'];
foreach( $name as $key => $n ) {
print "The name is " . $n . ", email is " . $email[$key] .
", and location is " . $location[$key] . ". Thank you\n";
}
E.g. by naming the fields like
<input type="text" name="item[0][name]" />
<input type="text" name="item[0][email]" />
<input type="text" name="item[1][name]" />
<input type="text" name="item[1][email]" />
<input type="text" name="item[2][name]" />
<input type="text" name="item[2][email]" />
(which is also possible when adding elements via JavaScript)
The corresponding PHP script might look like
function show_Names($e)
{
return "The name is $e[name] and email is $e[email], thank you";
}
$c = array_map("show_Names", $_POST['item']);
print_r($c);
You could do something such as this:
function AddToArray ($post_information) {
//Create the return array
$return = array();
//Iterate through the array passed
foreach ($post_information as $key => $value) {
//Append the key and value to the array, e.g.
//$_POST['keys'] = "values" would be in the array as "keys"=>"values"
$return[$key] = $value;
}
//Return the created array
return $return;
}
The test with:
if (isset($_POST['submit'])) {
var_dump(AddToArray($_POST));
}
This for me produced:
array (size=1)
0 =>
array (size=5)
'stake' => string '0' (length=1)
'odds' => string '' (length=0)
'ew' => string 'false' (length=5)
'ew_deduction' => string '' (length=0)
'submit' => string 'Open' (length=4)
I came across this problem as well. Given 3 inputs: field[], field2[], field3[]
You can access each of these fields dynamically. Since each field will be an array, the related fields will all share the same array key. For example, given input data:
Bob, bob#bob.com, male
Mark, mark#mark.com, male
Bob and his email and sex will share the same key. With this in mind, you can access the data in a for loop like this:
for($x = 0; $x < count($first_name); $x++ )
{
echo $first_name[$x];
echo $email[$x];
echo $sex[$x];
echo "<br/>";
}
This scales as well. All you need to do is add your respective array vars whenever you need new fields to be added.
You can use an array of fieldsets:
<fieldset>
<input type="text" name="item[1]" />
<input type="text" name="item[2]" />
<input type="hidden" name="fset[]"/>
</fieldset>
<fieldset>
<input type="text" name="item[3]" />
<input type="text" name="item[4]" />
<input type="hidden" name="fset[]"/>
</fieldset>
I added a hidden field to count the number of the fieldsets.
The user can add or delete the fields and then save it.
However, VolkerK's solution is the best to avoid miss couple between email and username. So you have to generate HTML code with PHP like this:
<? foreach ($i = 0; $i < $total_data; $i++) : ?>
<input type="text" name="name[<?= $i ?>]" />
<input type="text" name="email[<?= $i ?>]" />
<? endforeach; ?>
Change $total_data to suit your needs. To show it, just like this:
$output = array_map(create_function('$name, $email', 'return "The name is $name and email is $email, thank you.";'), $_POST['name'], $_POST['email']);
echo implode('<br>', $output);
Assuming the data was sent using POST method.
This is an easy one:
foreach($_POST['field'] as $num => $val) {
print ' ' . $num . ' -> ' . $val . ' ';
}
Already is an array.
My inputs are:
<input name="name[]" value='joe'>
<input name="lastname[]" value='doe'>
<input name="name[]" value='jose'>
<input name="lastname[]" value='morrison'>
In the $_POST data, returns the following:
[name] => Array
(
[0] => 'joe'
[1] => 'jose'
)
[lastname] => Array
(
[0] => 'doe'
[1] => 'morrison'
)
You can access to these data, in the following way:
$names = $_POST['name']
$lastnames = $_POST['lastname']
// accessing
echo $names[0]; // joe
This way It is very useful for creating pivot tables.
Using this method should work:
$name = $_POST['name'];
$email = $_POST['account'];
while($explore=each($email)) {
echo $explore['key'];
echo "-";
echo $explore['value'];
echo "<br/>";
}

Unable to insert multiple checkbox data in the database

UPDATED. I'm having problem about inserting multiple checkbox data in the database.
Here is my view
<?php
$i = 0;
$qArr = array();
$qArrc = 0;
$qArr1 = array();
$qArrc1 = 0;
$uArr = array();
$u = 0;
foreach($questions->result() as $q){ ?>
<input type="checkbox" name="check[<?php echo $i; ?>]" value="<?php echo $qArr[$qArrc++] = $q -> questions; ?>"> <?php echo $q->questions; ?> <br>
<input class="form-control" value="<?php echo $uArr[$u++] = $this->session->userdata('user_id'); ?>" name="hidden[<?php echo $i; ?>]" type="hidden">
<input class="form-control" value="<?php echo $qArr1[$qArrc1] = $q->id; ?>" name="hidden1[<?php echo $i; ?>]" type="hidden">
<?php
$i++;
}?>
<button type="submit" name="submit" class="btn btn-primary">Submit</button>
Here's my controller
for($i = 0; $i<count($this->input->post('check')); $i++){
$data1 = array(
'question' => $this->input->post('check')[$i],
'speaker_id' => $this->input->post('hidden')[$i],
'question_id' => $this->input->post('hidden1')[$i]
);
$this->input->post('submit');
$this->Speaker_Model->insert_speakerfeedback($data1);
}redirect('speaker/createfeedback');
Here's my model
public function insert_speakerfeedback($data1){
$this->db->insert("speakerdata", $data1);
}
Modify your for loops :
for($i = 0; $i<count($this->input->post('check')); $i++){
to use foreach instead` :
foreach ($this->input->post('check') as $i => $value) {
So it will prevent getting undefined index if you skip some checkbox. And the redirect line should be outside of the loops.
For bulk insert, you could use insert_batch function.
Within the loop, change :
$data1 = array(
to
$data1[] = array(
So it will not overwriting on each iteration.
And on the Model, change :
$this->db->insert("speakerdata", $data1);
to :
$this->db->insert_batch("speakerdata", $data1);

How to insert row by row values in single submit

This is my view Page
This is Dynamically fields Test Name and units are from Table I need to insert Test name and Result and Normal value also Unit.
My Controller
$patient_id = $this->input->post('patient_id');
$doctor_id = $this->input->post('doctor_id');
$prescription_id = $this->input->post('prescription_id');
$lab_result =$this->input->post('lab_result');
$lab_test =$this->input->post('lab_test');
$units =$this->input->post('units');
$normal_value =$this->input->post('normal_value');
$cat_id = $this->input->post('cat_id');
for($i=0; $i<count($prescription_id); $i++)
{
$labreport[] = array(
'patient_id' => $patient_id[$i],
'doctor_id' => $doctor_id[$i],
'prescription_id' =>$prescription_id[$i],
'lab_result' => $lab_result[$i],
'lab_test' => $lab_test[$i],
'units' => $units[$i],
'normal_value' => $normal_value[$i],
'cat_id' => $cat_id, );
//echo '<pre>'; print_r($labreport); '</pre>'; exit;
}
$stringlabreport= json_encode($labreport);
$this->db->insert('patient_lab_report',$stringlabreport);
if($this->db->affected_rows()){
return true;
} else {
return false;
}
$this->session->set_flashdata('message','Lab Report Added Successfully');
redirect('laboratory_report/all');
=========================
And This is My View Code
<tr>
<td><input type="hidden" value="<?php echo $data->name; ?>"name="lab_test[]"><?php echo $data->name; ?></td>
<td><input type="text" value="" name="lab_result[]" class="form-control"></td>
<td><input type="hidden" value="<?php echo $data->units; ?>" name="units[] "> <?php echo $data->units; ?></td>
<td><input type="hidden" value="<?php echo $data->n_value; ?>" name="normal_value[] "> <?php echo $data->n_value; ?></td>
</tr>
Use array names for input. I.E.:
<form action="" method="post" name="someform">
<input name="row[0][test_name]"/>
<input name="row[0][result]"/>
<input name="row[1][test_name]"/>
<input name="row[1][result]"/>
<input type="submit" name="submitted" value="send">
</form>
So upon submission, parsing in php, use filter_input with FILTER_REQUIRE_ARRAY as option:
<?php
$submitted = filter_input(INPUT_POST, 'submitted');
if(!empty($submitted)){
$rows = filter_input(INPUT_POST,'row', FILTER_DEFAULT, FILTER_REQUIRE_ARRAY);
$parsed_rows = [];
foreach($rows as $row){
if(!empty($row['test_name']) && !empty($row['result'])){
$parsed_rows[] = $row;
}
}
}
?>

set value default codeigniter

I have this function (get_profile_info)
function get_profile_info()
{
$this->db->where('username',$this->session->userdata('username'));
$get_profile_info = $this->db->get('memberships');
if($get_profile_info->num_rows() == 1){
foreach ($get_profile_info->result() as $row)
{
echo $row->firstname;
echo $row->lastname;
}
}
}
The question is how do i put this results: firstname and lastname into input form default set_value: echo "Firstname".form_input('firstname2');
Try this code:
foreach($get_profile_info->result() as $row)
{
$data = array(
'name' => 'firstname',
'id' => 'firstname',
'value' => $row->firstname,
);
echo form_input($data);
// do the same for lastname
}
Set input value of your view file like this.
<input type="text" value="<?php echo $data['firstname']; ?>" />
you have to pass data array from controller.
Try this
function get_profile_info()
{
$this->db->where('username',$this->session->userdata('username'));
$get_profile_info = $this->db->get('memberships');
if($get_profile_info->num_rows() == 1){
return $get_profile_info->row();
}
function your_controller
{
$data['row'] = get_profile_infor()
$this->load->view("yourview",$data)
}
IN View
<input type="text" name="firstname" value="<?php echo set_value('firstname',$row->firstname)?>" />
<input type="text" name="lastname" value="<?php echo set_value('lastname',$row->lastname)?>" />

Inserting multiple rows in database codeigniter

I have some issue about the inserting data. It will insert only the waybillno data but the quantity is always same. Please check my code - I think the model is wrong.
Controller
public function create_cargo_manifest(){
$core_model = new Core_m;
$core_model->save_cargo_details($this->input->post());
redirect('core/cargo_lookup/');
}
Model
function save_cargo_details(){
$quantity = $this->input->post('quantity');
$waybilldate = $this->input->post('waybilldate');
$data = array();
foreach($this->input->post('sys_wbdetails') as $sys_wbdetails) {
$data[] = array(
'waybillno' => $sys_wbdetails,
'quantity' => $quantity,
'waybilldate' => $waybilldate,
);
}
return $this->db->insert_batch('sys_cargodetails', $data);
}
View
<?php foreach($waybill_header as $waybill_header) { ?>
<?php echo form_open('core/create_cargo_manifest'); ?>
<td><input type="checkbox" name="sys_wbdetails[]" value="<?php echo $waybill_header->waybillno; ?>"></td>
<td><?php echo $waybill_header->waybillno; ?></td>
<td><?php echo $waybill_header->waybilldate; ?><input type="hidden" value="<?php echo $waybill_header->waybilldate; ?>" name="waybilldate"></td>
<td><input type="text" size="5" value="<?php echo $waybill_header->quantity; ?>" name="quantity"></td>
<td><input type="submit" value="save"></td>
<?php } ?>
<?php form_close(); ?>
All your inputs for quantity have the same name : quantity. So you're only submitting the last value in your form. You need to use an array for those inputs (quantity[]), just like for your checkboxes. And you might want to do the same for the waybilldate inputs.
<td><input type="text" size="5" value="<?php echo $waybill_header->quantity; ?>" name="quantity[]"></td>
And then in PHP, something like that :
$data = array();
// Count distinct entries in the form
$count = count($this->input->post['sys_wbdetails']);
for($i=0; $i < $count; $i++) {
$data[] = array(
'waybillno' => $this->input->post['sys_wbdetails'][$i],
'quantity' => $this->input->post['quantity'][$i],
'waybilldate' => $this->input->post['waybilldate'][$i],
);
}
EDIT : also, take a look at this answer if you want a clean way to keep track of which form input goes where.

Categories