Updating multiple rows in one form submission Codeigniter - php

Searched, Googled already couldn't found one with same case,
Basically i have a set of multiple categories in one form, i want to update Number of questions in each category on one form submit.
Following is the form:
Number of categories can be dynamic, each Question TextBox contains its name = "question" merged with category ID and make it as "question12 , question13" etc.
I know about update_batch() , but how do i get values and put them in array as they can be of unknown number.
How do i update all categories at once in CodeIgniter

$post = $this->input->post();
foreach($post as $key=>$value){
if(strpos($key, "question") == 0){
$category_id = substr($key, 8);
//Use $category_id and $value in this loop to build update statement
}
}

I have resolved this using foreach in Controller
foreach ($sample_settings as $sample) {
$array['category_id'] = $sample['category_id'];
$array['no_of_questions'] = $this->input->post('question'.$sample['category_id']);
$batch_array[] = $array;
}

Related

Nested loops for dynamics arrays

I have some results from a web form.
I'm working with WP and I get a lot of term_id (from different taxonomy).
In the frontend the form is "dynamic" for each kind of taxonomy (and i dont know before how many they are), I made a multiselect (all select have same NAME ex: terms[] ).
When I process the term_IDs after form Submit I check each term_Id and put it in the right array. EX:
$option = isset($_POST['terms']) ? $_POST['terms'] : false;
if ($option) {
$sql = "SELECT term_id,taxonomy FROM wp_term_taxonomy WHERE term_id IN (".implode(',',$option).") ";
$term_tax = $wpdb->get_results($sql);//print_r($option); exit();
foreach ($term_tax as $tt)
{
$global[$tt->taxonomy][] = $tt->term_id;
}
So in the end I have an array with 1 or N arrays.
For each sub-array i have to do a nested cycle
EX: after process terms i have $gloabl[category] and $global[tag] I need to do
foreach($global[category] as $t)
{
foreach($global[tag] as $x)
{
//do query with $t and $x
}
}
I need to do a query with all combinations of term_ids for each subarray dynamically.

insert the last inserted record id into an array in another table using Codeigniter

so I got two tables one called patients and another called tests , the tests table has the patient id , i have a page called add patient which have fields for adding new patient information and other fields for adding their test info and upload all the data into the two tables in one query ,tests fields could be duplicated by ajax to i could add more than one test to the same patient , now i wanna add more than one test at the same time into the tests table and i managed to do so but the case is i can't add the patient_id into tests table, i wanna add the same patient_id more than one time to all the tests i have added while adding a new patient in that page, i'm new to Codeigniter!
this is the adding page
the patient fields and the test fields
<input type="text" name="patientname" />
<input type="text" name="address" />
<input type="text" name="testname[]"/>
<input type="text" name="price[]" />
and this is my controller
public function testbby
{
$this->load->model("khmodel", "Khmodel");
// patient main info
$patient_input_data = array();
$patient_input_data['patientname'] = $this->input->post('patientname');
$patient_input_data['address'] = $this->input->post('address');
//test data
$testname = $this->input->post('testname[]');
$price = $this->input->post('price[]');
$test_input_data = array();
for ($i = 0; $i < count($testname); $i ++ )
{
$test_input_data[$i] = array(
'testname' => $testname[$i],
'price' => $price[$i],
);
}
$this->Khmodel->insert_bby($patient_input_data, $test_input_data);
redirect('main/dashboard');
}
and this is my model
public function insert_bby($patient, $test)
{
$this->db->insert('patients', $patient);
$patient_id = $this->db->insert_id();
// i used this and it worked but only while adding one test , s
//once it's gonna be an array i dunno what to do with the id !
//$test['refpatient']=$patient_id;
$this->db->insert_batch('tests', $test);
return $insert_id = $this->db->insert_id();
}
To start with, you don't need this.
$patient_input_data = array();
Because when you make the call
$patient_input_data['patientname'] = $this->input->post('patientname');
The array, $patient_input_data, will be created. There are times when you might want to make sure you have an array even if it's empty. But this isn't one of them.
For the array input values, ie testname[], get the data by leaving off the brackets at the end of the name. Like this.
//test data
$testname = $this->input->post('testname'); //instead of post('testname[]')
$price = $this->input->post('price');
The vars $testname and $price will be arrays with an item for each field on the form.
Seems to me that those two inputs are required so you should add code to check that is the case. The Form Validation class is excellent for that purpose.
The array $test_input_data is a case where you will want the array to exist - even if it's empty. You don't have to explicitly set the index value when adding items to the array, i.e. $test_input_data[$i] = array(... because $test_input_data[] = array(... will work just fine, but there's no harm either way.
On to the model. The first part is good. For the second you need to create arrays that include the patient id you got from the first insert and add that value to each of the sub-arrays in the $tests argument. The model then becomes this.
public function insert_bby($patient, $tests)
{
$this->db->insert('patients', $patient);
$patient_id = $this->db->insert_id();
// add the patient id key/value to each sub-array in $tests
foreach ($tests as $test)
{
$test['patient id'] = $patient_id;
}
// will return the number of rows inserted or FALSE on failure
return $this->db->insert_batch('tests', $tests);
}
the value i mean , i dont know but your code seems to be so right and logical
but i have tried this code and it worked so well , i didn't even use the model/
public function testbby
{
$this->load->model("khmodel", "Khmodel");
// patient main info
$patient_input_data = array();
$patient_input_data['patientname'] = $this->input->post('patientname');
$patient_input_data['address'] = $this->input->post('address');
//test data
$testname = $this->input->post('testname[]');
$price = $this->input->post('price[]');
$this->db->reset_query();
$this->db->insert('patients', $patient_input_data);
$patient_id=$this->db->insert_id();
$test_input_data = array();
for ($i = 0; $i < count($testname); $i ++ )
{
$test_input_data[] = array(
'testname' => $testname[$i],
'price' => $price[$i],
'patient_id'=>$patient_id
);
}
$this->db->reset_query();
$this->db->insert_batch('tbl_tests',$test_input_data);
redirect('main/dashboard');
}

I am using php with codeigniter framework and i am using filter in my function where user can input 1 filter or user can input 4 inputs(max. 5)

In my table there are 5 rows and i want to get data from the table on the basis of inputs .the rows are name,age,height,weight and class.
If user inputs age= 12 and weight=40 then person with 12 age as well as 40 weight should be shown.
if user inputs only name=jack then persons with name jack should be shown.
if user inputs all five entries then all entries should be matching.
user can input one field or all five fields.
I suggest to use the inputs to build an associative array which contains the filters you want to apply.
$filters = array();
if (!empty($this->input->post('name')))
$filters['name'] = $this->input->post('name');
if (!empty($this->input->post('age')))
$filters['age'] = $this->input->post('age');
if (!empty($this->input->post('height')))
$filters['height'] = $this->input->post('height');
if (!empty($this->input->post('weight')))
$filters['weight'] = $this->input->post('weight');
if (!empty($this->input->post('class')))
$filters['class'] = $this->input->post('class');
Then, when you are building your query, you just need to iterate over the array:
foreach ($filters as $key => $value) {
$this->db->where($key, $value);
}
Note the use of empty() instead of isset or any manual comparison. Docs reference.

Search filter in cakephp with dynamic options

I want to apply search filters in my project. I have options tables where options are being saved with the option values with parent id of option id. For example brand is saving as option with parent id set to 0 and all brands have brand id as their parent id set and while saving product I am saving product options in product_options table. Now i want to apply filters in product listing page. I am using following code for filtration:
$conditions = array();
$product_options = $this->ProductOption->find('list',array('fields'=>array('product_id'),'conditions'=>array('ProductOption.option_value_id'=>$data['data']['options'])));
$conditions = array_merge($conditions,array('Product.id'=>array_unique($product_options)));
$prod_info = $this->paginate('Product',$conditions);
$this->set(compact('prod_info'));
When I search any product with their brand name it works fine but if I try to search with the price (also an option) then it gives other brand products also which have price equal to filter price option. Please check following link to understand problem correctly.
http://primemart.in/Food-Processors-Ii4zRGAKYAo=
Please anyone help me to come out my problem.
Thanks.
Please have a look on my code which I used to pass conditions in and to get results
$product_options = $this->ProductOption->find('list',array(
'fields'=>array('product_id'),
'conditions'=>array('ProductOption.option_value_id'=>$data['data']['options'])
));
//$this->Option->unBindModel(array('belongsTo'=>'Product'));
$product_options = $this->Option->find('all', array(
'conditions'=>array('Option.id'=>$data['data']['options'])
));
//pr($product_options);
$opt_arr = array();
foreach ($product_options as $op) {
$opt_arr[$op['Option']['parent_id']][] = $op['Option']['id'];
}
$conditions_arr = array();
foreach($opt_arr as $opt) {
$key_arr = array();
foreach($opt as $op) {
$key_arr['OR']['ProductOption.option_value_id'][] = $op;
}
$conditions_arr['AND'][] = $key_arr;
}
$pr_options = $this->ProductOption->find('list', array(
'conditions'=>$conditions_arr,
'fields'=>array('product_id')
));
$conditions = array_merge($conditions, array('Product.id'=>array_unique($pr_options)));
I would try code bellow. I assume that $conditions constist of the other conditions you mention in your question.
$conditions = ... // other conditions you mentioned
$conditions = array('AND'=>array($conditions, array('Product.id'=>array_unique($product_options))));
$prod_info = $this->paginate('Product',$conditions);

Drupal 7 Delete field collection for node

I just found a script to programatically delete a field collection to a specific node :
<?php
$node = node_load(1);
$field_collection_item_value = $node->field_page_collection1[LANGUAGE_NONE][0]['value']; // Take field collection item value.
entity_delete_multiple('field_collection_item', array($field_collection_item_value)); // Delete field collection item.
?>
Unfortunately as i see it , it only delete first field collection, I need to select which one i want to delete.
Here is my structure :
Multiple field collection who have : a reference to another node and two selects
I have the reference nid in the url so I can use it, but I don't have any idea how to select the right field collection with that.
Thanks
Try to use this:
$node = node_load(1);
$searhed_nid = '2';
$field_page_collection1 = field_get_items('node', $node, 'field_page_collection1');
foreach ($field_page_collection1 as $item) {
$field_collection = entity_load_single('field_collection_item', $item['value']);
$fc_item_wrapper = entity_metadata_wrapper('field_collection_item', $field_collection);
// lets take name the field with ref field_ref_nid.
$field_val = $fc_item_wrapper->field_ref_nid->raw();
if ($field_val == $searhed_nid) {
$field_collection->delete();
}
}

Categories