[eluser]sunnyd[/eluser]
I am currently working on a small image organiser. I am using JQuery UI and also using Douglas Crockford json2.js file.
So far I have everything setup properly. Well I hope I have. But when I move an image to a new location, the new image order is not being saved.
database structure:
Code:
-- ----------------------------
-- Table structure for `images`
-- ----------------------------
DROP TABLE IF EXISTS `images`;
CREATE TABLE `images` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`src` varchar(255) NOT NULL,
`alt` varchar(255) DEFAULT NULL,
`order` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=6 DEFAULT CHARSET=latin1;
-- ----------------------------
-- Records of images
-- ----------------------------
INSERT INTO `images` VALUES ('1', 'img1.jpg', null, '1');
INSERT INTO `images` VALUES ('2', 'img2.jpg', null, '2');
INSERT INTO `images` VALUES ('3', 'img3.jpg', null, '3');
INSERT INTO `images` VALUES ('4', 'img4.jpg', null, '4');
INSERT INTO `images` VALUES ('5', 'img5.jpg', null, '5');
Controller Code:
Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
* @property Img_model $img_model
*/
class Images extends Controller {
public function __construct() {
parent::__construct();
$this->load->model('img_model');
// $this->output->enable_profiler(TRUE);
}
public function index() {
$data['images'] = $this->img_model->get_all_images();
$this->load->view('list_images',$data);
}
public function updateOrder() {
$data = file_get_contents('php://input');
$data = json_decode(utf8_decode($data), true);
$images = $data['d'];
foreach ( $images as $key => $value ) {
$id = $value['order'];
$order = $key;
try {
$this->img_model->update_image_order($id , $order);
// $e = $this->db->query('UPDATE images SET order = '. $order . ' WHERE id = ' . $id);
}
catch (Exception $e) {
echo "failed";
}
}
echo "saved";
}
}
Model:
Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class Img_model extends Model {
function __construct(){
parent::__construct();
}
function get_all_images() {
$this->db->order_by('order');
$query = $this->db->get('images');
return $query->result();
}
function update_image_order($id, $order) {
// die($id . ' ' . $order);
$this->db->set('order' , $order + 1, FALSE);
$this->db->where('id' , $id);
$this->db->update('images');
// return true;
// print_r($order);
// print_r($query);
}
}