Reputation: 1492
I want to show current week's data. In databse I have some perticular date data I have fill the missing dates and in view page I am showing that by foreach loop because I want to set different values for them. But it is showing mutliple times data because of 2 foreach loop. I want to get result in one array. My code is:
public function cashflowdetails() {
$cashflow = CashModel::orderBy('payment_date','asc') ->get();
$cashflow_array = [];
$cashflowDate_array = [];
$now = Carbon::now();
if (Input::get('from') != '') {
$weekStartDate =Input::get('from') ;
}
else{
$weekStartDate = Carbon::now()->startOfWeek();
}
if (Input::get('to') != '') {
$weekEndDate = Input::get('to');
}
else{
$weekEndDate = Carbon::now()->endOfWeek()->addDay();
}
$period = CarbonPeriod::create($weekStartDate, $weekEndDate);
foreach ($period as $date) {
$dt = new DateTime($date->format("Y-m-d"));
if($dt->format('l')!= 'Sunday' && $dt->format('l')!= 'SUNDAY' && $dt->format('l')!= 'SATURDAY' && $dt->format('l')!= 'Saturday')
{
foreach($cashflow as $cash){
if($date->format("Y-m-d") === $cash->payment_date )
{
$dbData['id'] = $cash->id;
$dbData['payment_date'] = $cash->payment_date;
$d = new DateTime($cash->payment_date);
$dbData['day'] = $d->format('l');
$dbData['opening_balance'] = $cash->opening_balance;
$dbData['other_income'] = $cash->other_income;
$dbData['rent'] = $cash->rent;
$dbData['labour'] = $cash->labour;
$dbData['electricity'] = $cash->electricity;
$dbData['creditors'] = $cash->creditors;
$dbData['insurance'] = $cash->insurance;
$dbData['direct_debits'] = $cash->direct_debits;
$dbData['others'] = $cash->others;
$dbData['total_amount'] = $cash->total_amount;
$dbData['updated_at'] = $cash->updated_at;
$dbData['created_at'] = $cash->created_at;
print_r($cash->payment_date);
$cashflow_array[] = $dbData;
}
else{
$dbData['id'] = null;
$dbData['payment_date'] = $date->format("Y-m-d");
$d = new DateTime($date->format("Y-m-d"));
$dbData['day'] = $d->format('l');
$dbData['labour'] = 0;
$dbData['opening_balance'] = 0;
$dbData['other_income'] = 0;
$dbData['rent'] =0;
$dbData['electricity'] = 0;
$dbData['creditors'] = 0;
$dbData['insurance'] =0;
$dbData['direct_debits'] = 0;
$dbData['others'] = 0;
$dbData['total_amount'] = 0;
$dbData['updated_at'] = '';
$dbData['created_at'] = '';
$cashflowDate_array[] = $dbData;
}
//break;
}
}
}
$cashflow = array_replace($cashflowDate_array, $cashflow_array);
return $cashflow;
}
Upvotes: 5
Views: 273
Reputation: 106
public function cashflowdetails() {
$cashflow = CashModel::orderBy('payment_date','asc') ->get();
$cashflow_array = [];
$cashflowDate_array = [];
$now = Carbon::now();
if (Input::get('from') != '') {
$weekStartDate =Input::get('from') ;
}
else{
$weekStartDate = Carbon::now()->startOfWeek();
}
if (Input::get('to') != '') {
$weekEndDate = Input::get('to');
}
else{
$weekEndDate = Carbon::now()->endOfWeek()->addDay();
}
$period = CarbonPeriod::create($weekStartDate, $weekEndDate);
foreach ($period as $date)
{
//add a flag to see if the code was executed or not
$isExecuted = false;
$dt = new DateTime($date->format("Y-m-d"));
if($dt->format('l')!= 'Sunday' && $dt->format('l')!= 'SUNDAY' && $dt->format('l')!= 'SATURDAY' && $dt->format('l')!= 'Saturday')
{
foreach($cashflow as $cash){
if( $date->format("Y-m-d") === $cash->payment_date )
{
$dbData['id'] = $cash->id;
$dbData['payment_date'] = $cash->payment_date;
$d = new DateTime($cash->payment_date);
$dbData['day'] = $d->format('l');
$dbData['opening_balance'] = $cash->opening_balance;
$dbData['other_income'] = $cash->other_income;
$dbData['rent'] = $cash->rent;
$dbData['labour'] = $cash->labour;
$dbData['electricity'] = $cash->electricity;
$dbData['creditors'] = $cash->creditors;
$dbData['insurance'] = $cash->insurance;
$dbData['direct_debits'] = $cash->direct_debits;
$dbData['others'] = $cash->others;
$dbData['total_amount'] = $cash->total_amount;
$dbData['updated_at'] = $cash->updated_at;
$dbData['created_at'] = $cash->created_at;
//if executed, assign it true
$isExecuted = true;
//psuh the values to array
$cashflow_array[] = $dbData;
//break the loop
break;
}
}
//if not found in database
if(!$isExecuted){
$dbData['id'] = NULL;
$dbData['payment_date'] = $date->format("Y-m-d");
$d = new DateTime($date->format("Y-m-d"));
$dbData['day'] = $d->format('l');
$dbData['labour'] = 0;
$dbData['opening_balance'] = 0;
$dbData['other_income'] = 0;
$dbData['rent'] =0;
$dbData['electricity'] = 0;
$dbData['creditors'] = 0;
$dbData['insurance'] =0;
$dbData['direct_debits'] = 0;
$dbData['others'] = 0;
$dbData['total_amount'] = 0;
$dbData['updated_at'] = '';
$dbData['created_at'] = '';
$cashflow_array[] = $dbData;
}
}
}
return $cashflow_array;
}
Upvotes: 2
Reputation: 41
you can achieve it easily by using a variable storing your current date, and double checking if the date is repeating or not.
For example:
//all the below code goes inside the foreach($cashflow as $cash)
//declare variable
$flag = false;
$counter = 0;
if($counter == 0)
{
$dt = new DateTime($date->format("Y-m-d"));
$curDate = $dt;
}else{
$dt = new DateTime($date->format("Y-m-d"));
}
if($curDate == $dt && counter !=0)
{
$flag = true;
}
$counter++;
//on your if conditions if($dt->format('l')!= 'Sunday' && $dt->format('l')!= 'SUNDAY' && $dt->format('l')!= 'SATURDAY' && $dt->format('l')!= 'Saturday')
//please add the condition:
&& flag == false
Upvotes: 0