[Codeigniter-users] 編集について

Back to archive index

牧尾竜一 ryuic****@jom*****
2008年 7月 11日 (金) 18:10:12 JST


JOMRです。

kunitsujiさん、ありがとうございます。
以下のものでテストしてみました(custmer→customerと少しだけかきかえました。)
少し長くなりますが、以下の内容のもので行いました、
<?php

class Customer extends Controller {

	var $limit = 5;

    function Customer()
    {
		parent::Controller();
		$this->load->helper(array('form', 'url'));
		$this->load->database();
    }

	function index($offset = '')
	{
		$offset = (int) $offset;
		$this->db->order_by('cu_id', 'desc');
		$data['query'] = $this->db->get('customer', $this->limit, $offset);
		$this->load->library('pagination');
		$config['base_url']		= $this->config->site_url('/customer/index/');
		$config['total_rows']	= $this->db->count_all('customer');
		$config['per_page']		= $this->limit;
		$config['first_link']	= '&laquo;最初';
		$config['last_link']	= '最後&raquo;';
		
		$this->pagination->initialize($config);
		$data['pagination'] = $this->pagination->create_links();
		$this->load->view('customer_show', $data);
	}


	function post()
	{
		$this->_set_validation();
		$this->_show_post_page();
	}
	
	function _set_validation()
	{
		$this->load->library('validation');
		
		$fields['cu_name']		= '顧客名';
		$fields['cu_kana']		= '顧客名(カナ)';
		$fields['cu_tel']		= '連絡先';
		$fields['cu_fax']		= 'FAX番号';
		$fields['cu_mail']		= 'メールアドレス';
		$fields['biko']			= '備考';
		$fields['captcha']		= '画像認証コード';
		$fields['key']			= 'key';
		$this->validation->set_fields($fields);
		
		$rules['cu_name']		= 'trim|required|max_length[32]';
		$rules['cu_kana']		= 'trim|max_length[32]';
		$rules['cu_tel']		= 'trim|max_length[16]';
		$rules['cu_fax']		= 'trim|max_length[16]';
		$rules['cu_mail']		= 'trim|valid_email|max_length[64]';
		$rules['biko']			= 'trim|max_length[200]';
		$rules['captcha']		= 'trim|required|alpha_numeric|callback_captcha_check';
		$rules['key']			= 'numeric';
		$this->validation->set_rules($rules);
	}
	
	function _show_post_page()
	{
		$this->load->helper('string');
		$this->load->plugin('captcha');
		$vals = array(
					'word'		=> random_string('numeric', 4),
					'img_path'	=> './captcha/',
					'img_url'	=> base_url() . 'captcha/'
					);
		$cap = create_captcha($vals);
		$data = array(
					'captcha_id'	=> '',
					'captcha_time'	=> $cap['time'],
					'word'			=> $cap['word']
					);
		$this->db->insert('captcha', $data);
		$key = $this->db->insert_id();
		
		$data['image']		= $cap['image'];
		$data['key']		= $key;
		$data['cu_name']	= $this->validation->cu_name;
		$data['cu_kana']	= $this->validation->cu_kana;
		$data['cu_tel']		= $this->validation->cu_tel;
		$data['cu_fax']		= $this->validation->cu_fax;
		$data['cu_mail']	= $this->validation->cu_mail;
		$data['biko']		= $this->validation->biko;
		$this->load->view('customer_post', $data);
	}
	
	function captcha_check($str)
	{
		$expiration = time() - 7200; //有効期限2時間
		$this->db->delete('captcha',
						array('captcha_time <' => $expiration));
		$this->db->select("COUNT(*) AS count");
		$this->db->where('word', $str);
		$this->db->where('captcha_id', $this->input->post('key'));
		$this->db->where('captcha_time >', $expiration);
		$query = $this->db->get('captcha');
		$row = $query->row();
		
		if ($row->count == 0)
		{
			$this->validation->set_message('captcha_check', '画像認証コードが一致
しません');
			return FALSE;
		}
		else
		{
			return TRUE;
		}
	}
	
	function confirm()
	{
		$this->_set_validation();
		
		if ($this->validation->run() == FALSE)
		{
			$this->_show_post_page();
		}
		else
		{
			$data['cu_name']	= $this->validation->cu_name;
			$data['cu_kana']	= $this->validation->cu_kana;
			$data['cu_tel']		= $this->validation->cu_tel;
			$data['cu_fax']		= $this->validation->cu_fax;
			$data['cu_mail']	= $this->validation->cu_mail;
			$data['biko']		= $this->validation->biko;
			$data['key']		= $this->validation->key;
			$data['captcha']	= $this->validation->captcha;
			
			$this->load->view('customer_confirm', $data);
		}
	}
	
	function insert()
	{
		$this->_set_validation();
		
		if ($this->validation->run() == FALSE)
		{
			$this->_show_post_page();
		}
		else
		{
			$data['cu_name']	= $this->validation->cu_name;
			$data['cu_kana']	= $this->validation->cu_kana;
			$data['cu_tel']		= $this->validation->cu_tel;
			$data['cu_fax']		= $this->validation->cu_fax;
			$data['cu_mail']	= $this->validation->cu_mail;
			$data['biko']		= $this->validation->biko;
			$this->db->insert('customer', $data);
			
			redirect('/customer');
		}
	}
	
	
	function delete($cu_id = '')
	{
		$cu_id = (int) $cu_id;
		$delete = (int) $this->input->post('delete');

		$this->db->where('cu_id', $cu_id);
		$query = $this->db->get('customer');

		if ($query->num_rows() == 1)
		{
			if ($delete == 1)
			{
				$this->db->where('cu_id', $cu_id);
				$this->db->delete('customer');
				$this->load->view('customer_delete_finished');
			}
			else
			{
				$row = $query->row();
			
				$data['cu_id']		= $row->cu_id;
				$data['cu_name']	= $row->cu_name;
				$data['cu_kana']	= $row->cu_kana;
				$data['cu_tel']		= $row->cu_tel;
				$data['cu_fax']		= $row->cu_fax;
				$data['cu_mail']	= $row->cu_mail;
				$data['biko']		= $row->biko;
				$data['datetime']	= $row->datetime;
				$this->load->view('customer_delete_confirm', $data);
			}
		}
		else
		{
			$this->load->view('customer_delete_error');
		}
	}


    function edit($cu_id = '')
    {
        //まず入力をさせる
        $cu_id = intval($cu_id);
        $this->db->where(array('cu_id'=>($cu_id)));
        $query = $this->db->get('customer');
        if ($query)
        {
            $row                = $query->row();
            $data['cu_id']      = $row->cu_id;
            $data['cu_name']    = $row->cu_name;
            $data['cu_kana']    = $row->cu_kana;
            $data['cu_tel']     = $row->cu_tel;
            $data['cu_fax']     = $row->cu_fax;
            $data['cu_mail']    = $row->cu_mail;
            $data['biko']       = $row->biko;
            $data['datetime']   = $row->datetime;
            $data['msg']        = '入力してください。';
            $data['err_flag']   = FALSE;    //エラーはない
        }
        else
        {
            //指定のIDが見つからなかった場合
            $data['msg'] = '<span style="color:red">指定のIDではデータが
見つかりませんでした。</span>';
            $data['err_flag']   = TRUE;     //エラーとする
        }
        $this->load->view('customer_edit', $data);
    }

    //edit_confirm
    function edit_confirm()
    {
        if ($this->input->post('submit') == '戻る')
        {
            //入力画面へ戻る
            $cu_id = intval($cu_id);
            $this->db->where(array('cu_id'=>($cu_id)));
            $query = $this->db->get('customer');
            if ($query)
            {
                $row                = $query->row();
                $data['cu_id']      = $row->cu_id;
                $data['cu_name']    = $row->cu_name;
                $data['cu_kana']    = $row->cu_kana;
                $data['cu_tel']     = $row->cu_tel;
                $data['cu_fax']     = $row->cu_fax;
                $data['cu_mail']    = $row->cu_mail;
                $data['biko']       = $row->biko;
                $data['datetime']   = $row->datetime;
                $data['msg']        = '入力してください。';
                $data['err_flag']   = FALSE;    //エラーはない
            }
            else
            {
                //指定のIDが見つからなかった場合
                $data['msg'] = '<span style="color:red">指定のIDではデー
タが見つかりませんでした。</span>';
                $data['err_flag']   = TRUE;     //エラーとする
            }
            $this->load->view('customer_edit', $data);
        }
        else
        {
            $data = array(
                'cu_id'   => $this->input->post('cu_id'),
                'cu_name' => $this->input->post('cu_name'),
                'cu_kana' => $this->input->post('cu_kana'),
                'cu_tel'  => $this->input->post('cu_tel'),
                'cu_fax'  => $this->input->post('cu_fax'),
                'cu_mail' => $this->input->post('cu_mail'),
                'biko'    => $this->input->post('biko'),
                'datetime' => date('Y-m-d H:i:s', time()),//更新日付に変
えてみる
            );
            $this->load->view('customer_edit_confirm', $data);
        }
    }

    function edit_post()
    {
        $where = array('cu_id' => $this->input->post('cu_id'));
        $data = array(
                'cu_id'   => $this->input->post('cu_id'),
                'cu_name' => $this->input->post('cu_name'),
                'cu_kana' => $this->input->post('cu_kana'),
                'cu_tel'  => $this->input->post('cu_tel'),
                'cu_fax'  => $this->input->post('cu_fax'),
                'cu_mail' => $this->input->post('cu_mail'),
                'biko'    => $this->input->post('biko'),
                'datetime' => date('Y-m-d H:i:s', time()),//更新日付に変
えてみる
            );
        $this->db->update('customer', $data, $where);
        redirect('customer');
    }

}
?>

ちなみにテーブル内容はこちらです
CREATE TABLE IF NOT EXISTS `customer` (
  `cu_id` int(12) NOT NULL auto_increment COMMENT '顧客ID',
  `cu_name` varchar(64) default NULL COMMENT '顧客名',
  `cu_kana` varchar(64) default NULL COMMENT '顧客名(カナ)',
  `cu_tel` varchar(16) default NULL COMMENT '電話番号',
  `cu_fax` varchar(16) default NULL COMMENT 'FAX番号',
  `cu_mail` varchar(128) default NULL COMMENT 'メールアドレス',
  `biko` mediumtext COMMENT '備考',
  `datetime` timestamp NOT NULL default CURRENT_TIMESTAMP on update
CURRENT_TIMESTAMP COMMENT '登録日',
  PRIMARY KEY  (`cu_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=46 ;


err_flag と msg というのが良くわからないのですが、これも追加しないと
いけないのでしょうか?

今現在は一覧から編集ボタンをクリックすると
A PHP Error was encountered

Severity: Notice

Message: Undefined variable: msg

Filename: views/customer_edit_confirm.php

Line Number: 18

と

A PHP Error was encountered

Severity: Notice

Message: Undefined variable: err_flag

Filename: views/customer_edit_confirm.php

Line Number: 21

といったエラーが返されます。
先のフィールドがないためだと思うのですが、どのようなフィールドを追加した
らいいのでしょうか?




Codeigniter-users メーリングリストの案内
Back to archive index