Farid Ahmadian / PHP

CodeIgniter 2.1.3 -Hello World


Phase 1


application/controllers/messages.php

<?php

class Messages extends CI_Controller {

    function index()
    {
            echo "Hello world (from controller).";
    }

}

Phase 2


application/controllers/messages.php

<?php

class Messages extends CI_Controller {

    function index()
    {
            $data ['msg'] = "Hello world ";
            $this->load->view('show', $data);
    }

}

application/views/show.php

<?php

echo $msg;
echo "(from view).";

Phase 3


MySQL

CREATE DATABASE msg;

CREATE TABLE `messages` (
    `id`            int(11) NOT NULL AUTO_INCREMENT,
    `body`          text NOT NULL,
    PRIMARY KEY (`id`)
);

INSERT INTO `messages` (`id`, `body`) VALUES
(1, 'Hello world !!! (from model)');

application/config/autoload.php

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

application/config/database.php

.
.
$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'root';
$db['default']['password'] = '123456';
$db['default']['database'] = 'msg';
$db['default']['dbdriver'] = 'mysql';
.
.

application/models/message.php

<?php

class Message extends CI_Model {

    function get_msg()
    {
            $this->db->select();
            $this->db->from('messages');
            $query = $this->db->get();
            return $query->result_array();
    }

}

application/controllers/messages.php

<?php

class Messages extends CI_Controller {

    function index()
    {
            $this->load->model('message');
            $data ['msgs'] = $this->message->get_msg();
            $this->load->view('show', $data);
    }

}

application/views/show.php

<?php

foreach ($msgs as $msg)
    echo $msg['body'];

BY: Pejman Moghadam
TAG: php, codeigniter
DATE: 2013-01-27 19:36:53


Farid Ahmadian / PHP [ TXT ]

With many thanks and best wishes for dear Pejman Moghadam, someone who taught me alot in linux and life :)