How do I update multiple inputs with the same name in codeigniter? - php

This is what I have so far
Controller
$i = 0;
foreach ($this->input->post('skill') as $cat) {
$data[$i++]['skill'] = $cat;
}
$this->db->update_batch('skills', $data);
}
Model
function update_record($data)
{
$this->db->update('skills', $data);
}
View
<?php foreach ($skills as $skill):?>
<input type="text" name="skill[]" value="<?php echo $skill->skill;?>">
<?php endforeach?>
I am getting a database error
You must specify an index to match on for batch updates.
Please help me fix this I tried googling and nothing is coming up.

Ok, here is the issue.
According to the CI Docs for update_batch you need to add a third parameter that is the where key . Here is an example from the docs.
$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->update_batch('mytable', $data, 'title');
So, above in the queries the title column is what the row is compared to. So for the rows that have title as 'My Title' The first array element is used as update and for those with 'Another Title' the second element. I hope you understand. If we don't have a compare field then the whole database will be updated :-P
Here is the final query produced from the above operation.
UPDATE `mytable` SET `name` = CASE
WHEN `title` = 'My title' THEN 'My Name 2'
WHEN `title` = 'Another title' THEN 'Another Name 2'
ELSE `name` END,
`date` = CASE
WHEN `title` = 'My title' THEN 'My date 2'
WHEN `title` = 'Another title' THEN 'Another date 2'
ELSE `date` END
WHERE `title` IN ('My title','Another title')
Hope this helps.

Related

Get max value from table

I am applying this query:
$this->db->select_max('product_id');
$this->db->from('products');
$query=$this->db->get();
return $query->result();
to get max value from table.
But when i add this result to db in another table. like this:
$this->db->insert('images',$product_id);
It show this error:
Error Number: 1054
Unknown column 'Array' in 'field list'
INSERT INTO `images` (`product_id`) VALUES (Array)
Filename: C:\wamp\www\system\database\DB_driver.php
Line Number: 330
I am using codeigniter.
You are applying an array within input here's I have updated your code
Model Code
$this->db->select_max('product_id');
$this->db->from('products');
$query=$this->db->get();
return $query->result_array();
Controller Code
$product_id = $this->Product_area_model->get_product_id();
$data['product_id'] = $product_id[0]['product_id'];
$result = $this->your_model->foo($data);
Model Code
function foo($data = ''){
$this->db->insert('images',$data);
return $this->db->insert_id();
}
Hope it will work for you...
$this->db->insert('images',$product_id[0]['product_id']);
Codeigniter allows you to specify an array of values as the data to insert, but I think your issue is that you're inserting an array of arrays.
If that's the case then use the batch function...
$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);
// Produces: INSERT INTO mytable (title, name, date) VALUES ('My title', 'My name', 'My date'), ('Another title', 'Another name', 'Another date')

mysql insert batch vs. insert loop (codeigniter)

kindly enlightened me, which is faster/better approach or just the same between CI batch insert and loop insert.
$data = array(
array(
'title' => 'My title' ,
'name' => 'My Name' ,
'date' => 'My date'
),
array(
'title' => 'Another title' ,
'name' => 'Another Name' ,
'date' => 'Another date'
)
);
batch insert:
$this->db->insert_batch('mytable', $data);
/* produces: INSERT INTO mytable (title, name, date)
VALUES ('My title', 'My name', 'My date'),
('Another title', 'Another name', 'Another date'); */
loop insert (php):
for( $i = 0; $ < count($data); $i++ )
{
INSERT INTO mytable (title, name, date)
VALUES ($data[$i]['title'], $data[$i]['name'], $data[$i]['date'])
}
thanks!
Batch inserts are usually faster since they process the data in once, were as the INSERT has some overhead (eg, the SQL optimizer cannot deduct certain steps). That said, you need to process a tremendous number of rows to create a notable difference.
If your curious if it would matter anyway, then don't forget to also measure the time it costs the framework to map the classes to the database table(s). There's a good chance the ORM consumes more resources than a looped SQL INSERT.

Creating a dropdown menu from an array and prepend the array

I have an array which I will use for a dropdown in a form. My array is something like this...
$data = ('1' => 'Option 1', '2' => 'Option 2', '3' => 'Options 3')
Now I want to prepend this to the $data array
'' => 'Please select'
How do I do this? I've tried array_unshift but this adds a key to my option which I don't want because of my form validation.
Anyone help?
Thanks
All array values have a key. array_shift is the best way to do this.
You can use union operator
$data = ('1' => 'Option 1', '2' => 'Option 2', '3' => 'Options 3')
$prependArray = array('' => 'Please select');
$result = $prependArray + $data;

Add a multidimension PHP Array at the end of each item of a multidimension array

Here are the two Multi-dimension arrays explained.
// Categories Array
$categories = array(
array('cat_id'=>'1', 'cat_name' => 'Category One', 'cat_data' => 'Some Data'),
array('cat_id'=>'2', 'cat_name' => 'Category Two', 'cat_data' => 'Some Data'),
array('cat_id'=>'3', 'cat_name' => 'Category Tree', 'cat_data' => 'Some Data')
);
// Products Array (One $products array is to be placed inside every new category.
$products = array(
array('p_id'=>'1', 'p_name'=>'Product One'),
array('p_id'=>'2', 'p_name'=>'Product Two'),
array('p_id'=>'3', 'p_name'=>'Product Three')
);
Here The $products needs to be placed inside every element of $category array, with a key of some random key take for eg 'product_list'.
Here is the result like
$category = array(
array('cat_id'=>'1', 'cat_name' => 'Category One', 'cat_data' => 'Some Data', 'product_list'=>array()),
array('cat_id'=>'2', 'cat_name' => 'Category Two', 'cat_data' => 'Some Data', 'product_list'=>array())
);
Please scroll Right for the above code to see the last element added to these elements.
Please tell how to add that multi-dimension array with a key to each n every element of the $category array. Thanks
What is the problem?
foreach ($categories as &$category) {
$category['product_list'] = $products;
}
Try to use this code.
foreach($categories as $key=>$value)
{
$categories[$key]['product_list'] = $products;
}
After trying several attemps, I solved this problem by myself by just a simple code. here it is.
$categories['product_list'] = $products;
Hope, users finding this type of problem this useful. Thanks

How to include a random array with 2 variable

My config.php contains:
$a1="title";
$b1="text";
$a2="title2";
$b2="text2";
and I have 1.php which includes config.php.
I need to include ($a1 and $b1) or ($a and $b) randomly.
How can I do this? :)
Thank you.
If this data is related, and you need a random set, store it in an array:
$sets = array(
array(
'title' => 'Some title',
'text' => 'Some text here about the title'
),
array(
'title' => 'Some other title',
'text' => 'Some other text here about the title'
)
);
With this array, we have two indexes that we can choose from:
$sets[0]; // Title: Some title, Text: Some text here about the title
$sets[1]; // Title: Some other title, Text: Some other text here about the title
If we want one of those, randomly, we could do the following:
$index = array_rand( $sets, 1 );
This will select either 0, or 1. With more entries in our array, the potential for this number to be larger also increases. We can then use it to grab one of our sets of data:
$dataSet = $sets[ $index ];
And then display the output:
echo $dataSet['title'];
echo $dataSet['text'];

Categories