download
http://localhost/CodeIgniter/index.php/Welcome/update
Code:
Controller file: welcome.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Welcome extends CI_Controller {
public function __construct()
{
// Call the CI_Model constructor
parent::__construct();
$this->load->model('data_model');
}
/**
* Index Page for this controller.
*
* Maps to the following URL
* http://example.com/index.php/welcome
* - or -
* http://example.com/index.php/welcome/index
* - or -
* Since this controller is set as the default controller in
* config/routes.php, it's displayed at http://example.com/
*
* So any other public methods not prefixed with an underscore will
* map to /index.php/welcome/<method_name>
* @see http://codeigniter.com/user_guide/general/urls.html
*/
public function index()
{
$data['data'] = $this->data_model->main_sticker_cat_list();
// $data['data']="a";
// print_r($data);
$this->load->view('welcome_message',$data);
$response = array('MESSAGE' => "REQUIRED_FIELD_MISSING", 'STATUS' => FALSE);
return $this->output->set_output(json_encode(array("JSON_ROOT_OBJECT" => $response)));
}
public function addedit()
{
// echo "hi";
$id = $_GET["id"];
$data['users_list'] = $this->data_model->main_sticker_cat_list2($id);
// print_r($data);
$this->load->view('update_form',$data);
}
public function delete()
{
$this->load->view('welcome_message');
}
public function update()
{
$id = $this->input->post('id');
$alink = trim($this->input->post('alink'));
//print_r($_FILES);
$this->db->select('image');
$this->db->from('demo');
$this->db->where_in('id', $id);
$query = $this->db->get();
foreach ($query->result() as $user) {
$old_thumb = $user->image;
$old_puzzle_file = $user->image;
}
$thumb_img = $_FILES['thumb_img']['name'];
$thumb_img_type = $_FILES['thumb_img']['type'];
$thumb_img_size = $_FILES['thumb_img']['size'];
$thumb_img_name = $_FILES['thumb_img']['tmp_name'];
// echo $thumb_img;
$base="localhost/CodeIgniter";
if ($thumb_img != "") {
// echo "hi";
$thumb_img_imgSize = @getImageSize($thumb_img_name);
$thumb_img_width = $thumb_img_imgSize[0];
$thumb_img_height = $thumb_img_imgSize[1];
if ( (($thumb_img_type == "image/x-png") || ($thumb_img_type == "image/png")||($thumb_img_type == "image/jpeg"))) {
//echo $old_thumb;
$file_name=rand();
// $file_name.=
$new_file_name = str_replace($base . 'image/', '', $thumb_img);
$thumb_path = "public/image/" . $new_file_name;
echo $thumb_path;
if (copy($_FILES['thumb_img']['tmp_name'], "" . $thumb_path)) {
// echo "hi";
//date_default_timezone_set("" . SERVER_DEFAULT_TIMEZONE . "");
$time = $dateNow = date('Y-m-d H:i:s', time());
$thumb_path = $base . $thumb_path;
$data['thumb_image_url'] = $thumb_path;
$data['time'] = $time;
$this->db->where('id', $id);
$data = array(
'firstname' => $alink,
'image'=>$new_file_name
);
$this->db->update('demo', $data);
echo "image upload successfully";
$response = array(MESSAGE => "REQUIRED_FIELD_MISSING", STATUS => FALSE);
return $this->output->set_output(json_encode(array(JSON_ROOT_OBJECT => $response)));
//redirect('welcome');
} else {
echo "2";
// $_SESSION['not_change'] = "<div style='color:#ff0000; text-align: center; font-size: 13px;'>Puzzle thumbnail uploading fail, please try again</div>";
// redirect('puzzles/edit_puzzle?row_no=' . $row_no . '');
}
} else {
echo "3";
// $_SESSION['not_change'] = "<div style='color:#ff0000; text-align: center; font-size: 13px;'>Icon must be .png and size must be less then 50 KB and resolution must be 300px X 300px.</div>";
// redirect('puzzles/edit_puzzle?row_no=' . $row_no . '');
}
}
// redirect('welcome/index');
}
}
Model File
data_model
<?php
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
class data_model extends CI_Model
{
public function __construct()
{
// Call the CI_Model constructor
parent::__construct();
$this->load->database();
}
public function main_sticker_cat_list() {
$query = $this->db->get('demo');
return $query->result();
}
public function main_sticker_cat_list2($id) {
$this->db->select('*');
$this->db->from('demo');
$this->db->where('id', $id);
$query = $this->db->get();
//echo $query;
/* $query = $this->db->query("SELECT * FROM i_sticker_category WHERE category_id='$id'"); */
return $query->result();
}
}
?>
http://localhost/CodeIgniter/index.php/Welcome/update
Code:
Controller file: welcome.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Welcome extends CI_Controller {
public function __construct()
{
// Call the CI_Model constructor
parent::__construct();
$this->load->model('data_model');
}
/**
* Index Page for this controller.
*
* Maps to the following URL
* http://example.com/index.php/welcome
* - or -
* http://example.com/index.php/welcome/index
* - or -
* Since this controller is set as the default controller in
* config/routes.php, it's displayed at http://example.com/
*
* So any other public methods not prefixed with an underscore will
* map to /index.php/welcome/<method_name>
* @see http://codeigniter.com/user_guide/general/urls.html
*/
public function index()
{
$data['data'] = $this->data_model->main_sticker_cat_list();
// $data['data']="a";
// print_r($data);
$this->load->view('welcome_message',$data);
$response = array('MESSAGE' => "REQUIRED_FIELD_MISSING", 'STATUS' => FALSE);
return $this->output->set_output(json_encode(array("JSON_ROOT_OBJECT" => $response)));
}
public function addedit()
{
// echo "hi";
$id = $_GET["id"];
$data['users_list'] = $this->data_model->main_sticker_cat_list2($id);
// print_r($data);
$this->load->view('update_form',$data);
}
public function delete()
{
$this->load->view('welcome_message');
}
public function update()
{
$id = $this->input->post('id');
$alink = trim($this->input->post('alink'));
//print_r($_FILES);
$this->db->select('image');
$this->db->from('demo');
$this->db->where_in('id', $id);
$query = $this->db->get();
foreach ($query->result() as $user) {
$old_thumb = $user->image;
$old_puzzle_file = $user->image;
}
$thumb_img = $_FILES['thumb_img']['name'];
$thumb_img_type = $_FILES['thumb_img']['type'];
$thumb_img_size = $_FILES['thumb_img']['size'];
$thumb_img_name = $_FILES['thumb_img']['tmp_name'];
// echo $thumb_img;
$base="localhost/CodeIgniter";
if ($thumb_img != "") {
// echo "hi";
$thumb_img_imgSize = @getImageSize($thumb_img_name);
$thumb_img_width = $thumb_img_imgSize[0];
$thumb_img_height = $thumb_img_imgSize[1];
if ( (($thumb_img_type == "image/x-png") || ($thumb_img_type == "image/png")||($thumb_img_type == "image/jpeg"))) {
//echo $old_thumb;
$file_name=rand();
// $file_name.=
$new_file_name = str_replace($base . 'image/', '', $thumb_img);
$thumb_path = "public/image/" . $new_file_name;
echo $thumb_path;
if (copy($_FILES['thumb_img']['tmp_name'], "" . $thumb_path)) {
// echo "hi";
//date_default_timezone_set("" . SERVER_DEFAULT_TIMEZONE . "");
$time = $dateNow = date('Y-m-d H:i:s', time());
$thumb_path = $base . $thumb_path;
$data['thumb_image_url'] = $thumb_path;
$data['time'] = $time;
$this->db->where('id', $id);
$data = array(
'firstname' => $alink,
'image'=>$new_file_name
);
$this->db->update('demo', $data);
echo "image upload successfully";
$response = array(MESSAGE => "REQUIRED_FIELD_MISSING", STATUS => FALSE);
return $this->output->set_output(json_encode(array(JSON_ROOT_OBJECT => $response)));
//redirect('welcome');
} else {
echo "2";
// $_SESSION['not_change'] = "<div style='color:#ff0000; text-align: center; font-size: 13px;'>Puzzle thumbnail uploading fail, please try again</div>";
// redirect('puzzles/edit_puzzle?row_no=' . $row_no . '');
}
} else {
echo "3";
// $_SESSION['not_change'] = "<div style='color:#ff0000; text-align: center; font-size: 13px;'>Icon must be .png and size must be less then 50 KB and resolution must be 300px X 300px.</div>";
// redirect('puzzles/edit_puzzle?row_no=' . $row_no . '');
}
}
// redirect('welcome/index');
}
}
Model File
data_model
<?php
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
class data_model extends CI_Model
{
public function __construct()
{
// Call the CI_Model constructor
parent::__construct();
$this->load->database();
}
public function main_sticker_cat_list() {
$query = $this->db->get('demo');
return $query->result();
}
public function main_sticker_cat_list2($id) {
$this->db->select('*');
$this->db->from('demo');
$this->db->where('id', $id);
$query = $this->db->get();
//echo $query;
/* $query = $this->db->query("SELECT * FROM i_sticker_category WHERE category_id='$id'"); */
return $query->result();
}
}
?>
Edit form
<?php
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
?>
<html>
<head>
<title>Edit Form</title>
</head>
<body>
<?php
// print_r($users_list);
$id=$users_list[0]->id;
$link=$users_list[0]->firstname;
$img=$users_list[0]->image;
$base="http://localhost/CodeIgniter/public/image/";
?>
<form action="update" method="POST" enctype="multipart/form-data">
<input name="id" type="hidden" value="<?php echo $id; ?> ">
Hyper Link:<input type="text" name="alink" id="alink" value="<?php echo $link?>"><br/><br/>
Image:<input type="file" name="thumb_img" src="<?php echo $base.''.$img?>"><br/><br/>
<img src="<?php echo $base.''.$img?>" width="150px" height="150px">
<button type="submit" class="btn btn-block btn-success" ><span class="semibold">Save</span></button>
</form>
</body>
</html>
<?php
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
?>
<html>
<head>
<title>Edit Form</title>
</head>
<body>
<?php
// print_r($users_list);
$id=$users_list[0]->id;
$link=$users_list[0]->firstname;
$img=$users_list[0]->image;
$base="http://localhost/CodeIgniter/public/image/";
?>
<form action="update" method="POST" enctype="multipart/form-data">
<input name="id" type="hidden" value="<?php echo $id; ?> ">
Hyper Link:<input type="text" name="alink" id="alink" value="<?php echo $link?>"><br/><br/>
Image:<input type="file" name="thumb_img" src="<?php echo $base.''.$img?>"><br/><br/>
<img src="<?php echo $base.''.$img?>" width="150px" height="150px">
<button type="submit" class="btn btn-block btn-success" ><span class="semibold">Save</span></button>
</form>
</body>
</html>