Search code examples
phpcodeigniteralphabetical

Display array in Alphabetically order Using Codeigniter


I want to display student list according to their result in Alphabetically ascending order. Suppose, Students name who are obtained A grade display first, then who obtained B. I have tried but it is not showing in Alphabetic order.

Thanks for any help.

My code
controller:

function showResult(){

         $data['count']=$this->data_Model->getStudent('result');


        $this->load->view('section',$data); 

      } 
Model:
    public function getStudent($table) {


      $query = $this->db->query("SELECT * FROM $table order by grade asc");

          return $query->result_array();

    }

View

section.php
            <?php 
               foreach($count as $student):

                  echo $student['name']; 
                  echo $student['grade'];
              endforeach; ?>

Solution

  • Simply list the "order by" fields by priority. Ascending is the default so you don't have to specify that.

    $query = $this->db->query("SELECT * FROM $table order by grade, name");