Thursday, August 18, 2016

create crud web page for movie

first create database named ci
create table movie with attributes id(int 10),title varchar(50),genre varchar(50).

in config folder in autoload.php file change lines as following .

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

we use helpers url and form.
$autoload['helper'] = array('url','form');



in config.php change as following
$config['base_url'] = 'http://localhost/codi';
here codigniter folder name as  codi.


for add and get  data 

create site_model.php in model
create add function to add data and get data
<?php

class Site_model extends CI_Model
{

function add($data)
{
$this->db->insert('movie',$data);
return;
}
function get(){

$query=$this->db->get('movie');
return $query->result();
}
}
?>
in controller create movie.php

<?php
class Movie extends CI_Controller
{

function __construct()
{
parent::__construct();
$this->load->model('site_model');
}
function index(){
if($query=$this->site_model->get()){
$data['records']=$query;
}

$this->load->view('movie_view',$data);
}
function create(){

$data=array(
'id'=>$this->input->post('id'),
'title'=>$this->input->post('title'),
'genre'=>$this->input->post('genre')
);
$this->site_model->add($data);
$this->index();
}
}

 ?>
here constructure  use to load site_model.php file.it can also do it by changing autoload .php file line as following
$autoload['model'] = array('site_model');

here index function use to handling display data.
function create use to store  data in database 

for the user input and display the existing data in database  create movie_view.php in
view
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<h1>add new movie</h1>
<?php echo form_open('movie/create'); ?>
<p>
<label for ='id'>ID : </label>
<input type="text" name="id" id="id">
</p>
<p>
<label for ='title'>title : </label>
<input type="text" name="title" id="title">
</p>
<p>
<label for ='genre'>genre : </label>
<input type="text" name="genre" id="genre">
</p>
<p><input type="submit" name="submit"></p>
<?php echo form_close(); ?>
<h2>list</h2>
<?php if(isset($records)):foreach ($records as $row ):  ?>
<?php echo $row->title ; ?>
<?php echo $row->genre; ?>
<?php endforeach; ?>
<?php else: ?>no records
<?php endif;; ?>

</body>
</html>



we give the following path for routing

http://localhost/codi/movie























it gives output as above

Monday, June 20, 2016

Routing in model

Routing in  model

Here we rout files in model folder.1
First we create New_model .php file in model.
<?php
 class New_model extends CI_Model{
public function getProfile($name)
                {
                                return array("fulname"=>"jack sparrow","age"=>40);
                }
 }
Here we create array and return it when the getProfile method is calling.
In controller we create hello.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Hello extends CI_Controller
{
                public function first($value)
                {
                                $this ->load->model("new_model");
                                $profile =$this->new_model->getProfile("jack");
                                print_r($profile);
                }
}
?>
We can route new_model.php as above mentioned.
We should use following url.
then the output is Array ( [fulname] => jack sparrow [age] => 40 )
if we want to use following url
http://localhost/codi/index.php/hello/first we have to define method as follows.
public function first()
then output as same
create first.php in views
<h1>this is the first in view</h1>
<fieldset>
<legend>detail</legend>
hello ,<?php echo $profile["fulname"];
echo "<br>";
echo "age is ".$profile["age"];

 ?></fieldset>
                Change function first in hello.php as follows.
public function first($value)
                {
                                $this ->load->model("new_model");
                                $profile =$this->new_model->getProfile("jack");
                                $data['profile']=$profile;
                                $this -> load ->view('first',$data);
                }
then output is follow.




Database connect and retrieve data

Database connect and retrieve data.

First we have to change data in  autoload.php in config folder as follows.
$autoload['libraries'] = array('database');
Next change database.php file data as follows.
'hostname' => 'localhost',
'username' => 'root',
'password' => '',
'database' => 'mydb',
Here host is localhost ,user is root user, default password for user is no password,I creat database as mydb.
In mydb database there is a table name users with three attributes id,fname,email.

We retrieve data according to following steps.
Create  Model.php file in model folder
<?php
class User_model extends CI_Model{

                public function getFirstName(){
                                $query=$this->db->query("SELECT fname FROM users");
                                return $query->result();
                }
}
?>
Then create Message.php in views folder
<!DOCTYPE html>
<html>
<head>
                <title>403 Forbidden</title>
</head>
<body>
<p>Directory access is forbidden.</p>
</body>
</html>

Now create Hello2.php in controller
<?php
class Hello2 extends CI_Controller{

                public function index()
                {

                                $this->home();
                }

                public function home()
                {
                                $this-> load->model('user_model');
                                $data['names']=$this->user_model->getFirstName();
                                $this->load->view('message',$data);
                }
}

?>
Now edit the welcome.php in controller
$this->load->view('welcome_message');
To
$this->load->view('message');

I already  insert data to mydb database users table.
We can retrieve data by using following url
output will be
Here database details included sql file.



-- phpMyAdmin SQL Dump
-- version 4.3.11
-- http://www.phpmyadmin.net
--
-- Host: 127.0.0.1
-- Generation Time: Jun 20, 2016 at 06:34 PM
-- Server version: 5.6.24
-- PHP Version: 5.6.8

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;

--
-- Database: `mydb`
--

-- --------------------------------------------------------

--
-- Table structure for table `users`
--

CREATE TABLE IF NOT EXISTS `users` (
  `id` int(50) NOT NULL,
  `fname` varchar(50) NOT NULL,
  `email` varchar(50) NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1;

--
-- Dumping data for table `users`
--

INSERT INTO `users` (`id`, `fname`, `email`) VALUES
(1, 'nalaka', 'nalaka@gmail.com'),
(2, 'sarath', 'sarath@gmail.com');

--
-- Indexes for dumped tables
--

--
-- Indexes for table `users`
--
ALTER TABLE `users`
  ADD PRIMARY KEY (`id`);

--
-- AUTO_INCREMENT for dumped tables
--

--
-- AUTO_INCREMENT for table `users`
--
ALTER TABLE `users`
  MODIFY `id` int(50) NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=3;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;

/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

Thursday, June 2, 2016

how to routing in codeigniter

date:2016.06.03
we can route in codeigniter framework, from controller or from view

  • generally we route the  class from controller .
considere a php file in controller as hello.inside that class we make to three functions index,first,second with parameters $p1, $p2.index function is specific function.we can call it using class name.

public function index()
    {
        echo "index method";
    } 

this is the index function. we can call this function
  1.  http://localhost/codi/index.php/hello  or
  2. http://localhost/codi/index.php/hello/index
in both cases output output will be
index method  

other functions we create in hello.php can't call by using first url.
public function first()
    {
        echo "first method";
     }


we can call this function using http://localhost/codi/index.php/hello/first  url.
output will be
first method 


public function second($p1,$p2)
    {
        echo "second  method<br>";
        echo "my parameters are : $p1 and $p2";
    } 

  
we call the second function using http://localhost/codi/index.php/hello/second/one/two url
 output will be

second method 
my parameters are : one and two

when we calling the second function we have to give values for parameter p1 and p2.
if we don't give values or we give only one value for parameter it will show error.
we can avoid this when we defining the function we can give default values second2($p1="one",$p2="two")
if we don't give values for parameters it there will not be any errors because we already defined default value for parameters.
  http://localhost/codi/index.php/hello/second2/ten
then output will be
 second method 
my parameters are : ten and two