Read Two Tables data using INNER JOIN with CodeIgniter 3

 <?php

defined("BASEPATH") or exit("No direct script access allowed");

class Read extends CI_Controller
{
    public function construct()
    {
        parent::__construct();
    }
    public function index()
    {
        $this->load->model("ReadDataModel", "", TRUE);
        $data["data_details"] =  $this->ReadDataModel->readAllData();
        $this->load->view("showData", $data);
    }
}
Above File is application/controllers/Read.php File.





Below File is application/models/ReadDataModel.php File.
<?php
defined("BASEPATH") or exit("No direct script access allowed");

class ReadDataModel extends CI_Model
{
    public function __construct()
    {
        parent::__construct();
    }
    public function readAllData()
    {
        $query = $this->db->query("SELECT * FROM city INNER JOIN personal ON city.cid = personal.city");
        if ($query) {
            return $query->result();
        }
    }
}







Below File is application/views/showData.php File.
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, shrink-to-fit=no">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Show Data</title>
    <link rel="stylesheet" type="text/css" href="<?php echo base_url(); ?>assets/css/bootstrap.min.css">
</head>

<body>
    <div class="container">
        <table class="table table-striped table-hover">
            <thead>
                <tr>
                    <th>CID</th>
                    <th>CITYNAME</th>
                    <th>ID</th>
                    <th>NAME</th>
                    <th>PERCENTAGE</th>
                    <th>AGE</th>
                    <th>GENDER</th>
                    <th>CITY</th>
                </tr>
            </thead>
            <tbody>
                <?php
                foreach ($data_details as $row) { ?>
                    <tr>
                        <td>
                            <?php echo $row->cid; ?>
                        </td>
                        <td>
                            <?php echo $row->cityname; ?>
                        </td>
                        <td>
                            <?php echo $row->id; ?>
                        </td>
                        <td>
                            <?php echo $row->name; ?>
                        </td>
                        <td>
                            <?php echo $row->percentage; ?>
                        </td>
                        <td>
                            <?php echo $row->age; ?>
                        </td>
                        <td>
                            <?php echo $row->gender; ?>
                        </td>
                        <td>
                            <?php echo $row->city; ?>
                        </td>
                    </tr>
                <?php } ?>

            </tbody>
        </table>
    </div>
</body>

</html>


Comments