Home > Enterprise >  WHERE CLAUSE is not filtering results
WHERE CLAUSE is not filtering results

Time:07-19

I have this working function in my model where I'm trying to get the position of students in a class based on their subject scores.

This is working but the where clause does not filter results to classify them based on subjects.

model:

public function GetSubjectPosScores($exam_group_class_batch_exam_subject_id) 
    {
        $sql = "SELECT `get_tot_score` FROM `exam_group_exam_results` 
WHERE `exam_group_class_batch_exam_subject_id`= `exam_group_class_batch_exam_subject_id` ORDER BY `get_tot_score` DESC";
        $query = $this->db->query($sql);
        return $query->result();
    }

controller:

public function GetSubjectPosScores($exam_group_class_batch_exam_subject_id)
    {
        $data = $this->examresult_model->GetSubjectPosScores($exam_group_class_batch_exam_subject_id); 
        return $data;
    }

view:

<?php
$scores2 = $CI->GetSubjectPosScores($exam_group_class_batch_exam_subject_id); //echo $value->pos;
$scores = array_column($scores2, 'get_tot_score');
$pos = array_search($exam_result_value->get_tot_score, $scores);
$number = $pos   1;
 echo $CI->ordinal($number);?>

This is what I get with this query:

student_id | subject_id | get_tot_score | Position
---------------------------------------------------
 11        |   1        |   76          |  3rd
 12        |   1        |   90          |  1st
 28        |   6        |   89          |  2nd
 30        |   6        |   70          |  4th

what I want is this :

student_id | subject_id | get_tot_score | Position
---------------------------------------------------
 11        |   1        |   76          |  2nd
 12        |   1        |   90          |  1st
 28        |   6        |   89          |  1st
 30        |   6        |   70          |  2nd

CodePudding user response:

the problem is in your query

SELECT `get_tot_score` FROM `exam_group_exam_results` 
WHERE `exam_group_class_batch_exam_subject_id`= `exam_group_class_batch_exam_subject_id` ORDER BY `get_tot_score` DESC

basically your are checking that the value in the columnexam_group_class_batch_exam_subject_id is equal to the value of the column exam_group_class_batch_exam_subject_id

Which is always true

you have to change it passing your value $exam_group_class_batch_exam_subject_id using some sort of statement (I don't know what library you are using)

edit I take a look at codignirer documentation

just try this

public function GetSubjectPosScores($exam_group_class_batch_exam_subject_id) 
    {
        $sql = "SELECT `get_tot_score` FROM `exam_group_exam_results` 
WHERE `exam_group_class_batch_exam_subject_id`= ? ORDER BY `get_tot_score` DESC";
        $query = $this->db->query($sql, [$exam_group_class_batch_exam_subject_id]);
        return $query->result();
    }

CodePudding user response:

You have specified string value in the where condition and not the value of the variable passed as parameter in the getSubjectPosScores function

Use String Interpolation in your query

Replace

public function GetSubjectPosScores($exam_group_class_batch_exam_subject_id) { $sql = "SELECT get_tot_scoreFROMexam_group_exam_resultsWHEREexam_group_class_batch_exam_subject_id= exam_group_class_batch_exam_subject_idORDER BYget_tot_score DESC"; $query = $this->db->query($sql); return $query->result(); }

with this

public function GetSubjectPosScores($exam_group_class_batch_exam_subject_id) { $sql = "SELECT get_tot_score FROM exam_group_exam_results WHERE exam_group_class_batch_exam_subject_id= $exam_group_class_batch_exam_subject_id ORDER BY get_tot_score DESC"; $query = $this->db->query($sql); return $query->result(); }

Or

public function GetSubjectPosScores($exam_group_class_batch_exam_subject_id) { $sql = "SELECT get_tot_scoreFROMexam_group_exam_resultsWHEREexam_group_class_batch_exam_subject_id= '".$exam_group_class_batch_exam_subject_id."' ORDER BY get_tot_score DESC"; $query = $this->db->query($sql); return $query->result(); }

  • Related