0

I'm new to CodeIgniter and PHP in general and I'm stuck in displaying data from a query.

This is my query which I believe is correct.

$sql = $this->db->query("SELECT sum( ".$totField.") FROM ".$table." s WHERE class_id='".$class."' AND subject_id='".$subject."' AND section_id=".$section." GROUP BY `subject_id`");

This query is basically getting to sum of all the total scores of students in different subjects. I'm going to be displaying these scores in a table in the view and am at a loss. I know I should place codes in the student_model, student.php controller, and studentview. I'm kind of confused. This is what I'm doing and I'm very sure I'm wrong.

I need your help. Thanks

This is the view:

<td style="border: 1px solid black;font-size:12px;width:5px;white-space: nowrap;"></td>
<td style="border: 1px solid black;font-size:12px;width:30px;text-align:left;"><?php echo $subName['subject_name'];?></td>
<td style="border: 1px solid black;font-size:12px;width:30px;text-align:center;">
<?php
 $sumMarks = $this->db->query("SELECT sum( ".$totField.") FROM ".$table." s WHERE class_id='".$class."' AND subject_id='".$subject."' AND section_id=".$section." GROUP BY `subject_id`");
echo $sumMarks;?>
</td>

MODEL

 public function SubjectTotal($subject_id,$class_id,$section_id,$session_id,$table,$totField)
{
    $this->db->select_sum($totField);
    $this->db->where('subject_id', $subject_id);
    $this->db->where('class_id', $class_id);
    $this->db->where('section_id', $section_id);
    $this->db->where('session_id', $session_id);
    $this->db->group_by('subject_id');
    return $this->db->get($totField)->row();
      }

CONTROLLER

 
    public function SubjectTotal($subject_id,$class_id,$section_id,$session_id,$table,$totField)
    {
        $data = $this->student_model->SubjectTotal($subject_id,$class_id,$section_id,$session_id,$table,$totField); 
        return $data->$totField;
    }

How do I echo this?

  • 1
    You should definitely not be querying your database in your view -- that is what the model is for. This snippet is very far from how you should be using a MVC design. Please continue researching. – mickmackusa Nov 10 '21 at 23:08
  • As I commented in your earlier question: you find the answers in the documentation of Codeigniter, please take some time to read into it! [Query Builder Class](https://codeigniter.com/userguide3/database/query_builder.html) and [Generating Query Results](https://codeigniter.com/userguide3/database/results.html) – Vickel Nov 10 '21 at 23:24
  • Hi @mickmackusa, I did more research and I came up with the model and controller. Please help and point me aright – MaryPebbles Nov 10 '21 at 23:32
  • Do you want `count_all_results()` in your model? The controller must explicitly pass the model data to the view. Keep researching. – mickmackusa Nov 10 '21 at 23:36
  • @mickmackusa, model edited – MaryPebbles Nov 10 '21 at 23:48

0 Answers0