How to add multiple cart items in database using codeigniter - php

I want to insert cart items into database but when I insert data it will insert just a single product so how to insert multiple products at once

Use $this->db->insert_batch();
Example
Your record data should be array format
$data = array(
array(
'title' => 'My title',
'name' => 'My Name',
'date' => 'My date'
),
array(
'title' => 'Another title',
'name' => 'Another Name',
'date' => 'Another date'
)
);
Now pass the set of data to the following function
$this->db->insert_batch('table_name', $data );
Simple use following function for your case, it will help you. add this function to your model file
public function insertAllQuery($table, $valueArray){
$this->db->insert_batch($table, $valueArray);
return ($this->db->trans_status()) ? true : false;
}
pass the card items in a controller as like below
$this->your_model->insertAllQuery('table_name', $data );

Related

How can I store request array without using foreach in Laravel?

I have an array in the post request as an example below:
$data = array(
array('title'=>'1st title', 'desc'=>'desc'),
array('title'=>'2nd title', 'desc'=>'desc'),
array('title'=>'3rd title', 'desc'=>'desc'),
)
Is there a way in Laravel using Eloquent I can save above data without using foreach? Note that the array keys which I am getting in the request is not same as column names of the table.
I hope this would help you
$data = [
['title' => '1st title', 'desc' => 'desc'],
['title' => '2nd title', 'desc' => 'desc']
.....
];
DB::table('users')->insert($data);
Put all the values you want to insert in to an array and then pass it to the insert function.
Source: https://laravel.com/docs/5.1/queries#inserts
try this:
DB::table('table_name')->insert($data);
Using eloquent: just as mentioned by Sethu, but a few lines will be:
Model::insert($data); // eg: Posts::insert($your_request_array);
Just pass in the array directly here: above will return true on success.

Best way to insert large record to the database table imported from csv

I have the following code written in codeigniter.
foreach ($data as $value) {
# item_transactions fields and values
$data = array (
'checklist_item_id' => ($this->set['table'] == 'checklist_item') ? $this->set['id'] : null,
'imp_sequence_no' => $value['Sequence No.'],
'imp_vendor_tin' => $value['Vendor TIN'],
'imp_vendor_name' => $value['Vendor Name'],
'imp_input_vat_per_client' => floatval(str_replace(',', '', trim($value['Input VAT per client']))),
'imp_gsi' => $value['Goods/Services/Importations']
);
# Insert to item_transactions table
$id = parent::insert($data);
$count++;
}
return $count;
What this code do, is to insert a thousands of records to a database table, from the csv file imported.
$data is an associative array of fields and values to be inserted.
The problem
When I tried to import a csv file containing more than 4,000 records, I got a PHP error saying Maximum execution time of 30 seconds exceeded.
I don't want to change the php.ini setting, because mostly you don't have control on the live servers settings.
Does anyone can suggest best way to solve this problem?
Codeigniter has a query builder class that supports batch upload. You can either pass an array or an object to the function. Here is an example using an array:
$data = array(
array(
'title' => 'My title',
'name' => 'My Name',
'date' => 'My date'
),
array(
'title' => 'Another title',
'name' => 'Another Name',
'date' => 'Another date'
)
);
$this->db->insert_batch('mytable', $data);
Producess:
INSERT INTO mytable (title, name, date) VALUES ('My title', 'My name', 'My date'), ('Another title', 'Another name', 'Another date')
Change your php.ini file settings. max_execution_time = 300 will be enough. Restart your server.

How to insert form fields in mysql using codeigniter

I am new to php codeigniter, Please any one tell me how to insert form fields in mysql using codeigniter. Thanks in advance.
Well, study the codeigniter Active Record, you will find all the ways to insert data into the database, including your form fields as well. It is available on codeigniters website as well, codeigniter is a well documented php framework, so you will find lots of help out there.
https://ellislab.com/codeigniter/user-guide/database/active_record.html
Here you will find all the guide.
$data = array(
'title' => 'My title' ,
'name' => 'My Name' ,
'date' => 'My date'
);
$this->db->insert('mytable', $data);
// Produces: INSERT INTO mytable (title, name, date) VALUES ('My title', 'My name', 'My date')
controller
$data = array(
'title' => 'My title' ,
'name' => 'My Name' ,
'date' => 'My date'
);
$this->modelname->form_insert($data);
model
function form_insert($data){
$this->db->insert('tablename', $data);}
view
<?php echo form_label(' Name :'); ?>
<?php echo form_error('Name'); ?>
<?php echo form_input(array('id' => 'id', 'name' =>'Name')); ?>

Error in Programmatically creating Taxonomy in drupal

I'm getting the below error while building taxonomy voccabulary and terms using the following code
FieldException: Attempt to create an instance of a field field_my_custom_vocab002 that doesn't exist or is currently inactive. in field_create_instance() (line 476 of C:\wamp\www\pur_theme\modules\field\field.crud.inc).
I checked the voccabulary and it is created, the problem is only with term creation
code
<?php
$new_vocab = (object) array(
'name' => 'My custom vocabulary002',
'description' => 'Test',
'machine_name' => 'my_custom_vocab002',
);
taxonomy_vocabulary_save($new_vocab);
$vocab = taxonomy_vocabulary_machine_name_load('my_custom_vocab002');
$term1 = (object) array(
'name' => 'Term 1',
'description' => 'This is term 1',
'vid' => $vocab->vid,
);
taxonomy_term_save($term1);
What/where did I wrong?
I think it's because you try to create a term with more fields than the 2 basical ones (name and vid). You have a "description" field in addition.
Try this code instead:
$term = new stdClass();
$term->name = 'Term 1';
$term->vid = $vocab->vid;
$term->field_description[LANGUAGE_NONE][0]['value'] = 'This is term 1';
taxonomy_term_save($term);
Source: http://www.lightrains.com/blog/programmatically-create-taxonomy-term-drupal
I found the only solution to this was to create the field it's looking for before creating the vocabulary. Personally I did this with a custom function:
function MY_MODULE_create_new_taxonomy($taxonomy_name, $taxonomy_machine_name, $taxonomy_description){
//Add field you know is going to cause trouble
$field = array(
'field_name' => 'field_'.$taxonomy_machine_name,
'type' => 'text',
'label' => 'Label'
);
field_create_field($field);
//create the vocab
$new_vocabulary = new stdClass();
$new_vocabulary->name = $taxonomy_name;
$new_vocabulary->machine_name = $taxonomy_machine_name;
$new_vocabulary->description = t($taxonomy_description);
$new_vocabulary->module = 'taxonomy';
taxonomy_vocabulary_save($new_vocabulary);
}

Codeigniter Active Record multi-update

How can I update multiple rows using active record?
My array
foreach($services as $service){
$this->service_batch[]=array(
'service_id'=>$service->service_id,
'service_count'=>$service->service_count,
'id_customer'=>$service->id_customer,
'id_section'=>$service->id_section);
}
When I update the table, I need define 2 parameters in the WHERE clause (id_customer AND id_section), but if I use update_batch, I can specify only one parameter.
How can I set a few parameters in the WHERE clause?
something like this from the Code Igniter Documentation - See Update Batch :-
$data = array(
array(
'title' => 'My title' ,
'name' => 'My Name 2' ,
'date' => 'My date 2'
),
array(
'title' => 'Another title' ,
'name' => 'Another Name 2' ,
'date' => 'Another date 2'
)
);
$this->db->where($this->db->where('id', $id);
$this->db->update_batch('mytable', $data, 'title');
in your case data will be replaced by $this->service_batch and title by your desired column.
Not quite sure of catching what you exactly needs, but i would do:
//controller
foreach($services as $service){
$this->model->update(
$service->service_id,
array(
'service_count'=>$service->service_count,
'id_customer'=>$service->id_customer,
'id_section'=>$service->id_section
));
}
//model
function update($id,$data){
$this->db->where('id',$id);
return $this->db->update('my_table',$data);
}

Categories