<?php
class Message_model extends CI_Model {

	public function __construct() {
    	$this->load->database();
  	}
	//SELECT
  public function pic_message($uuid) {
    $this->db->order_by("msg_datetime", "desc"); 
    $query = $this->db->get_where('xi_messages',array('msg_pic' => $uuid));
    return $query->result_array();
  }

  public function pic_count($uuid) {
    $query = $this->db->get_where('xi_messages',array('msg_pic' => $uuid));
    return $query->num_rows();
  }

  public function is_have( $id ) {  //查看某条记录是否存在
    $query = $this->db->get_where('xi_messages',array('ID' => $id));
    return $query->num_rows();
  }

	//INSERT
  public function message($user) { //新增留言
    $now  = date("Y-m-d H:i:s");
    $message = array(
      'msg_text'     => $this->input->post('message'), 
      'msg_pic'    => $this->input->post('view'),
      'msg_user'     => $user,
      'msg_datetime' => $now
    );

    $db = $this->db->insert('xi_messages',$message);
    return $db;
  }

  public function addmessage($view,$text,$username) { //新增留言
    $data = date("Y-m-d H:i:s");
    //$user = $this->session->userdata('Username');
    $message = array(
      'msg_text'     => $text, 
      'msg_pic'      => $view,
      'msg_user'     => $username,
      'msg_datetime' => $data
    );

    $db = $this->db->insert('xi_messages',$message);
    return $db;
  }
	//UPDATE
	//DELETE
  public function delete( $id ) {
    $this->db->delete('xi_messages', array('ID' => $id)); 
  }
}
?>