I was working on a project. In the project I have to make a report in which all the values that are being checked from the form using check box are stored in the column areChecked, whereas unchecked checkboxes values are stored in areUnchecked column. The values are stored perfectly as according to requirements. But whenever I checked these columns like if the values are inside the arechecked column then it should print 1 else 0. But in my case the code is not printing as per the conditions which made me confused a bit. Below is my controller:
public function scoreSheet(Request $request)
{
$query = new QCFeedback;
$users = User::all();
$cru_users = CRUUser::all();
$feedbacks = $query->paginate(20);
return view('qc-feedback-audits.score-sheet', compact('feedbacks', 'users', 'cru_users'));
}
here $feedbacks is the variable that collects all the data from qc_feedbacks. Below are two pictures that will further elaborate the problem: The Structure of the table how data is insert especially in the columns areChecked and areUnchecked The way I am checking that if there are checked boxes then show 1 else 0 in the view is:
@foreach ($feedbacks as $feedback)
<tr>
<td>{{ $feedback->record_id ?? '-' }}</td>
<td>{{ $feedback->cru_user->cru_id ?? '-' }}</td>
@php
$areChecked = json_decode($feedback->areChecked);
$areUnChecked = json_decode($feedback->areUnChecked);
@endphp
@if (array_search('score-1', $areChecked))
<td>{{ 1 }}</td>
@else
<td>{{ 0 }}</td>
@endif
@if (array_search('score-6', $areChecked))
<td>{{ 1 }}</td>
@else
<td>{{ 0 }}</td>
@endif
@if (array_search('score-11 ', $areChecked))
<td>{{ 1 }}</td>
@else
<td>{{ 0 }}</td>
@endif
@if (array_search('score-2', $areChecked))
<td>{{ 1 }}</td>
@else
<td>{{ 0 }}</td>
@endif
@if (array_search('score-7', $areChecked))
<td>{{ 1 }}</td>
@else
<td>{{ 0 }}</td>
@endif
@if (array_search('score-12', $areChecked))
<td>{{ 1 }}</td>
@else
<td>{{ 0 }}</td>
@endif
@if (array_search('score-3', $areChecked))
<td>{{ 1 }}</td>
@else
<td>{{ 0 }}</td>
@endif
@if (array_search('score-8', $areChecked))
<td>{{ 1 }}</td>
@else
<td>{{ 0 }}</td>
@endif
@if (array_search('score-13', $areChecked))
<td>{{ 1 }}</td>
@else
<td>{{ 0 }}</td>
@endif
@if (array_search('score-4', $areChecked))
<td>{{ 1 }}</td>
@else
<td>{{ 0 }}</td>
@endif
@if (array_search('score-9', $areChecked))
<td>{{ 1 }}</td>
@else
<td>{{ 0 }}</td>
@endif
@if (array_search('score-14', $areChecked))
<td>{{ 1 }}</td>
@else
<td>{{ 0 }}</td>
@endif
@if (array_search('score-19', $areChecked))
<td>{{ 1 }}</td>
@else
<td>{{ 0 }}</td>
@endif
@if (array_search('score-5', $areChecked))
<td>{{ 1 }}</td>
@else
<td>{{ 0 }}</td>
@endif
@if (array_search('score-10', $areChecked))
<td>{{ 1 }}</td>
@else
<td>{{ 0 }}</td>
@endif
@if (array_search('score-15', $areChecked))
<td>{{ 1 }}</td>
@else
<td>{{ 0 }}</td>
@endif
@if (array_search('score-20', $areChecked))
<td>{{ 1 }}</td>
@else
<td>{{ 0 }}</td>
@endif
@if (array_search('score-18', $areChecked))
<td>{{ 1 }}</td>
@else
<td>{{ 0 }}</td>
@endif
@if (array_search('score-17', $areChecked))
<td>{{ 1 }}</td>
@else
<td>{{ 0 }}</td>
@endif
@if (array_search('score-16', $areChecked))
<td>{{ 1 }}</td>
@else
<td>{{ 0 }}</td>
@endif
<td>
{{ count($areChecked) }}
</td>
<td>{{ (count($areChecked) / 20) * 100 }}</td>
<td></td>
<td></td>
<td>{{ $feedback->elevation_audit->name ?? '-' }}</td>
</tr>
@endforeach
In the above code expect for the values of 1 or 0 all data is being retrieved accurately. It is just missing or skipping the values in the process of checking if the value is checked then show 1 else 0.
CodePudding user response:
Assalomu aleykum, array_search returns false if value not found in array else returns key of value. If key of value 0 this is actually false. In your situation you need in_array function, this function checks the value placed in array.
Use this code like:
@php
$check = ['score-1','score-6','score-11','score-2','score-
7','score-12','score-3','score-8','score-13','score-
4','score-9','score-14','score-19','score-5','score-
10','score-15','score-20','score-18','score-17','score-
16',];
@endphp
@foreach ($feedbacks as $feedback)
<tr>
<td>{{ $feedback->record_id ?? '-' }}</td>
<td>{{ $feedback->cru_user->cru_id ?? '-' }}</td>
@php
$areChecked = json_decode($feedback->areChecked);
$areUnChecked = json_decode($feedback->areUnChecked);
@endphp
@foreach ($check as $key)
@if (in_array($key, $areChecked))
<td>{{ 1 }}</td>
@else
<td>{{ 0 }}</td>
@endif
@endforeach
<td>
{{ count($areChecked) }}
</td>
<td>{{ (count($areChecked) / 20) * 100 }}</td>
<td></td>
<td></td>
<td>{{ $feedback->elevation_audit->name ?? '-' }}</td>
</tr>
@endforeach