0

i have a marks table with studentid, marks, class_id, exam_id. i got marks and student_id from

$mark_query = $this->db->get_where('mark' , 
                                    array('class_id' => $class_id, 
                                           'exam_id' => $exam_id)
                );

After getting the marks and student_id i am displaying the marks with the student Name which i got it from another table with the student_id from the above query. I have a another table which have the student_id and their name.

The output is like this

Sajiv 25
Arun 35
Sonu 32
Binu 45

But i want to display the names in Ascending order.

Any ideas? I am using codeigniter.

5
  • 2
    use order by $this->db->order_by("name", "asc"); Jun 21, 2018 at 4:07
  • Marks table does not have the name column. I am getting the name from another table with the student_id.
    – binu j
    Jun 21, 2018 at 4:08
  • you getting final result in associative array right? Jun 21, 2018 at 4:24
  • I am getting mark from $mark = $mark_query->row()->mark; and display it
    – binu j
    Jun 21, 2018 at 4:39
  • Show your complete code which queries both tables Jun 21, 2018 at 5:16

2 Answers 2

1

Join the student table with mark table and order by student name in ascending order

    $this->db->select('students_table.name, mark.marks')
    ->from('mark')
    ->join('students_table', 'mark.student_id = students_table.id')
    ->where(['class_id' => $class_id, 'exam_id' => $exam_id])
    ->order_by('students_table.name', 'ASC')
    ->get();
0

Ordering results

$this->db->order_by()

Lets you set an ORDER BY clause.

The first parameter contains the name of the column you would like to order by.

The second parameter lets you set the direction of the result. Options are ASC, DESC.

$this->db->order_by('title', 'DESC');

// Produces: ORDER BY title DESC You can also pass your own string in the first parameter:

$this->db->order_by('title DESC, name ASC');

// Produces: ORDER BY title DESC, name ASC Or multiple function calls can be made if you need multiple fields.

$this->db->order_by('title', 'DESC');

$this->db->order_by('name', 'ASC');

// Produces: ORDER BY title DESC, name ASC If you choose the RANDOM direction option, then the first parameters will be ignored, unless you specify a numeric seed value.

You can always check CodeIgniter Query Builder for help. Here is the link:https://www.codeigniter.com/userguide3/database/query_builder.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.