The reason why Cannot access empty property ? - codeigniter

I'm new to this...help me please
$data is object
stdClass Object ( [menu_id] => 38 [menu_code] => M062 [menu_name] => BAP (RICE) [price] => Rp 6.364 [total] => 1
and $fields is array, this is not all..
Array ( [0] => Array ( [code] => menu_id [title] => ID [width] => 5 ) [1] => Array ( [code] => menu_code [title] => Kode [width] => 8 ) [2] =>
and this is my function :
function writeRowAsli($row, $startChar, $fields, $data){
$i=$startChar; $j=''; $k='';
foreach($fields as $field){
$k = $j.$i;
$this->excel->getActiveSheet()->setCellValue($k.$row, $data->$field['code']);
$last = $k;
if($i == 'Z'){
$i='A';
$j.=$i;
} else $i++;
}
$this->excel->getActiveSheet()->setCellValue($j.$i.$row, '=SUM(C'.$row.':'.$k.$row.')');
}
i know the bad line is $this->excel->getActiveSheet()->setCellValue($k.$row, $data->$field['code']);
thanks all

Change your function as below and check if its work or not:
function writeRowAsli($row, $startChar, $fields, $data){
$i=$startChar; $j=''; $k='';
$fields = array_filter($fields);
foreach($fields as $field){
$k = $j.$i;
$this->excel->getActiveSheet()->setCellValue($k.$row, $data->$field['code']);
$last = $k;
if($i == 'Z'){
$i='A';
$j.=$i;
} else $i++;
}
$this->excel->getActiveSheet()->setCellValue($j.$i.$row, '=SUM(C'.$row.':'.$k.$row.')');
}

Related

Laravel - How to update table multiple rows at once

I have this variable called $projectFieldOptions and it's output is like this:
https://prnt.sc/7HtxrfTy9HiI.
Now, In the Controller I need to update this. What I am doing this, first delete all the existing rows based on id_feed and id_project and then loop through this variable $projectFieldOptions and insert it. Like this:
if( $request->feed_type !== 'scrape' ) {
$delete_mapping = DB::connection($db_name)->table($db_name . '.feed_mappings')
->where('id_feed', '=', $id_feed)
->where('id_project', '=', $token)
->delete();
}
// now insert
$field_mapping = true;
if( $request->feed_type !== 'scrape' ) {
if( count($projectFieldOptions) ) {
foreach ($projectFieldOptions as $mapping) {
$data[] = [
'id_feed' => $id_feed,
'id_project' => $token,
'import_field_slug' => $mapping['value'],
'internal_field_slug' => $mapping['custom'] ? $mapping['custom_field'] : $mapping['text'],
'custom_field' => $mapping['custom'],
'updates' => $mapping['updates'],
'removes' => $mapping['removes'],
'import' => 1,
'date_add' => now(),
'date_upd' => now()
];
}
} else {
$data = [];
}
$field_mapping = DB::connection($db_name)->table($db_name . ".feed_mappings")->insert($data);
}
Now, I don't want to delete existing rows instead I want to update those rows based on the id_feed_mappings. Can you tell how can I do this?
Check if this would work, to update based on id_feed_mappings value, you can use the ->where('id_feed_mappings', '=' ,'a value or variable') before ->update($data)
if( $request->feed_type !== 'scrape' ) {
// try using update instead of insert
$field_mapping = true;
if( $request->feed_type !== 'scrape' ) {
if( count($projectFieldOptions) ) {
foreach ($projectFieldOptions as $mapping) {
$data[] = [
'id_feed' => $id_feed,
'id_project' => $token,
'import_field_slug' => $mapping['value'],
'internal_field_slug' => $mapping['custom'] ? $mapping['custom_field'] : $mapping['text'],
'custom_field' => $mapping['custom'],
'updates' => $mapping['updates'],
'removes' => $mapping['removes'],
'import' => 1,
'date_add' => now(),
'date_upd' => now()
];
}
} else {
$data = [];
}
$field_mapping = DB::connection($db_name)->table($db_name . ".feed_mappings")->update($data);
}

laravel how to add price product variations in laravel cart session

this is taking more than i anticipated,
On my shopping cart app i'm able to add product to app cart session with no problem, But now i need to add product variations like (color, size, ... etc) for every product added to the cart, These variation can change to product price according to users selection on product details page.
The code that i have currently is working for me except that i can't add a list of variation for a single product i can only create one variation then the code is replacing it with new variation value.
This is my cart object when running {{print_r($cart)}}
App\Cart Object
(
[items] => Array
(
[27] => Array
(
[id] => 27
[name] => product with variation and colors
[slug] => iphone-pro13
[price] => 100
[prefix] => QAR
[qty] => 1
[poster] => /image/md/9964677a-c957-4510-9eb4-f41578c069b3
[subtotal] => 730
[quotable] => 0
[variations] => Array // these are the list of product variations
(
[16] => Array
(
[id] => 16
[var_name] => This is the variation
[var_price] => 365.00
[var_qty] => 1
[var_subtotal] => 365
[color_code] => #e6bf00
)
)
)
[12] => Array
(
[id] => 12
[name] => Dolore quis sunt reiciendis.
[slug] => iste-autem-beatae-eaque-natus-distinctio
[price] => 96.08
[prefix] => QAR
[qty] => 1
[poster] => /media/default/product-placeholder.jpg
[subtotal] => 350.692
[quotable] => 0
[variations] => Array // these are the list of product variations
(
[0] => Array
(
[var_name] => This is the variation 2
[var_price] => 335.00
[var_qty] => 1
[var_subtotal] => 365
[color_code] => #e6bf00
)
)
)
)
the is my cart.php class
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Cart extends Model
{
public $items = null;
public $itemsCount = 0;
public $grandTotal = 0;
public $variations = null;
public function __Construct($oldCart = null) {
if($oldCart) {
$this->items = $oldCart->items;
$this->itemsCount = $oldCart->itemsCount;
$this->grandTotal = $oldCart->grandTotal;
$this->variations = $oldCart->variations;
}
}
public function add($product) {
if (isset($this->items)) {
if( array_key_exists($product->id, $this->items) ) {
$qty = $this->items[$product->id]['qty'] += $product->qty;
$subtotal = $qty * $product->price;
}else{
$qty = $product->qty;
$subtotal = $product->subtotal;
}
}else{
$qty = $product->qty;
$subtotal = $product->subtotal;
}
$item = [
'id' => $product->id,
'name' => $product->name,
'slug' => $product->slug,
'price' => $product->price,
'prefix' => $product->prefix,
'qty' => $qty,
'poster' => $product->poster,
'subtotal' => $subtotal,
'quotable' => $product->quotable,
];
$variations = [
'id' => $product->variation_id,
'var_name' => $product->variation,
'var_price' => $product->variation_price,
'var_qty' => $qty,
'var_subtotal' => $qty * $product->variation_price,
'color_code' => $product->color,
];
$this->items[$product->id] = $item; // <-- this adds new product
$this->items[$product->id]['variations'][$product->variation_id] = $variations; // <-- this adds new product variation
$this->itemsCount +=1;
$this->grandTotal += $product->price * $product->qty;
}
}
Not sure where my code went wrong but i need to be able to add list of product variations instead of replacing current ones.
any ideas?

Linking arrays according to common values

I have some array like
0: [id: '1',department: 'xyz',date: '10-10-2019',time: '12:50']
1: [id: '1',department: 'xyz',date: '11-10-2019',time: '10:30']
2: [id: '2',department: 'abc',date: '09-09-2019',time: '09:50']
3: [id: '2',department: 'abc',date: '07-07-2019',time: '03:20']
I want them to be merged according to the id and department
so the 0 and 1 array will be merged together and the output should be something like
0:[id: '1',department: 'xyz',[[date: '10-10-2019',time: '12:50'],[date: '11-10-2019',time: '10:30']]]
and the 2 and 3 array will be merged together and the output should be something like
1:[id: '2',department: 'abc',[[date: '09-09-2019',time: '09:50'],[date: '07-07-2019',time: '03:20']]]
how can I do it?
Try this..
$input = [
0 =>['id' => '1','department' => 'xyz','date' => '10-10-2019','time' => '12:50'],
1 => ['id' => '1','department' => 'xyz','date' => '11-10-2019','time' => '10:30'],
2 => ['id' => '2','department' => 'abc','date' => '09-09-2019','time' => '09:50'],
3 =>['id' => '2','department' => 'abc','date' => '07-07-2019','time' => '03:20']];
$output = [];
$id_array = [];
foreach ($input as $values)
{
$id_array [] = $values['id'];
}
$unique_id_array = array_unique($id_array);
foreach($input as $key => $in)
{
if(array_key_exists($key,$unique_id_array))
{
$output[] = [
'department' => $in['department'],
'id' => $in['id']
];
}
}
foreach($input as $in)
{
foreach($output as $key => $out)
{
if($out['id'] == $in['id'] && $out['department'] == $in['department'])
{
$output[$key]['date_time'][] = ['date' =>$in['date'],'time' => $in['time']];
}
}
}
print_r($output);die();
The output is going to be
Array
(
[0] => Array
(
[department] => xyz
[id] => 1
[date_time] => Array
(
[0] => Array
(
[date] => 10-10-2019
[time] => 12:50
)
[1] => Array
(
[date] => 11-10-2019
[time] => 10:30
)
)
)
[1] => Array
(
[department] => abc
[id] => 2
[date_time] => Array
(
[0] => Array
(
[date] => 09-09-2019
[time] => 09:50
)
[1] => Array
(
[date] => 07-07-2019
[time] => 03:20
)
)
)
)
Hello i think this will works fine,and it's easy and best way to achieve your output.
i tried many codes in my system with data given by you,just try this
i hope this will work for you as well.
<?php
$inputArr = [0 =>['id' => '1','department' => 'xyz','date' => '10-10-2019','time' => '12:50'],1 => ['id' => '1','department' => 'xyz','date' => '11-10-2019','time' => '10:30'],2 => ['id' => '2','department' => 'abc','date' => '09-09-2019','time' => '09:50'],3 =>['id' => '2','department' => 'abc','date' => '07-07-2019','time' => '03:20']];
print_r($inputArr);
$second_copy = $inputArr;
foreach ($inputArr as $key => $single)
{
$goalArr[$single['id']] = getRow($single['id'],$single['department'],$second_copy);
}
function getRow($id,$department,$second_copy)
{
$returnArr['id'] = $id;
$returnArr['department'] = $department;
foreach ($second_copy as $key => $single)
{
if($id == $single['id'] && $department == $single['department'])
{
$returnArr['date'][] = $single['date'];
$returnArr['time'][] = $single['time'];
}
}
return $returnArr;
}
echo "<br>Output is: <br>";
print_r($goalArr);
?>

how to take index for this array

[file_1] => Array
(
[name] => Array
(
[file] => EmailAccounts-Testing-3-customers-120email-accounts.xlsx
)
[type] => Array
(
[file] => application/octet-stream
)
[tmp_name] => Array
(
[file] => D:\wamp\tmp\phpDB76.tmp
)
[error] => Array
(
[file] => 0
)
[size] => Array
(
[file] => 21505
)
)
Let's do this with Recursive:
public function array_print(){
$myArray = array(
'type'=>'Array outside 1',
'name'=>'Array outside 2',
array(
'file_name'=>'second array',
array(
'file_ext'=>'third array',
array('text'=>'Fourth array inside thrid array')
)
)
);
Print "<pre>";
$output=array();
$result=$this->recursive($myArray);
print_r($result);
//Output value has key and value. I used ## as separator
foreach ($result as $value) {
$data = explode('##',$value);
$output[$data[0]] = $data[1];
}
print_r($output);
}
function recursive($array){
global $temp_data;
if(!empty($array)){
foreach($array as $key => $value){
//If $value is an array.
if(is_array($value)){
//We need to loop through it.
return $this->recursive($value);
} else{
$temp_data[]= $key.'##'.$value;
}
}
}
return $temp_data;
}
Using this we can loop through multidimensional array and store it in Global array. $myArray is your array.

Adding elements to array?

How can I add more than one element to my keyed array?
array_add($myArray, 'key', 'a');
array_add($myArray, 'key-2', 'b');
Is there a better way?
There is no need for any other custom function because in PHP there is a built-in function for this and it's array_merge and you can use it like this:
$myArray = array('one' => 'TheOne', 'two' => 'TheTwo');
$array = array_merge($myArray, array('three' => 'TheThree', 'four' => 'TheFour'));
print_r($array);
Output:
Array
(
[one] => TheOne
[two] => TheTwo
[three] => TheThree
[four] => TheFour
)
You can also use this:
$myArray1 = array('one' => 'TheOne', 'two' => 'TheTwo');
$myArray2 = array('three' => 'TheThree', 'four' => 'TheFour');;
$array = $myArray1 + $myArray2;
print_r($array);
Output:
Array
(
[one] => TheOne
[two] => TheTwo
[three] => TheThree
[four] => TheFour
)
I prefer:
$myArray['key'] = 'a';
$myArray['key-2'] = 'b';
But this is not really better, because you're not adding more than one in a single command.
And if you really need to add multiple, you can always create a helper:
function array_add_multiple($array, $items)
{
foreach($items as $key => $value)
{
$array = array_add($items, $key, $value);
}
return $array;
}
And use it as
$array = array_add_multiple($array, ['key' => 'a', 'key-2' => 'b']);
or, if you're not using PHP 5.4:
$array = array_add_multiple($array, array('key' => 'a', 'key-2' => 'b'));
My custom "on the fly" method:
function add_to_array($key_value)
{
$arr = [];
foreach ($key_value as $key => $value) {
$arr[] = [$key=>$value];
}
return $arr;
}
dd(add_to_array(["hello"=>"from this array","and"=>"one more time","what"=>"do you think?"]));

Resources