条目和具体注释mysql codeigniter

时间:2017-01-13 22:44:28

标签: mysql forms codeigniter

我从2天起就开始挣扎了。我无法获得每个条目的entry_id,这些条目通过我的评论表单上的$ post传递给视图。

是否有更好的解决方案来连接条目和具体的评论? 我很新,并没有真正了解我所拥有的机会。

参赛表:

CREATE TABLE IF NOT EXISTS `entry` (
`entry_id` int(11) NOT NULL AUTO_INCREMENT,
`entry_name` varchar(255) NOT NULL,
`entry_body` text NOT NULL,
`entry_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`entry_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

评论表:

  CREATE TABLE IF NOT EXISTS `comment` (
  `comment_id` int(11) NOT NULL AUTO_INCREMENT,
  `entry_id` int(11) NOT NULL,
  `comment_name` varchar(255) NOT NULL,
  `comment_email` varchar(255) NOT NULL,
  `comment_body` text NOT NULL,
  `comment_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`comment_id`)
  ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

型号:

class System_model extends CI_Model
{

function __construct()
{
    parent::__construct();
    $this->load->database();
}

function get_all_posts()
{
    //get all entry
    $query = $this->db->get('entry');
    return $query->result();
}

function add_new_entry($name,$body)
{
    $data = array(
        'entry_name' => $name,
        'entry_body' => $body,
        'entry_id' => $id
    );
    $this->db->insert('entry',$data);
}

function add_new_comment($post_id,$commentor,$email,$comment)
{
$data = array(
    'entry_id'=>$post_id,
    'comment_name'=>$commentor,
    'comment_email'=>$email,
    'comment_body'=>$comment,
);
$this->db->insert('comment',$data);
}

function get_post($id)
{
$this->db->where('entry_id',$id);
$query = $this->db->get('entry');
if($query->num_rows()!==0)
{
    return $query->result();
}
else
    return FALSE;
}

function get_post_comment($post_id)
{
$this->db->where('entry_id',$post_id);
$query = $this->db->get('comment');
return $query->result();
}

function total_comments($id)
{
$this->db->like('entry_id', $id);
$this->db->from('comment');
return $this->db->count_all_results();
}
}

控制器:

   function index()
   {
    //this function will retrieve all entry in the database
    $data['query'] = $this->system_model->get_all_posts();

    $this->load->view('main',$data);
}

function add_new_entry()
{
    $this->load->helper('form');
    $this->load->library(array('form_validation','session'));

    //set validation rules
    $this->form_validation->set_rules('entry_name', 'Title', 'required|xss_clean|max_length[200]');
    $this->form_validation->set_rules('entry_body', 'Body', 'required|xss_clean');

    if ($this->form_validation->run() === FALSE)
    {
        //if not valid
        $this->load->view('add_entry');
    }
    else
    {
        //if valid
        $name = $this->input->post('entry_name');
        $body = $this->input->post('entry_body');
        $this->system_model->add_new_entry($name,$body);
        $this->session->set_flashdata('message', '1 new entry added!');
        redirect('/system/index/');
    }
}


public function post($id)
{
$this->load->database();
$data['query'] = $this->blog_model->get_post($id);
$data['comments'] = $this->blog_model->get_post_comment($id);
$data['post_id'] = $this->db->insert_id();
$data['total_comments'] = $this->blog_model->total_comments($id);

$this->load->helper('form');
$this->load->library(array('form_validation','session'));

//set validation rules
$this->form_validation->set_rules('commentor', 'Name', 'required');
$this->form_validation->set_rules('email', 'Email', 'required|valid_email');
$this->form_validation->set_rules('comment', 'Comment', 'required');

if($this->blog_model->get_post($id))
{
    foreach($this->blog_model->get_post($id) as $row)
    {
        //set page title
        $data['title'] = $row->entry_name;
    }

    if ($this->form_validation->run() == FALSE)
    {
        //if not valid
        $this->load->view('system/post',$data);
    }
    else
    {
        //if valid
        $name = $this->input->post('commentor');
        $email = strtolower($this->input->post('email'));
        $comment = $this->input->post('comment');
        $post_id = $this->input->post('post_id');

        $this->blog_model->add_new_comment($post_id,$name,$email,$comment);
        $this->session->set_flashdata('message', '1 new comment added!');
        redirect('system/post/'.$id);
    }
}
else
    show_404();
}   

查看:

   <!DOCTYPE html>
     <html>
        <head>
     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Blog</title>
    </head>

    <body>
    <h2>This is my blog</h2>
    <?php if($query):foreach($query as $post):?>

    <h4><?php echo $post->entry_name ;?> (<?php echo $post->entry_date ;?>)</h4>
    <?php echo $post->entry_body ;?>

    <?php echo form_open('system/post');?>
    <p>Name:<br />
    <input type="text" name="commentor" />
    </p>
     <p>Email:<br />
    <textarea name="email" rows="5" cols="50" style="resize:none;"></textarea>
    </p>
    <p>Kommentar:<br />
     <textarea name="comment" rows="5" cols="50" style="resize:none;"></textarea>
     </p>

     <input type="hidden" name="post_id" value="<?php echo $post->$entry_id; ?>" />
     <input type="submit" value="Submit" />
     <?php echo form_close();?>

     <?php endforeach; else:?>
    <h4>No entry yet!</h4>

    <?php endif;?>


</body>
</html>

1 个答案:

答案 0 :(得分:0)

从列名称字段中删除$。应该是$ post-&gt; entry_id;

从控制器中的post函数中删除参数

function post(){
   $entry_id = $this->input->post('entry_id');

   .... the rest of your code. 
}

您还可以在此post函数之外创建一个函数来执行相同的操作

function get_data_from_post(){
   $for each variable you have =  $this->input->post('variable_name');
}
那么你也可以这样称呼它。可能不需要。