http://avenir.ro/
Tuesday, March 29, 2016
Monday, March 28, 2016
Lesson 3 : Interacting with database
Go to application>config>database.php
Change name of the database, username and the password.
Create a
Controller : post.php
Model : postmodel.php
View : postview.php
post.php
========
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Post extends CI_Controller {
public function posts(){
$this->load->database();
$this->load->model('Postmodel');
$posts=$this->Postmodel->get_posts();
$view_params['posts']=$posts;
$view_params['mega_title']="Posts";
$this->load->view('postview',$view_params);
}
}
postmodel.php
=============
<?php
class Postmodel extends CI_Model {
function __construct()
{
// Call the Model constructor parent::__construct();
parent::__construct();
}
function get_posts()
{
$query = $this->db->get('posts');
return $query->result();
}
}
postview.php
============
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title><?php echo $mega_title ?></title>
</head>
<body>
<table>
<tr>
<td>Title</td>
<td>Content</td>
</tr>
<?php foreach ($posts as $post): ?>
<tr>
<td><?php echo $post->postTitle ?></td>
<td><?php echo $post->postBody ?></td>
</tr>
<?php endforeach; ?>
</body>
</html>
Change name of the database, username and the password.
Create a
Controller : post.php
Model : postmodel.php
View : postview.php
post.php
========
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Post extends CI_Controller {
public function posts(){
$this->load->database();
$this->load->model('Postmodel');
$posts=$this->Postmodel->get_posts();
$view_params['posts']=$posts;
$view_params['mega_title']="Posts";
$this->load->view('postview',$view_params);
}
}
postmodel.php
=============
<?php
class Postmodel extends CI_Model {
function __construct()
{
// Call the Model constructor parent::__construct();
parent::__construct();
}
function get_posts()
{
$query = $this->db->get('posts');
return $query->result();
}
}
postview.php
============
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title><?php echo $mega_title ?></title>
</head>
<body>
<table>
<tr>
<td>Title</td>
<td>Content</td>
</tr>
<?php foreach ($posts as $post): ?>
<tr>
<td><?php echo $post->postTitle ?></td>
<td><?php echo $post->postBody ?></td>
</tr>
<?php endforeach; ?>
</body>
</html>
Lesson 2 : Passing data to controller from the URL
Create a controller file named 'lessontwo.php' with the following code:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Lessontwo extends CI_Controller {
public function index(){
echo "<h1> Welcome to Lesson Two.</h1>
}
public function quote($a){
$quotes['einstein']=
"The world is a dangerous place to live; not because of the
people who are evil, but because of the people who don't do
anything about it. Albert Einstein.";
$quotes['shakespeare']="Love all, trust a few, do wrong to none. William Shakespeare.";
$quotes['twain']="All you need in this life is ignorance and confidence, and then success is sure. Mark Twain.";
switch ($a) {
case 'einstein':
$data['quote']=$quotes['einstein'];
break;
case 'shakespeare':
$data['quote']=$quotes['shakespeare'];
break;
case 'twain':
$data['quote']=$quotes['twain'];
break;
}
$this->load->view('lessontwoview',$data);
}
}
Next, create a view file 'lessontwoview.php' with following code:
<h1>
<?php echo $quote ?>
</h1>
Point to url: http://localhost/codi/lessontwo/quote/shakespeare
You should see the quote by shakespeare.
Change shakespeare to twain, you get Mark Twain's quote. Cool huh!
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Lessontwo extends CI_Controller {
public function index(){
echo "<h1> Welcome to Lesson Two.</h1>
}
public function quote($a){
$quotes['einstein']=
"The world is a dangerous place to live; not because of the
people who are evil, but because of the people who don't do
anything about it. Albert Einstein.";
$quotes['shakespeare']="Love all, trust a few, do wrong to none. William Shakespeare.";
$quotes['twain']="All you need in this life is ignorance and confidence, and then success is sure. Mark Twain.";
switch ($a) {
case 'einstein':
$data['quote']=$quotes['einstein'];
break;
case 'shakespeare':
$data['quote']=$quotes['shakespeare'];
break;
case 'twain':
$data['quote']=$quotes['twain'];
break;
}
$this->load->view('lessontwoview',$data);
}
}
Next, create a view file 'lessontwoview.php' with following code:
<h1>
<?php echo $quote ?>
</h1>
Point to url: http://localhost/codi/lessontwo/quote/shakespeare
You should see the quote by shakespeare.
Change shakespeare to twain, you get Mark Twain's quote. Cool huh!
Miscellaneous 1
Create a file named .htaccess in the project root directory. In our case project root is codi. Type the following code in the file and save it.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
<Files "index.php">
AcceptPathInfo On
</Files>
Now restart the server.
Point your browser to http://localhost/codi/quotes. You shoud see the quote we used in Lesson 1. No longer it is required to type index.php in every URL.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
<Files "index.php">
AcceptPathInfo On
</Files>
Now restart the server.
Point your browser to http://localhost/codi/quotes. You shoud see the quote we used in Lesson 1. No longer it is required to type index.php in every URL.
Lesson 1 : Creating a Controller and View
Lets create a controller and a view.
In the Application/controllers folder, create a file named quotes.php and type the following code the file:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Quotes extends CI_Controller {
public function index(){
echo "When you are courting a nice girl an hour seems like a second. When you sit on a red-hot cinder a second seems like an hour. That's relativity. <h3>Albert Einstein</h3>";
}
}
In the Application/controllers folder, create a file named quotes.php and type the following code the file:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Quotes extends CI_Controller {
public function index(){
echo "When you are courting a nice girl an hour seems like a second. When you sit on a red-hot cinder a second seems like an hour. That's relativity. <h3>Albert Einstein</h3>";
}
}
Point your browser to: http://localhost/codi/index.php/quotes. You should a see a quote you just typed.
But, we didn't make use of a view file. Let's create one: quotesview.php with following code:
<?php echo $quotes ?>
Edit the controller file like so:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Quotes extends CI_Controller {
public function index(){
$quotes = "When you are courting a nice girl an hour seems like a second. When you sit on a red-hot cinder a second seems like an hour. That's relativity. <h3>Albert Einstein</h3>";
$data['quotes']=$quotes;
$this->load->view('quotesview',$data);
}
}
Point your browser to: http://localhost/codi/index.php/quotes. You should the same quote.
This time we called the view file quotesview and passed the parameter named $data, and used printed the same with echo: <?php echo $quotes ?>
<?php echo $quotes ?> equals to <?= $quotes ?>.
This time we called the view file quotesview and passed the parameter named $data, and used printed the same with echo: <?php echo $quotes ?>
<?php echo $quotes ?> equals to <?= $quotes ?>.
You could also use <?= $quotes ?>
You must have observed that everytime we point to a URL, we have to include index.php. In the next post we shall do something so that we don't have to type index.php every time.
Installing Codeigniter
I am working on a Windows 7 PC.
Download CodeIgniter-3.0.6.zip (installer) from https://www.codeigniter.com/
Unzip the installer in the root folder of your local server. I am using XAMPP. So I placed the zip file in the XAMPP/htdocs folder.
Unzip the file in CodeIgniter-3.0.6 folder. Rename the folder according to your project name. I renamed it to 'codi'.
Start XAMP server. Point your browser to the URL http://localhost/codi/. You should see the following screen.
Congratulations! You have successfully installed CodeIgniter.
Download CodeIgniter-3.0.6.zip (installer) from https://www.codeigniter.com/
Unzip the installer in the root folder of your local server. I am using XAMPP. So I placed the zip file in the XAMPP/htdocs folder.
Unzip the file in CodeIgniter-3.0.6 folder. Rename the folder according to your project name. I renamed it to 'codi'.
Start XAMP server. Point your browser to the URL http://localhost/codi/. You should see the following screen.
Congratulations! You have successfully installed CodeIgniter.
Friday, March 11, 2016
base_url
base_url() not working
Go to application>config>autoload.php. Find the $autoload['helper'] = array();
Add 'url' in the array() so that it looks like this:
$autoload['helper'] = array('url');
Go to application>config>autoload.php. Find the $autoload['helper'] = array();
Add 'url' in the array() so that it looks like this:
$autoload['helper'] = array('url');
Subscribe to:
Posts (Atom)
