Here we will see how to call model in controller and pass value of controller to view that means we are talking about flow of MVC pattern in codeigniter. Lets start practical things with the below steps.
First create database in MySQL. Mentioned syntax in the below:
create database website;
Second create table.
create table web_pages(
page_id int unsigned auto_increment primary key,
page_name varchar(255) not null,
page_title varchar(255) not null,
page_description text not null
);
Third insert records in the table with the below insert statement:
INSERT INTO web_pages(page_name, page_title, page_description) VALUES('about.php', 'About', 'Content for About page - Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.');
INSERT INTO web_pages(page_name, page_title, page_description) VALUES('services.php', 'Services', 'Content for Services page - Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.');
INSERT INTO web_pages(page_name, page_title, page_description) VALUES('product.php', 'Product', 'Content for Product page - Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.');
After successful insertion of above records, create model file 'Article_model.php' in 'application/models/' folder. And write below code in the same file.
<?php
if(!defined('BASEPATH')) exit('Direct script access not allowed');
class Article_model extends CI_Model{
public function __construct(){
$this->load->database();
}
public function get_website_content(){
return $this->db->query("SELECT * FROM web_pages")->result_array();
}
}
?>
Create controller file 'Article.php' under 'application/controllers/' and put the below code in the same file (Article.php).
<?php
if(!defined('BASEPATH')) exit('No direct script access allowed');
class Article extends CI_Controller{
public function __construct(){
parent::__construct();
$this->load->model('article_model');
}
public function show_content(){
if($this->article_model->get_website_content()){
$data['content'] = $this->article_model->get_website_content();
$this->load->view('article', $data);
}
}
}
?>
Create last file 'article.php' under views folder 'application/views/' and mentioned following code in the same file (article.php).
<!DOCTYPE html>
<head>
<title>CodeLifo - MVC pattern demo for Codeigniter</title>
</head>
<body>
<?php
foreach($content as $key=>$val){
foreach($val as $k=>$v){
if($k=='page_title') echo '<strong>'. $v . '</strong>' . "<br>";
if($k=='page_description') echo $v . "<br><br>";
}
}
?>
</body>
</html>
Your whole MVC files are ready you just have to do path setting under routes.php to see result on browser, just mentioned following one line code under 'application/config/routes.php'.
$route['show_content'] = 'article/show_content';
Go to browser and put http://localhost/codeigniter/index.php/show_content and press enter you will see below result.
Hope you guys like above simple steps to make dynamic web page. Have any suggestion or query, please use comment section and I will try respond as soon as possible.
First create database in MySQL. Mentioned syntax in the below:
create database website;
Second create table.
create table web_pages(
page_id int unsigned auto_increment primary key,
page_name varchar(255) not null,
page_title varchar(255) not null,
page_description text not null
);
Third insert records in the table with the below insert statement:
INSERT INTO web_pages(page_name, page_title, page_description) VALUES('about.php', 'About', 'Content for About page - Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.');
INSERT INTO web_pages(page_name, page_title, page_description) VALUES('services.php', 'Services', 'Content for Services page - Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.');
INSERT INTO web_pages(page_name, page_title, page_description) VALUES('product.php', 'Product', 'Content for Product page - Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.');
After successful insertion of above records, create model file 'Article_model.php' in 'application/models/' folder. And write below code in the same file.
<?php
if(!defined('BASEPATH')) exit('Direct script access not allowed');
class Article_model extends CI_Model{
public function __construct(){
$this->load->database();
}
public function get_website_content(){
return $this->db->query("SELECT * FROM web_pages")->result_array();
}
}
?>
Create controller file 'Article.php' under 'application/controllers/' and put the below code in the same file (Article.php).
<?php
if(!defined('BASEPATH')) exit('No direct script access allowed');
class Article extends CI_Controller{
public function __construct(){
parent::__construct();
$this->load->model('article_model');
}
public function show_content(){
if($this->article_model->get_website_content()){
$data['content'] = $this->article_model->get_website_content();
$this->load->view('article', $data);
}
}
}
?>
Create last file 'article.php' under views folder 'application/views/' and mentioned following code in the same file (article.php).
<!DOCTYPE html>
<head>
<title>CodeLifo - MVC pattern demo for Codeigniter</title>
</head>
<body>
<?php
foreach($content as $key=>$val){
foreach($val as $k=>$v){
if($k=='page_title') echo '<strong>'. $v . '</strong>' . "<br>";
if($k=='page_description') echo $v . "<br><br>";
}
}
?>
</body>
</html>
Your whole MVC files are ready you just have to do path setting under routes.php to see result on browser, just mentioned following one line code under 'application/config/routes.php'.
$route['show_content'] = 'article/show_content';
Go to browser and put http://localhost/codeigniter/index.php/show_content and press enter you will see below result.
Hope you guys like above simple steps to make dynamic web page. Have any suggestion or query, please use comment section and I will try respond as soon as possible.

No comments:
Post a Comment