Quick Learning of CI basic CRUD prog.

Downloaded and installed WAMP (http://www.wampserver.com/en/) in a directory called wamp (under some directory). Started wamp by running wamp\wampmanager.exe. Noted that http://localhost showed:
Server Configuration

Apache Version :
2.2.17
PHP Version :
5.3.5
…..
…..

Followed http://fwebde.com/php/introduction-to-codeigniter/. Created directory ci under wamp\www. Copied CodeIgniter 2.0.3 (http://codeigniter.com/downloads/) files in wamp\www\ci folder.

Encountered some problems with app. of above tutorial. Seems like some of the problems at least are due to the tutorial being written for an older version of CI. Fixed problems as given below (Note that fixed versions of related source files source code for both fwebde.com tutorial pages referred in this post are given at the bottom of this post):

message.php:
a) use CI_Controller instead of Controller
b) Not a problem but a note that function foobar() should be defined with class Message (within its closing brace).

With these changes, if I recall correctly, it worked properly.

Then followed http://fwebde.com/php/simple-crud-with-codeigniter/.
For creation of database http://localhost showed a phpmyadmin link which opened up to mySQL administration page. It was easy to create database ci and then execute SQL to create table posts using it.

For database.php changes figured out that default mySQL admin id is ‘root’ with ” (blank/null) password

Then I encountered quite a few problems as listed below:
crud.php problems:
a) It required <?php at the beginning (and ?> at the end – though that may be optional).
b) CI_Controller instead of Controller
c) $this->security->xss_clean instead of $this->input->xss_clean (2 places)
d) $this->load->model(‘posts_model’); $this->posts_model->addPost($title, $content);
was required. The tutorial omits the load->model() due to which it gives an error.

posts_model.php problems:
a) It required <?php at the beginning (and ?> at the end – though that may be optional).
b) CI_Model instead of Model

With these changes, if I recall correctly, ci/index.php/crud/ worked correctly. I could add rows to posts table.

For the read part I needed only one tutorial fix in crud.php:
a)     $this->load->model(‘posts_model’);  has to be moved to beginning of index() function (before if statement)

Now http://localhost/ci/index.php/crud shows the existing records first and then has the input fields and Submit button.

For update needed following fixes:
a) In crud.php in function update $this->security->xss_clean instead of $this->input->xss_clean (2 places)
b) In crud.php in function update before invoking  $this->posts_model->updatePost($id, $title, $content);
we need $this->load->model(‘posts_model’);

For delete needed following fixes:
b) In crud.php in function delete before invoking  $this->posts_model->deletePost($id);
we need $this->load->model(‘posts_model’);

That’s it. I have got a running start on CI including bare bones CRUD – no pagination, no validation, no smart choose row & update/delete etc. & zero presentation niceties – but still a functional CRUD app. I need to analyze the code more carefully and also perhaps refer CI documentation where needed but overall I am pretty confident now about using basic CRUD functionality with CI.

I think my experience with MVC pattern starting from early versions of Microsoft Foundation Classes (MFC) allowed me to more easily understand CI MVC classes. ASP.Net, HTTP protocol, socket prog. etc background as well as some scripting language exposure allowed me get some quick idea of other parts of CI and php that came into play in the CRUD app & first hello world app.

It just took me maybe three to four hours for this running start into CI CRUD apps. Possibly an hour or so more for documenting all this stuff decently. But I must say that I have not studied each line of code in detail.

I am giving these details so that others who are considering learning CI basic CRUD functionality can get some feel about the “learning curve” time frame.

I had created a zip of all the controller, model and view files created, with my fixes, for the two FWebDe tutorials I used to learn CI CRUD stuff. But came to learn that WordPress does not allow upload of zip files!!

So I have just appended all the text of the relevant source files below.
Controllers
————-
File: message.php
———————
<?php
// message.php

class Message extends CI_Controller
{

function index() {
$msg1 = ‘hello, world’;
$msg2 = ‘a second message’;

$data = array(
‘msg1’ => $msg1,
‘msg2’ => $msg2
);

$this->load->view(‘display’, $data);
}

function foobar() {
$msg1 = ‘foo’;
$msg2 = ‘bar’;

$data = array(
‘msg1’ => $msg1,
‘msg2’ => $msg2
);

$this->load->view(‘display’, $data);
}

}

—-
File: crud.php
—————-
<?php

class Crud extends CI_Controller {

function index() {

$this->load->model(‘posts_model’);
// Check if form is submitted
if ($this->input->post(‘submit’)) {
$title = $this->security->xss_clean($this->input->post(‘title’));
$content = $this->security->xss_clean($this->input->post(‘content’));

// Add the post

//$this->load->model(‘posts_model’); $this->posts_model->addPost($title, $content);
$this->posts_model->addPost($title, $content);
}

$data = array();

$data[‘posts’] = $this->posts_model->getPosts();
$this->load->view(‘crud_view’, $data);
//$this->load->view(‘crud_view’);
}

function update() {
$id = $this->uri->segment(3);

if ($this->input->post(‘submit’)) {
$title = $this->security->xss_clean($this->input->post(‘title’));
$content = $this->security->xss_clean($this->input->post(‘content’));

$this->load->model(‘posts_model’);
$this->posts_model->updatePost($id, $title, $content);

$data[‘posts’] = $this->posts_model->getPosts();
$this->load->view(‘crud_view’, $data);
} else {
$data = array(‘id’ => $id);
$this->load->view(‘updateform’, $data);
}
}

function delete() {
$id = $this->uri->segment(3);

$this->load->model(‘posts_model’);

$this->posts_model->deletePost($id);

$data[‘posts’] = $this->posts_model->getPosts();
$this->load->view(‘crud_view’, $data);
}
}
?>
—-

Models
—————-
File: posts_model.php
————————–
<?php

class Posts_model extends CI_Model {

function addPost($title, $content) {
$data = array(
‘title’ => $title,
‘content’ => $content
);

$this->db->insert(‘posts’, $data);
}

function getPosts() {
$query = $this->db->get(‘posts’);
$posts = array();

foreach ($query->result() as $row) {
$posts[] = array(
‘id’ => $row->id,
‘title’ => $row->title,
‘content’ => $row->content
);
}

return $posts;
}

function updatePost($id, $title, $content) {
$data = array(
‘title’ => $title,
‘content’ => $content
);

$this->db->where(‘id’, $id);
$this->db->update(‘posts’, $data);
}

function deletePost($id) {
$this->db->where(‘id’, $id);
$this->db->delete(‘posts’);
}

}
?>
—–

Views
——————–
File: display.php
——————-
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.1//EN” “http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd”&gt;
<html xmlns=”http://www.w3.org/1999/xhtml&#8221; xml:lang=”en”>

<head>
<title>CodeIgniter Test</title>
</head>
<body>
<h1>CodeIgniter Test</h1>
<p><strong>Message 1:</strong> <?php echo $msg1; ?></p>

<p><strong>Message 2:</strong> <?php echo $msg2; ?></p>
</body>
</html>
—–

File:crud_view.php
———————–
<?php $this->load->helper(‘form’); ?>

<?php echo form_open(‘crud’); ?>
<?php if (isset($posts)): foreach ($posts as $p): ?>

<h2><?php echo $p[‘title’]; ?></h2>

<?php echo $p[‘content’]; ?>

<?php endforeach; else: ?>

<h2>No posts found</h2>

<?php endif; ?>
<p>
<?php echo form_input(‘title’); ?>
</p>

<p>
<?php echo form_textarea(‘content’); ?>
</p>

<p>
<?php echo form_submit(‘submit’, ‘Submit’); ?>
</p>

<?php echo form_close(); ?>
—-

File: updateform.php
————————
<?php $this->load->helper(‘form’); ?>

<h1>CodeIgniter CRUD Tutorial Example</h1>

<?php echo form_open(‘crud/update/’.$id); ?>

<p>
<?php echo form_input(‘title’); ?>
</p>

<p>
<?php echo form_textarea(‘content’); ?>
</p>

<p>
<?php echo form_submit(‘submit’, ‘Submit’); ?>
</p>

<?php echo form_close(); ?>
—–

This entry was posted in Spoken English App. Blog. Bookmark the permalink.

5 Responses to Quick Learning of CI basic CRUD prog.

  1. Nithin says:

    I have this error when add data to the table
    ———-
    A PHP Error was encountered

    Severity: Notice

    Message: Undefined property: Crud::$db

    Filename: models/posts_model.php

    Line Number: 18

    Fatal error: Call to a member function insert() on a non-object in D:\wamp\www\CI\application\models\posts_model.php on line 18
    ———–

  2. Nithin says:

    Problem solved
    It was a mistake.In config/autoload.php,I forget to Change the autoloaded libraries to this:
    $autoload[‘libraries’] = array(‘database’);

  3. Mutongoria says:

    this worked for me. thanks.

    you did well to include a link to the original post – it helped be debug the crud app

  4. Kelster says:

    One more useful fix. In the controller, put this code at the top:
    public function __construct()
    {
    parent::__construct();
    $this->load->model(‘posts_model’);
    }
    Then, you don’t have to put $this->load->model(‘posts_model’); into each function. It is also the more “Codeigniter 2.0” way

  5. Ravi says:

    Thanks Kelster. I do not have the time to check it out now. But readers can try it and get the benefit of your fix.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s