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