<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Calendar_model extends CI_Model {
/*Read the data from DB */
Public function getEvents()
{
/*$sql = $this->db->select('*')
->where('events.date', '$start')
->where('events.date', '$end' )
->order_by('events.date','asc')
->get('events');*/
$sql = "SELECT * FROM events WHERE events.date BETWEEN ? AND ? ORDER BY events.date ASC";
return $this->db->query($sql, array($_GET['start'], $_GET['end']))->result();
}
/*Create new events */
Public function addEvent()
{
$sql = array(
'title' => $this->input->post('title'),
'events.date' => $this->input->post('date'),
'description' => $this->input->post('description'),
'color' => $this->input->post('color')
);
/*$sql = "INSERT INTO events (title,events.date, description, color) VALUES (?,?,?,?)";
$this->db->query($sql, array($_POST['title'], $_POST['date'], $_POST['description'], $_POST['color']));*/
/*$insert_status += $this->db->affected_rows();
return $insert_status > 0 ? TRUE : FALSE;*/
$this->db->insert('events', $sql);
return ($this->db->affected_rows()!=1)?false:true;
}
/*Update event */
Public function updateEvent()
{
$data = array(
'title' => $this->input->post('title'),
'events.date' => $this->input->post('date'),
'description' => $this->input->post('description'),
'color' => $this->input->post('color')
);
$this->db->where('id', $id);
$this->db->update('events', $data);
/*$sql = "UPDATE events SET title = ?, events.date = ?, description = ?, color = ? WHERE id = ?";
$this->db->query($sql, array($_POST['title'], $_POST['date'], $_POST['description'], $_POST['color'], $_POST['id']));*/
return ($this->db->affected_rows()!=1)?false:true;
}
/*Delete event */
Public function deleteEvent()
{
$this->db->delete('events', array('id' => $this->input->get('id')));
/*$sql = "DELETE FROM events WHERE id = ?";
$this->db->query($sql, array($_GET['id']));*/
return ($this->db->affected_rows()!=1)?false:true;
}
/*Update event */
Public function dragUpdateEvent()
{
$date=date('Y-m-d h:i:s',strtotime($_POST['date']));
$sql = "UPDATE events SET events.date = ? WHERE id = ?";
$this->db->query($sql, array($date, $_POST['id']));
return ($this->db->affected_rows()!=1)?false:true;
}
}
?>
หน้า controller ครับCode (PHP)
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Calendar extends CI_Controller {
function __construct()
{
// Call the Model constructor
parent::__construct();
$this->load->model('Calendar_model');
}
/*Home page Calendar view */
Public function index()
{
$this->load->view('home');
}
/*Get all Events */
Public function getEvents()
{
$result=$this->Calendar_model->getEvents();
echo json_encode($result);
}
/*Add new event */
Public function addEvent()
{
$result=$this->Calendar_model->addEvent();
echo $result;
}
/*Update Event */
Public function updateEvent()
{
$result=$this->Calendar_model->updateEvent();
echo $result;
}
/*Delete Event*/
Public function deleteEvent()
{
$result=$this->Calendar_model->deleteEvent();
echo $result;
}
Public function dragUpdateEvent()
{
$result=$this->Calendar_model->dragUpdateEvent();
echo $result;
}
}
คือโค๊ดสามารถ runได้ปกติกับ mysql ครับ แต่พอผมเปลี่ยนมาเป็น sqlserver 2012 (sqlsrv)