0

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:

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?

halfer
  • 19,824
  • 17
  • 99
  • 186

4 Answers4

5

This is not the problem with your view page. go to config folder open autoload.php

$autoload['helper'] = array('url');

Other Way

Uncomment the #$this->load->helper('url'); to $this->load->helper('url');

<?php

class Users_model extends CI_Model {

    function __construct()
    {
        parent::__construct();

        $this->load->database();
        $this->load->helper('url');

    }   

    public function get_all_users()
    {
        $query = $this->db->get('users');
        return $query->result();
    }

}

?>
Sanith
  • 681
  • 7
  • 13
  • As soon as I set `$autoload['helper'] = array('url')`, my view page started to work alright. So I'm marking your solution as answer. But can you also explain me another thing: I made another mistake in my model - `users_model.php`. I wrote `$this->load->database()` and didn't put my database name there, which is `ci_test`. In the tutorial, I found that it was originally written as `$this->load->database(ci_test)`. What is the significance of this using my database name? Is it necessary? – Choudhury Saadmaan Mahmid Sep 25 '14 at 04:31
  • There are two ways to connect to a database: – Sanith Sep 29 '14 at 04:24
2

The answer for your comment : $this->load->database()

There are two ways to connect to a database: The "auto connect" in application/config/autoload.php

$autoload['libraries'] = array('database');

Manually Connecting

The first parameter of this function can optionally be used to specify a particular database group from your config file, or you can even submit connection values for a database that is not specified in your config file.

$config['hostname'] = "localhost";
$config['username'] = "myusername";
$config['password'] = "mypassword";
$config['database'] = "mydatabase";
$config['dbdriver'] = "mysql";
$config['dbprefix'] = "";
$config['pconnect'] = FALSE;
$config['db_debug'] = TRUE;
$config['cache_on'] = FALSE;
$config['cachedir'] = "";
$config['char_set'] = "utf8";
$config['dbcollat'] = "utf8_general_ci";

$this->load->database($config);
Sanith
  • 681
  • 7
  • 13
1

Instead of this <?php echo base_url();?>index.php/user/add_form Use: <?php echo site_url('user/add_form');?>

Sanith
  • 681
  • 7
  • 13
0

I think problem is in this line:

window.location="<?php echo base_url();?>index.php/users/"+act+"/"+gotoid;

maybe you have forgot load helper url?

user2883814
  • 365
  • 2
  • 18