CodeIgniter Form Step By Step Tutorial - Part 14: We have learned create table use table library at here. In this post, we try to create as manual (not use table library).
First, add a method books_getall() at model (books_model.php) like this:
function books_getall()
{
$this->load->database();
$query = $this->db->get('books');
return $query->result();
}
Update controller (books.php), like this:
function main(){
$this->load->library('table');
$this->load->helper('html');
$this->load->model('books_model');
$data = $this->books_model->general();
$data['query'] = $this->books_model->books_getall();
$this->load->view('books_main',$data);
}
Last, update views (books_main.php) like this:
<html>
<head>
<link rel="stylesheet" type="text/css"
href="<?php echo "$base/$css"?>">
</head>
<body>
<div id="header">
<? $this->load->view('books_header'); ?>
</div>
<div id="menu">
<? $this->load->view('books_menu'); ?>
</div>
<? echo heading('List of Books',3); ?>
<table border="1">
<tr>
<th>ID</th>
<th>Title</th>
<th>Author</th>
<th>Year</th>
</tr>
<?php
foreach($query as $row){
echo "<tr>";
echo "<td>". $row->id ."</td>";
echo "<td>". $row->title ."</td>";
echo "<td>". $row->author ."</td>";
echo "<td>". $row->year ."</td>";
echo "</tr>";
}
?>
</table>
<div id="footer">
<? $this->load->view('books_footer'); ?>
</div>
</body>
</html>
Result like this: