View/add/edit/delete images in
CodeIgniter
application/config/routes.php
$route['default_controller'] =
'MainController/index';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
application/config/autoload.php
$autoload['libraries'] = array('database');
$autoload['helper'] = array('form');
.htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /CodeIgniter2/
RewriteCond %(REQUEST_FILENAME) !-f
RewriteCond %(REQUEST_FILENAME) !-d
RewriteCond $1 !^(index.php|images|robots.txt)
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
/config/database.php
db['default'] = array(
'dsn' => '',
'hostname' => 'localhost',
'username' => 'root',
'password' => '',
'database' => 'images',
'dbdriver' => 'mysqli',
'dbprefix' => '',
………………………………………..
MainController.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class MainController extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->database();
$this->load->model('MainModel','f');
}
public function index()
{
$this->load->view('view');
}
public function view($id=NULL){
$row=$this->f->getImage($id);
$data['r']=$row;
$this->load->view('view_single',$data);
}
public function upload()
{
$this->load->helper('form');
$this->load->view('upload');
}
public function save()
{
$url=$this->do_upload();
$title=$_POST["title"];
$this->f->save($title, $url);
header('location:http://localhost/CodeIgniter2/index.php');
}
private function do_upload()
{
$type=explode('.',$_FILES["poza"]["name"]);
$type=$type[count($type)-1];
$url="./images/".$_FILES["poza"]["name"];
if(in_array($type,array("jpg","jpeg","gif","png")))
if(is_uploaded_file($_FILES["poza"]["tmp_name"]))
if(move_uploaded_file($_FILES["poza"]["tmp_name"],
$url))
return $url;
return "";
}
public function delete($id){
$id=$this->db->where('id',$id);
$this->db->delete('images');
header('location:http://localhost/CodeIgniter2/inde
x.php');
//redirect('MainController/index');
}
public function edit($id){
$row=$this->f->getImage($id);
$data['r']=$row;
$this->load->view('edit_view',$data);
}
public function update(){
$id=$this->input->post('id');
//create array with input data
$data=array(
'title'=>$this->input->post('title'),
'image'=>"./images/".$_FILES["poza"]["name"]
);
$title=$this->input->post('title');
$image="./images/".$_FILES["poza"]["name"];
move_uploaded_file($_FILES["poza"]["tmp_name"], $image);
//update data
// $this->f->update($title,$image,$id);
$this->db->where('id',$id);
$this->db->update('images',$data);
//redirect
header('location:http://localhost/CodeIgniter2/index.php');
}
}
MainModel.php
<?php
class MainModel extends CI_Model
{
public function __construct()
{
parent::__construct();
}
public function save($title,$url)
{
$this->db->set('title',$title);
$this->db->set('image',$url);
$this->db->insert('images');
}
public function update($title,$image,$id){
$this->db->set('title',$title);
$this->db->set('image',$image);
$this->db->where('id',$id);
}
public function getImages(){
$this->db->select('id,title,image')->from('images');
$query = $this->db->get();
return $query->result();
}
function getImage($id){
$this->db->where('id',$id);
$query = $this->db->get('images');
return $query->row();
}
}
edit_view.php
<?php
//$this->load->helper('form');
echo form_open_multipart('MainController/update/');
$data1 = ['name' => 'title',
'id' => 'title',
'value'=>$r->title,
'maxlength' => '100',
'size' => '30',
];
$data2 = ['name' => 'poza',
'id' => 'poza',
'value'=>$r->image,
'maxlength' => '100',
'size' => '30',
];
$data3 = ['name' => 'id',
'id' => 'id',
'type'=>'hidden',
'value'=>$r->id,
'maxlength' => '100',
'size' => '30',
];
?>
<?php echo form_input($data3);?>
<table>
<tr>
<td><?php echo form_label('Title ', 'title');?></td>
<td><?php echo form_input($data1);?></td>
</tr>
<tr>
<td><?php echo form_label('Image ', 'poza');?></td>
<td><?php echo form_upload($data2);?></td>
</tr>
</table>
<?php echo form_submit('submit', 'Update');?>
upload.php
<!DOCTYPE html>
<html lang="en">
<head>
</head>
<body>
<?php echo form_open_multipart('MainController/save'); ?>
<table class="table">
<tr>
<td>Titlu</td>
<td><?php echo form_input('title');?></td>
</tr>
<tr>
<td>Imagine</td>
<td><?php echo form_upload('poza');?></td>
</tr>
<tr>
<td></td>
<td><?php echo form_submit('submit','Save','class="btn btn-primary"')?></td>
</tr>
</table>
</body>
</html>
view.php
<!DOCTYPE html>
<head>
</head>
<body>
<table>
<tr>
<td><strong>Nume</strong></td>
<td><strong>Imagine</strong></td>
<td colspan="3"><strong>Actions</strong></td>
</tr>
<?php foreach($this->f->getImages() as $var){?>
<tr>
<td><?php echo $var->title;?></td>
<td><img src="<?php echo base_url($var->image);?>"
width="100" height="100"></td>
<td><?php echo anchor(array('MainController/view/',$var-
>id),'View');?> </td>
<td><?php echo anchor(array('MainController/edit/',$var-
>id),'Edit');?> </td>
<td><?php echo anchor(array('MainController/delete/',$var-
>id), 'Delete',array('onclick' => "return confirm('Do you want delete
this record')"));?> </td>
</tr>
<?php }?>
</table
<br><br>
<?php echo anchor(array('MainController/upload/'),'Upload another
image'); ?>
</body>
</html>
view_single.php
<h2><?php echo $r->title; ?></h2>
<h2><img src="<?php echo base_url($r->image);?>"
width="100" height="100"></h2>
<a href="<?php echo site_url() ?>">Back</a>

20. CodeIgniter edit images