Unable to update database query in Codeigniter - php

I have to fetch the record from the form of an array. following is an array result.
Array(
[price] => Array
(
[0] => 210
)
[code] => Array
(
[0] => SER-1001
)
)
Array
(
[price] => Array
(
[1] => 80
)
[code] => Array
(
[1] => XYZ-121
)
)
Now I am confused, how to update that records in product table where a code is e.g. SER-1001 in the database then the price should be updated 210.
Same way as I do rest of matching code records.

foreach ($arr as $key => $value) {
$data = array(
'price'=> $value['price'];
);
$this->db->where('code', $value['code']);
$this->db->update(‘database-table-name’,$data);
}
try this, here you are looping through the array and updating the data one by one.

$array = [
[
'price' => [ '0' => 210],
'code' =>['0' => 'SER-1001']
],
[
'price' => ['1' => 80],
'code' =>['1' => 'XYZ-121']
]
];
With reference to the above output array. You can write the code something like this.
$counter = 0;
foreach($array as $data)
{
if(isset($data['code'][$counter]))
{
$code = $data['code'][$counter];
$data = [
'price' => $data['price'][$counter];
];
$this->db->where($column, $code);
$this->db->update('database-table-name',$data);
}
$counter++;
}

Related

Fill an multidimensional array with a foreach

I need to make an array like this
$preferenceData = [
'items' => [
[
'id' => 1,
'title' => 'Recarga de Créditos',
'description' => 'Recarga de Créditos para Negocioson.com',
'quantity' => 1,
'currency_id' => 'ARS',
'unit_price' => $amount
]
]
];
But I need to create it with a foreach
foreach ($items as $item) {
}
I tried this:
$data[0]['items'][0]['id'] = 1;
BUT the results are not the same:
This is mine:
Array ( [0] => Array ( [items] => Array ( [0] => Array ( [id] => 1 ) ) ) )
This is the preference one:
Array ( [items] => Array ( [0] => Array ( [id] => 1 ) ) )
I add a [0] how can I delete it?
Thanks
You shouldn't use $data[0]. The top-level array is an associative array, not numeric.
$data['items'][0] = [
'id' => 1,
'title' => 'Recarga de Créditos',
'description' => 'Recarga de Créditos para Negocioson.com',
'quantity' => 1,
'currency_id' => 'ARS',
'unit_price' => $amount
];
You can do something like:
$data = [];
foreach ($items as $item) {
$data['items'][] = [
'id' => $item['id'],
'title' => $item['title'],
...
];
}

Making Terms Query in Elastic Search

I need to search multiple ids in the Elastic search. same like IN in SQL.
When I write static multiple IDs, it works but when I make IDs array and then implode to make comma-separated IDs for ES. It does not work.
Implode query:
$ids = array();
foreach ($this->session->userdata('cart') as $key => $value) {
$ids[] = trim($key);
}
$params = [
'index' => ES_INDEX_PD,
'body' => [
'query' => [
'constant_score' => [
'filter' => [
'terms' => [
'id' => [implode(",", $ids)]
]
],
]
]
]
];
$products = $this->elasticsearch->client->search($params);
This is from imploding result... Does not work
Array ( [index] => example-prod [body] => Array ( [query] => Array ( [constant_score] => Array ( [filter] => Array ( [terms] => Array ( [id] => Array ( [0] => 10241308,10928958 ) ) ) ) ) ) )
This is static IDs passed to query. This works
Array ( [index] => example-prod [body] => Array ( [query] => Array ( [constant_score] => Array ( [filter] => Array ( [terms] => Array ( [id] => Array ( [0] => 10241308 [1] => 10928958 ) ) ) ) ) ) )
No need to use extra implode() with , here when you pass ids. I had the same issue couple of months back.Just pass the array of ids like this to your terms query and it'll work perfectly.
$ids = array();
foreach ($this->session->userdata('cart') as $key => $value) {
$ids[] = trim($key);
}
$params = [
'index' => ES_INDEX_PD,
'body' => [
'query' => [
'constant_score' => [
'filter' => [
'terms' => [
'id' => $ids
]
],
]
]
]
];
$products = $this->elasticsearch->client->search($params);

PHP Array of arrays of arrays

I am trying to search this array by the value of ['field'] and then return the value of ['value'] associated with that field in the array.
For example, I want to say "Tell me the value associated with theThirdField".
I've tried many, many permutations of something like the following:
$myVariable = $Array['result']['totalrows'][0]['rownum'][0]['field'];
To further clarify, I will never know which order / sub-array my search field is located in, I only know the string value associated with ['field'].
How would I accomplish this?
Array
(
[result] => Array
(
[totalrows] => 1
[rows] => Array
(
[0] => Array
(
[rownum] => 1
[values] => Array
(
[0] => Array
(
[field] => testMeOnce
[value] => 436586498
)
[1] => Array
(
[field] => testMeTwice
[value] => 327698034
)
[2] => Array
(
[field] => theThirdField
[value] => 108760374
)
[3] => Array
(
[field] => theFourthField
[value] => 2458505
)
[4] => Array
(
[field] => fifthField
[value] => -0.0201
)
)
)
)
)
)
Did you expect something like that?
$needle = 'theThirdField'; // searched field name
$valuesArray = $Array['result']['rows'][0]['values']; //now you have clearer array
array_walk($valuesArray, function($element, $key) use ($needle) {
if ($element['field'] == $needle) {
echo $element['value'];
}
});
I would do something like this, assuming you want to search only in the $myVariable dimension :
$myVariable = $Array['result']['rows'][0]['values'];
foreach ($myVariable as $key => $value) {
if( $value['field'] === 'theThirdField' ) {
echo $value['value'];
break;
}
}
when I ran your code, I got some syntax errors. I have changed your Array to this (changed syntax only):
$a = Array
(
'result' =>
[
'totalrows' => 1,
'rows' =>
[
0 =>
[
'rownum' => 1,
'values' =>
[
0 =>
[
'field' => 'testMeOnce',
'value' => 436586498
],
1 =>
[
'field' => 'testMeTwice',
'value' => 327698034
],
2 =>
[
'field' => 'theThirdField',
'value' => 108760374
],
3 =>
[
'field' => 'theFourthField',
'value' => 2458505
],
4 =>
[
'field' => 'fifthField',
'value' => -0.0201
]
]
]
]
]
);
now you can get the value like this:
print($a['result']['rows'][0]['values'][2]['value']); // --> 108760374
I hope that this is what you are looking for!
Thanks everyone. I used a mix of your suggestions and ended up doing the following:
$valuesArray = $Array['result']['rows'][0]['values'];
foreach ($valuesArray as $value) {
$fieldName = $value['field'];
$$fieldName = $value['value'];
}
Since I know the names of all the fields I want, but not the order, this allowed me to capture all of the values of each field with the name of the field becoming the variable name.

How to separate the array?

The following array is coming on $output variable,
Array
(
[title] => Subtotal
[data] => $1,104.05
)
Array
(
[title] => Total Shipping
[data] => $56.45
)
Array
(
[title] => Order total
[data] => $1,160.50
)
Array
(
[border] => top
[title] => Paying by
[data] => PayPal
)
I want to separate the following array from the above array and want to store it on new variable.
Note:Arrays are coming dynamic.
Array
(
[title] => Order total
[data] => $1,160.50
)
Please help!!
Try this,
$array = array(
Array
(
[title] => Subtotal
[data] => $1,104.05
)
Array
(
[title] => Total Shipping
[data] => $56.45
)
Array
(
[title] => Order total
[data] => $1,160.50
)
Array
(
[border] => top
[title] => Paying by
[data] => PayPal
)
);
$second_array = array();
foreach($array as $key => $arr){
if(isset($arr['title']) && $arr['title'] === 'Order total'){
$second_array = $arr;
unset($array[$key]);
}
}
This code will check title key is set or not and it's value
$array = [
[
'title' => 'Subtotal',
'data' => '$1,104.05'
],
[
'title' => 'Total Shipping',
'data' => '$56.45'
],
[
'title' => 'Order total',
'data' => '$1,160.50'
],
[
'border' => 'top',
'title' => 'Paying by',
'data' => 'PayPal'
]
];
for($i = 0; $i<=count($array); $i){
if(isset($array[$i]['title']) && $arr[$i]['title'] === 'Order total'){
$newarr = $arr;
unset($array[$key]);
}
}
You should use array_filter().
$array = [
[
'title' => 'Subtotal',
'data' => '$1,104.05'
],
[
'title' => 'Total Shipping',
'data' => '$56.45'
],
[
'title' => 'Order total',
'data' => '$1,160.50'
],
[
'border' => 'top',
'title' => 'Paying by',
'data' => 'PayPal'
]
];
$array = array_filter($array, function($ar) {
return ($ar['title'] == 'Order total'); //check condition and return record with key
});
$newArray = []; // your new array
$key = key($array); // get key of result array
$newArray = $array[$key]; // assign result record to new array
echo "<pre>"; print_r($newArray);
output:-
Array
(
[title] => Order total
[data] => $1,160.50
)
Hope it will help you :-)

Add array elements to associative array

I am unable to find a way to take the elements of an array (product IDs) and add them to a particular key of an associative array ($choices['id]) so that it creates as many instances of that array ($choices[ ]) as there are elements in $id.
I want the final version of $choices[ ] to include as many arrays as there are elements in $id[ ].
After that I want to repeat this process for part_numbers & quantity.
// Create $choices array with keys only
$choices = array(
'id' => '',
'part_number' => '',
'quantity' => '',
);
// Insert $id array values into 'id' key of $choices[]
$id = array('181', '33', '34');
If I'm understanding your question correctly, you mean something like this?
$choices = array();
$id = array('181', '33', '34');
foreach($id as $element)
{
$choices[] = array(
'id' => $element,
'part_number' => '',
'quantity' => '',
);
}
echo "<pre>";
print_r($choices);
echo "</pre>";
Output:
Array (
[0] => Array
(
[id] => 181
[part_number] =>
[quantity] =>
)
[1] => Array
(
[id] => 33
[part_number] =>
[quantity] =>
)
[2] => Array
(
[id] => 34
[part_number] =>
[quantity] =>
)
)
Edit:
A more general solution would be as follows:
$choices = array();
$values = array('sku-123', 'sku-132', 'sku-1323');
foreach($values as $i => $value){
if(array_key_exists($i, $choices))
{
$choices[$i]['part_number'] = $value;
}
else
{
$choices[] = array(
'id' => '',
'part_number' => $value,
'quantity' => '',
);
}
}
This can be used for array creation and insertion, hence the if/else block.

Categories