First of all, I am absolutely new to CodeIgniter.
I'm trying to develop a simple CRUD
page using CodeIgniter. So far, I've just created the View page, without any CRUD
feature. It's just a normal view page, with a table that contains my model data retrieved from the database.
Here is the code for my view page which works ok, file name - show_users.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>CI CRUD</title>
</head>
<body>
<h2> Simple CI CRUD Application </h2>
<table width="600" border="1" cellpadding="5">
<tr>
<th scope="col">Id</th>
<th scope="col">User Name</th>
<th scope="col">Email</th>
<th scope="col">Mobile</th>
<th scope="col">Address</th>
</tr>
<?php foreach ($user_list as $u_key){ ?>
<tr>
<td><?php echo $u_key->id; ?></td>
<td><?php echo $u_key->name; ?></td>
<td><?php echo $u_key->email; ?></td>
<td><?php echo $u_key->address; ?></td>
<td><?php echo $u_key->mobile; ?></td>
</tr>
<?php }?>
</table>
</body>
</html>
My model is - users_model.php
<?php
class Users_model extends CI_Model {
function __construct()
{
parent::__construct();
$this->load->database();
}
public function get_all_users()
{
$query = $this->db->get('users');
return $query->result();
}
}
?>
My controller is - users.php
<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Users extends CI_Controller {
function __construct()
{
parent::__construct();
#$this->load->helper('url');
$this->load->model('users_model');
}
public function index()
{
$data['user_list'] = $this->users_model->get_all_users();
$this->load->view('show_users', $data);
}
}
?>
My database server is MySQL, database name ci_test
and my table is users
:
CREATE TABLE IF NOT EXISTS `users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(50) NOT NULL,
`email` varchar(100) NOT NULL,
`address` varchar(255) NOT NULL,
`mobile` varchar(15) NOT NULL,
PRIMARY KEY (`id`)
)
My view page is showing this output:
So far so good. Now, whenever I try to modify my view page show_users.php
, the page is loaded, but empty.
Here is the code of my modified view page, for which I encounter the above mentioned problem:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>CI CRUD</title>
<script type="text/javascript">
function show_confirm(act,gotoid)
{
if(act=="edit")
var r=confirm("Do you really want to edit?");
else
var r=confirm("Do you really want to delete?");
if (r==true)
{
window.location="<?php echo base_url();?>index.php/users/"+act+"/"+gotoid;
}
}
</script>
</head>
<body>
<h2> Simple CI CRUD Application </h2>
<table width="600" border="1" cellpadding="5">
<tr>
<th scope="col">Id</th>
<th scope="col">User Name</th>
<th scope="col">Email</th>
<th scope="col">Mobile</th>
<th scope="col">Address</th>
<th scope="col" colspan="2">Action</th>
</tr>
<?php foreach ($user_list as $u_key){ ?>
<tr>
<td><?php echo $u_key->id; ?></td>
<td><?php echo $u_key->name; ?></td>
<td><?php echo $u_key->email; ?></td>
<td><?php echo $u_key->address; ?></td>
<td><?php echo $u_key->mobile; ?></td>
<td width="40" align="left" ><a href="#" onClick="show_confirm('edit',<?php echo $u_key->id;?>)">Edit</a></td>
<td width="40" align="left" ><a href="#" onClick="show_confirm('delete',<?php echo $u_key->id;?>)">Delete </a></td>
</tr>
<?php }?>
<tr>
<td colspan="7" align="right"> <a href="<?php echo base_url();?>index.php/user/add_form">Insert New User</a></td>
</tr>
</table>
</body>
</html>
Note that I haven't changed anything in my model and controller yet.
Can anyone please specify where the problem is? Any convenient and efficient solution to this? I am using Notepad++ for development, no IDE.
EDIT: Although I found a solution to my problem and accepted it as answer, I'm still confused about one issue.
In my model - users_model.php
, I have a line:
$this->load->database();
But in the tutorial, it was written like this:
$this->load->database("ci_test");
So basically, I forgot to provide my database name, which is ci_test
. I still haven't edited my model code, but my view page is working perfectly now. So what is the significance of it? Is it necessary to mention the database name inside $this->load->database()
? Is this one of the reasons my view page was empty?