« first day (130 days earlier)      last day (218 days later) » 

6:26 AM
Hi sir @ZakariaAcharki
How are you ?
I am doing a small update in the project but facing an issue
I will just explain that issue
I have an array of obtained marks
And its in the descending order
So I am gonna get its index number and that will be student's position in the class
Now I done everything else. I am getting the array and getting its index number too
But every time its saving the last index number for all of them
Let me show you the code
$array = array_reverse(array_sort($input['obt_marks']));

foreach ($array as $j => $pos) {
$position = $j;
}
This is what I done
Take a look please when you have time :)
 
 
2 hours later…
8:17 AM
Hi bro
where you use $position after that ?
 
8:44 AM
Hi sir
Because I wanna save the position index in $position
And then save it in the table
Let me show you
Report::create([
'percentage' => $percentage,
'position' => $position + 1,
Like this
So the index started from 0 that's why I added 1
 
9:12 AM
try to put this piece of code inside the loop
 
Report::create([
'percentage' => $percentage,
'position' => $position + 1,
This ?
 
10:10 AM
foreach ($array as $j => $pos) {
$position = $j;
Report::create([
'percentage' => $percentage,
'position' => $position + 1
...
}
yes
 
I did that as well
The its storing the multiple
data
 
yes
do you want to store all the positions ?
 
Yeah all the positions
 
where ?
if in the "Report"
 
But lets say I have two reports. Then its storing four reports when I put that inside
Yeah in the Report
 
10:13 AM
if you've 5 positions
then you'll end with 5 reports
 
Let me check it again
 
what you mean by 2 reports ?
what is the relationship between report and position
 
Sir I am calculating position on the base of percentage
for each student
 
By 2 reports
I mean
If I wanna store the reports for two students
And put that inside foreach
Then its creating four reports
Because I put that inside foreach
Indeed the positiong is saving correctly
 
10:16 AM
that based on the count of $array
 
But I got multiple data in that case
 
show me the full code so I can understand the logic
 
ok sir
 
what $array contains ?
and "$array" itself as the variable name is a bad practice
the variable name should reflect the data stored inside it
 
No sir I was testing it
That's why
Actually in $array I am saving the percentage in descending order
Surely I will change it later
Code
 
10:19 AM
ok got it, but where you have the students?
 
in the students table
 
and how you'll say that this report is about X student
?
 
Let me explain the whole logic briefly
 
First I have a table named as subjects
So the user will define the subject name and full marks and class
Schema::create('subjects', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('subject_name');
$table->string('subject_marks');
$table->string('class_id');
$table->string('class_name');
$table->string('rep_cat');
$table->string('rep_cat_name');
$table->timestamps();
});
This is the structure for subjects table
Now the user will define all these values for each class
 
10:22 AM
ok
 
Now when they click a button I added for adding marks
Then it go to a view
Where the students of that class given in the subjects
will be coming from the student table
And input name as obtained marks
So they will just enter the obtained marks for each student
 
And the rest will be calculating auto like grade, percentage and position
This is the whole logic actually
wait
 
Then after submitting that record
It go to the reports table
$table->increments('id');
$table->string('student_id');
$table->string('student_name');
$table->integer('class_id');
$table->integer('class_name');
$table->string('subject');
$table->string('teacher_name');
$table->string('report_categories_id');
$table->string('report_categories_name');
$table->string('total_marks');
$table->string('obtained_marks');
$table->string('percentage');
$table->string('position');
$table->string('grade');
$table->string('date');
$table->string('created_by');
$table->string('status')->default('unpost');
This is the structure for reports table
 
10:24 AM
good
 
Now here everything is working as expected and its so easy to use
The only problem is position
 
show me the store method that store all those information in the Reports table
 
ok sir
Store method
public function store(Request $request) {

$input = $request->all();
$date = $input['date'];
$teacher_name = $input['teacher_name'];


foreach($input['student_id'] as $i => $student_id) {

if (is_numeric($input['obt_marks'][$i])) {
$percentage = (($input['obt_marks'][$i]/$input['total_marks'][$i]) * 100);
} else {
$percentage = 0;
}

if ($percentage >= 0 && $percentage <= 49) {
$grade = 'D';
} elseif ($percentage >= 50 && $percentage <= 59) {
$grade = 'C';
} elseif ($percentage >= 60 && $percentage <= 69) {
I changed that $array to $marks
Now here I follow your suggestion to put that Report inside the foreach
And I get multiple records in this case
Yes it stores the right positions but multiple records created too
 
first thing noticed here is
the Report::create([ is inside two foreach that what explained why you got duplicate data
 
Yes
Exactly
 
10:30 AM
I'm checking the code
 
That's why I was not putting that inside because I was aware of that another foreach
Ok sir
 
$input['obt_marks']
contain all the students marks
that it ?
 
yes
That's array
Let me show you
 
so every mark is related to a student
no it's ok
 
yes sir
ok sir
 
10:34 AM
I'm just trying to explain
 
Okay sir
 
every mark related to a student
 
yes sir
 
if you take just those marks and order, reverse them
then you don't know any more
inside this array which lark belong to wich student
 
yeah
right
 
10:35 AM
and also how you must deal with the same exact marks
 
exactly
 
you'll just give them an incremental position
or the same position for them all ?
 
incremental
In case if any of two students got the exact same percentage
Then in that case it will be same
 
so if 5 students have the 20 mark for example
all the 5 then will have the same position
?
 
Yeah
 
10:38 AM
then the easiest way comes to my mind now is
get unique marks from the array
then order them
 
I had no idea how to deal with positions so I just used that approach but I think it will not work
 
and in the report creation, we'll get the position by mark
let so that and test it
 
Get the unique marks ?
It means getting the marks which are not same
 
I'll give you a sample
 
ok sir
 
10:51 AM
public function store(Request $request) {

$input = $request->all();
$date = $input['date'];
$teacher_name = $input['teacher_name'];


foreach($input['student_id'] as $i => $student_id) {

if (is_numeric($input['obt_marks'][$i])) {
$percentage = (($input['obt_marks'][$i]/$input['total_marks'][$i]) * 100);
} else {
$percentage = 0;
}

if ($percentage >= 0 && $percentage <= 49) {
$grade = 'D';
} elseif ($percentage >= 50 && $percentage <= 59) {
$grade = 'C';
} elseif ($percentage >= 60 && $percentage <= 69) {
 
ok sir
Let me understand it first and then will test it
I will inform you if I got issues
 
Yes
Exactly working the same I was struggling for
Now I am gonna search a lil deep about array_unique
 
Cool
it's simple
let say
$marks = [10,15,12,13,18,10,19,20,13,13];

$unique_marks = array_unique($marks);

the output will be :
Array
(
[0] => 10
[1] => 15
[2] => 12
[3] => 13
[4] => 18
[6] => 19
[7] => 20
)
it will remove any duplicate
gives you just unique marks
 
oh fine
Got it
 
10:59 AM
then we'll sort and reverse
and the last part is getting the position by mark
 
Yes sir
 
using array_search()
 
Got the rest of changes
Yes sir
 
Then good job
you were close
 
Thank you so much sir for the support
Haha yes sir
I was struggling for 8 hours I guess
At the same time I have one more issue and that is complex I guess. But I am working still on that. I will tell if I failed completely
 
11:03 AM
Ok you're doing well
and you're welcome
 
 
5 hours later…
4:20 PM
Hi sir
Another issue in the same case
Now what if the user update the obtained marks of a student
Then its position must be changed
So for that I do something like this
$records = DB::table('reports')->select('obtained_marks', 'subject', 'report_categories_id')->where([['class_id', '=', $report->class_id], ['report_categories_id', '=', $report->report_categories_id],
['subject', '=', $report->subject]])->get()->toArray();

sort($records);
$marks = array_reverse($records);

foreach ($records as $i => $record) {
$m = array_search($marks[$i], $marks)+1;
$input['position'] = $m;
}
But this is not working as expected
till the $mark variable its working fine
Here is the output of $marks
array:2 [▼
0 => {#432 ▼
+"obtained_marks": "56"
}
1 => {#430 ▼
+"obtained_marks": "43"
}
]
Here I updated that 56 marks from 34 marks and now its on the top
But just the position is not changing in the database
Take a look please when you have time
 

« first day (130 days earlier)      last day (218 days later) »