Read Table Data in CodeIgniter 3
<?php
defined("BASEPATH") or exit("No direct script access allowed");
class Contacts extends CI_Controller
{
public function __construct()
{
parent::__construct();
}
public function index()
{
$this->load->model("ContactsModel");
$data["details"] = $this->ContactsModel->getContacts();
$this->load->view("contacts_view", $data);
$this->load->model("StudentsModel");
$data["records"] = $this->StudentsModel->getStudentsData();
$this->load->view("students_view", $data);
}
}
Above File is application/controllers/Contacts.php FileBelow File is application/models/ContactsModel.php File
<?php
defined("BASEPATH") or exit("No direct script access allowed");
class ContactsModel extends CI_Model
{
public function __construct()
{
parent::__construct();
}
public function getContacts()
{
$query = $this->db->query("SELECT * FROM contact");
if ($query) {
return $query->result();
}
}
}
Below File is application/models/StudentsModel.php File
<?php
defined("BASEPATH") or exit("No direct script access allowed");
class StudentsModel extends CI_Model
{
public function __construct()
{
parent::__construct();
}
public function getStudentsData()
{
$query = $this->db->query("SELECT * FROM students");
if ($query->num_rows() > 0) {
return $query->result();
} else {
return false;
}
}
}
Below File is application/views/contacts_view.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>Contacts View</title>
<link rel="stylesheet" href="<?php echo base_url(); ?>assets/css/bootstrap.min.css">
</head>
<body>
<h1>Contacts View List</h1>
<table class="table table-striped table-hover">
<tr>
<th>Id</th>
<th>Name</th>
<th>Email</th>
<th>Mobile</th>
<th>Message</th>
<th>City</th>
</tr>
<tr>
<?php foreach ($details as $row) { ?>
<tr>
<td>
<?php echo $row->id; ?>
</td>
<td>
<?php echo $row->name; ?>
</td>
<td>
<?php echo $row->email; ?>
</td>
<td>
<?php echo $row->mobile; ?>
</td>
<td>
<?php echo $row->message; ?>
</td>
<td>
<?php echo $row->city; ?>
</td>
</tr>
<?php } ?>
</tr>
</table>
</body>
</html>
Below File is application/views/students_view.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>Student View</title>
</head>
<body>
<h1>Students View List</h1>
<table class="table table-striped table-hover">
<tr>
<th>ID</th>
<th>First Name</th>
<th>Last Name</th>
<th>Age</th>
</tr>
<tr>
<?php if (count($records) > 0) { ?>
<table class="table table-striped table-hover">
<tr>
<th>ID</th>
<th>First Name</th>
<th>Last Name</th>
<th>Age</th>
</tr>
<tr>
<?php foreach ($records as $row) { ?>
<tr>
<td>
<?php echo $row->id; ?>
</td>
<td>
<?php echo $row->first_name; ?>
</td>
<td>
<?php echo $row->last_name; ?>
</td>
<td>
<?php echo $row->age; ?>
</td>
</tr>
<?php } ?>
</tr>
</table>
<?php } else {
echo "<h1>Sorry! No Records Found.</h1>";
} ?>
</tr>
</table>
</body>
.png)
.png)
Comments
Post a Comment