SlideShare a Scribd company logo
View/add/edit/delete images in
CodeIgniter
20. CodeIgniter edit images
20. CodeIgniter edit images
20. CodeIgniter edit images
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>

More Related Content

PDF
MEAN Stack
PPTX
Introduction to Sharding
PDF
High Performance PL/SQL
PPTX
AlexNet
PDF
MySQL Administrator 2021 - 네오클로바
PDF
Virtual machine and javascript engine
PDF
Nabil Nawaz Oracle Oracle 12c Data Guard Deep Dive Presentation
PDF
MySQL Multi-Source Replication for PL2016
MEAN Stack
Introduction to Sharding
High Performance PL/SQL
AlexNet
MySQL Administrator 2021 - 네오클로바
Virtual machine and javascript engine
Nabil Nawaz Oracle Oracle 12c Data Guard Deep Dive Presentation
MySQL Multi-Source Replication for PL2016

What's hot (20)

PDF
Optimize and Simplify Oracle 12C RAC using dNFS, ZFS and OISP
PPTX
Oracle dba training
PDF
MySQL Performance Schema in 20 Minutes
PPTX
Basic Concept of Node.js & NPM
PDF
Model-Based Testing: Why, What, How
PDF
How to Design Indexes, Really
PDF
MySQL Cluster: El ‘qué’ y el ‘cómo’.
PDF
Defending against Java Deserialization Vulnerabilities
PDF
SQL Monitoring in Oracle Database 12c
PPTX
Introduction to MERN
PDF
Understanding Oracle RAC 12c Internals OOW13 [CON8806]
PPTX
MySql:Introduction
PDF
sqlmap - security development in Python
PPT
Javascript
PPTX
OWASP AppSecCali 2015 - Marshalling Pickles
PPTX
What is SQL Server?
PPTX
An Enterprise Architect's View of MongoDB
PDF
MySQL Data Encryption at Rest
PPTX
Express js
PDF
MySQL Performance for DevOps
Optimize and Simplify Oracle 12C RAC using dNFS, ZFS and OISP
Oracle dba training
MySQL Performance Schema in 20 Minutes
Basic Concept of Node.js & NPM
Model-Based Testing: Why, What, How
How to Design Indexes, Really
MySQL Cluster: El ‘qué’ y el ‘cómo’.
Defending against Java Deserialization Vulnerabilities
SQL Monitoring in Oracle Database 12c
Introduction to MERN
Understanding Oracle RAC 12c Internals OOW13 [CON8806]
MySql:Introduction
sqlmap - security development in Python
Javascript
OWASP AppSecCali 2015 - Marshalling Pickles
What is SQL Server?
An Enterprise Architect's View of MongoDB
MySQL Data Encryption at Rest
Express js
MySQL Performance for DevOps
Ad

Similar to 20. CodeIgniter edit images (20)

PPTX
15. CodeIgniter editarea inregistrarilor
PPTX
21. CodeIgniter search
PPTX
19. CodeIgniter imagini in mysql
PPTX
16. CodeIgniter stergerea inregistrarilor
PDF
Silex Cheat Sheet
PDF
Silex Cheat Sheet
KEY
Lithium Best
PPTX
23. CodeIgniter sessions
PDF
ZF2 for the ZF1 Developer
PPTX
8. vederea inregistrarilor
ODP
Zend Framework 1.9 Setup & Using Zend_Tool
PDF
Building Lithium Apps
ODP
CodeIgniter PHP MVC Framework
PPTX
Routing in Drupal 8
PDF
Codeigniter : Two Step View - Concept Implementation
PDF
関西PHP勉強会 php5.4つまみぐい
PDF
Keeping it Small: Getting to know the Slim Micro Framework
PPTX
Magento Live Australia 2016: Request Flow
PDF
I Phone On Rails
PDF
Bag Of Tricks From Iusethis
15. CodeIgniter editarea inregistrarilor
21. CodeIgniter search
19. CodeIgniter imagini in mysql
16. CodeIgniter stergerea inregistrarilor
Silex Cheat Sheet
Silex Cheat Sheet
Lithium Best
23. CodeIgniter sessions
ZF2 for the ZF1 Developer
8. vederea inregistrarilor
Zend Framework 1.9 Setup & Using Zend_Tool
Building Lithium Apps
CodeIgniter PHP MVC Framework
Routing in Drupal 8
Codeigniter : Two Step View - Concept Implementation
関西PHP勉強会 php5.4つまみぐい
Keeping it Small: Getting to know the Slim Micro Framework
Magento Live Australia 2016: Request Flow
I Phone On Rails
Bag Of Tricks From Iusethis
Ad

More from Razvan Raducanu, PhD (20)

PPTX
12. edit record
PPTX
11. delete record
PPTX
10. view one record
PPTX
9. add new record
PPTX
PPTX
6. hello popescu 2
PPTX
5. hello popescu
PPTX
4. forme in zend framework 3
PPTX
3. trimiterea datelor la vederi
PPTX
2.routing in zend framework 3
PPTX
1. zend framework intro
PPTX
18. images in symfony 4
PPTX
17. delete data
PPTX
16. edit data
PPTX
15. view single data
PPTX
14. add data in symfony4
PPTX
13. view data
PPTX
12.doctrine view data
PPTX
11. move in Symfony 4
PPTX
10. add in Symfony 4
12. edit record
11. delete record
10. view one record
9. add new record
6. hello popescu 2
5. hello popescu
4. forme in zend framework 3
3. trimiterea datelor la vederi
2.routing in zend framework 3
1. zend framework intro
18. images in symfony 4
17. delete data
16. edit data
15. view single data
14. add data in symfony4
13. view data
12.doctrine view data
11. move in Symfony 4
10. add in Symfony 4

Recently uploaded (20)

PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PDF
Basic Mud Logging Guide for educational purpose
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PPTX
Cell Structure & Organelles in detailed.
PDF
Insiders guide to clinical Medicine.pdf
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PDF
Computing-Curriculum for Schools in Ghana
PPTX
Pharma ospi slides which help in ospi learning
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
Anesthesia in Laparoscopic Surgery in India
PPTX
PPH.pptx obstetrics and gynecology in nursing
PDF
Pre independence Education in Inndia.pdf
PDF
TR - Agricultural Crops Production NC III.pdf
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PDF
Sports Quiz easy sports quiz sports quiz
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
Basic Mud Logging Guide for educational purpose
O7-L3 Supply Chain Operations - ICLT Program
Abdominal Access Techniques with Prof. Dr. R K Mishra
Cell Structure & Organelles in detailed.
Insiders guide to clinical Medicine.pdf
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
Renaissance Architecture: A Journey from Faith to Humanism
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
Computing-Curriculum for Schools in Ghana
Pharma ospi slides which help in ospi learning
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Anesthesia in Laparoscopic Surgery in India
PPH.pptx obstetrics and gynecology in nursing
Pre independence Education in Inndia.pdf
TR - Agricultural Crops Production NC III.pdf
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
FourierSeries-QuestionsWithAnswers(Part-A).pdf
Sports Quiz easy sports quiz sports quiz

20. CodeIgniter edit images