Wednesday, April 20, 2016
Sunday, April 17, 2016
FAQ
If you have problem with base_url(), try this in application>config>config.php:
$config['base_url'] = "http://".$_SERVER['HTTP_HOST']."/codi/";
name of model class and model file name must be same[exactly same]
Ref: user_guide: user_guide/general/models.html?highlight=model
Executing raw SQL:
public function deactivate($id){
$sql='update quotes set active=0 where aid='.$id;
$this->db->query($sql);
}
$config['base_url'] = "http://".$_SERVER['HTTP_HOST']."/codi/";
name of model class and model file name must be same[exactly same]
Ref: user_guide: user_guide/general/models.html?highlight=model
Executing raw SQL:
public function deactivate($id){
$sql='update quotes set active=0 where aid='.$id;
$this->db->query($sql);
}
Thursday, April 14, 2016
Automatic Table Printer
All you need to do is copy the following code to your view file and call the view from controller and pass the table data as todos. i.e. $data['todos']=$query->result. $this->load->view('view',$data).
<?php
echo "<table>";
//print table headers
if($todos){
echo "<thead>";
foreach($todos[0] as $key=>$val){
echo "<td> $key </td>";
}
echo "</thead>";
//print data
foreach($todos as $todo){
echo "<tr>";
foreach($todo as $key => $val){
echo "<td> $val </td>";
}
echo "<tr>";
}
}else{
echo "<h1>Sorry, No Todo is available</h1>";
}
?>
</table>
Friday, April 8, 2016
Form Validation
Controller
=======
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Config extends CI_Controller {
public function index()
{
if($this->input->post('submit')){
$this->form_validation->set_rules('emp_type','Employee Type','trim|required|min_length[9]');
if($this->form_validation->run()==FALSE)
{
$data=array(
'errors'=> validation_errors()
);
$this->session->set_flashdata($data);
}
redirect('config');
} else{
$this->load->view('config_view');
}
}
}
View
====
<?php echo form_open('config/index');
if($this->session->flashdata('errors')){
echo $this->session->flashdata('errors');
}
echo form_label('Employee Type','emp_type');
echo form_input(array('name' => 'emp_type',
'class' => 'form-control',
'placeholder'=>'Employee Type'
));
echo form_submit('submit','Save',array('class' => 'form-control'));
echo form_close();
?>
=======
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Config extends CI_Controller {
public function index()
{
if($this->input->post('submit')){
$this->form_validation->set_rules('emp_type','Employee Type','trim|required|min_length[9]');
if($this->form_validation->run()==FALSE)
{
$data=array(
'errors'=> validation_errors()
);
$this->session->set_flashdata($data);
}
redirect('config');
} else{
$this->load->view('config_view');
}
}
}
View
====
<?php echo form_open('config/index');
if($this->session->flashdata('errors')){
echo $this->session->flashdata('errors');
}
echo form_label('Employee Type','emp_type');
echo form_input(array('name' => 'emp_type',
'class' => 'form-control',
'placeholder'=>'Employee Type'
));
echo form_submit('submit','Save',array('class' => 'form-control'));
echo form_close();
?>
Thursday, April 7, 2016
Create a Simple API
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class api extends CI_Controller {
public function index($secretkey)
{
//api url http://dilir.me/codi/api/index/916
if(isset($secretkey) && $secretkey==='916'){
$data= json_encode(array(
"status" => true,
"name" => "Dilir",
"mail" => "dilir.khan@example.com",
"quote" =>"Try Simple Solution First"
));
}else{
$data=json_encode(array(
"status" => false
));
}
header('Content-Type: application/json');
echo $data;
}
}
defined('BASEPATH') OR exit('No direct script access allowed');
class api extends CI_Controller {
public function index($secretkey)
{
//api url http://dilir.me/codi/api/index/916
if(isset($secretkey) && $secretkey==='916'){
$data= json_encode(array(
"status" => true,
"name" => "Dilir",
"mail" => "dilir.khan@example.com",
"quote" =>"Try Simple Solution First"
));
}else{
$data=json_encode(array(
"status" => false
));
}
header('Content-Type: application/json');
echo $data;
}
}
Creating a API Example
public function api(){
$app = array();
$app['status']=false;
//url: dilir.me/api.php?auth=abraCadabra
//Check API key for security
if(isset($_GET['auth']) && $_GET['auth']==='abraCadabra'){
$app['status']=true;
$app['name']="Dilir";
$app['mail']="dilir.khan@example.com";
$app['quote']="Try Simple Solution First";
}else{
unset($app);
$app['status']=false;
}
header('Content-Type: application/json');
echo json_encode($app);
}
$app = array();
$app['status']=false;
//url: dilir.me/api.php?auth=abraCadabra
//Check API key for security
if(isset($_GET['auth']) && $_GET['auth']==='abraCadabra'){
$app['status']=true;
$app['name']="Dilir";
$app['mail']="dilir.khan@example.com";
$app['quote']="Try Simple Solution First";
}else{
unset($app);
$app['status']=false;
}
header('Content-Type: application/json');
echo json_encode($app);
}
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)
