Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display data from multiple tables in View using codeigniter

Student table Student table

Result table Result table

MY Question: Student table join result table ,but same student name display multiple, so My question I have same student display once

Model:

public function select(){
$this->db->select('*');
$this->db->from('result');
 $this->db->join('student', 'result.student_id=student.student_id');
 return $this->db->get()->result_array();
}

Controller:

public function index(){
  $data = array(
            'page_title' => 'Result',
            'page_name' => 'result/result',
            'result'=>  $this->result_model->select()
        );
$this->load->view('admin/template', $data);
}

View

 <?php foreach ($result as $key => $value): ?>
                <tr>
                    <td><?php echo $value['first_name']; ?></td>
 <td><?php echo $value['mark']; ?></td>
</tr>
<?php endforeach;?>
like image 425
kishankakadiya Avatar asked Jul 15 '26 06:07

kishankakadiya


1 Answers

You can use Group By for display your record as one time

Add This code in your model:

Model:

<?php

Class Result_model extends CI_Model {

    public function __construct() {
        // Call the CI_Model constructor
        parent::__construct();
    }
    public function select(){
         $this->db->select('*');
         $this->db->from('result');
         $this->db->join('student', 'result.student_id=student.student_id');
         $this->db->group_by('student.student_id');
         return $this->db->get()->result_array();
    }
}
like image 119
Mayank Vadiya Avatar answered Jul 18 '26 09:07

Mayank Vadiya



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!