2015-11-23 64 views
0

我正在用CodeIgniter構建一個小博客腳本,並且希望在我的數據庫的blogs表中存儲海報ID和海報名稱。如何在數據庫中存儲帖子的詳細信息和時間戳?

我應該怎麼做:我是否像這樣通過會話?其次,我在表中還有一個date_posted列。我如何使用視圖中發佈的日期進行設置?

+0

歡迎堆棧溢出!我編輯了你的問題,所以它專注於本質。這將會引起讀者的關注。感覺當然可以自己編輯。沒有必要自我介紹,或者事先感謝......最好是要達到這個本質,並留下它。祝你好運! – trincot

回答

0

如果海報ID是一個會話,你得到它正確

夫婦用戶指南鏈接將被使用完整。

http://www.codeigniter.com/user_guide/database/query_builder.html#inserting-data

http://www.codeigniter.com/user_guide/database/query_builder.html#updating-data

http://www.codeigniter.com/user_guide/general/models.html

型號名:Post_model.php

<?php 

class Post_model extends CI_Model { 

public function add_post() { 

$data = array(
     'id' => $this->session->userdata('id'), 
     'username' => $this->input->post('username'), 
     'post' => $this->input->post('post') 
); 

$this->db->insert($this->db->dbprefix . 'posts'); 

} 

public function edit_post() { 

$data = array(
    'username' => $this->input->post('username'), 
    'post' => $this->input->post('post') 
); 

$this->db->where('id', $this->session->userdata('id')); 
$this->db->update($this->db->dbprefix . 'posts'); 

} 

} 

控制器:

http://www.codeigniter.com/user_guide/libraries/form_validation.html

http://www.codeigniter.com/user_guide/helpers/form_helper.html

<?php 
class Welcome extends CI_Controller { 

    public function __construct() { 
     parent::__construct(); 
     $this->load->model('folder/post_model'); 
     $this->load->library('form_validation'); 
     $this->load->helper('url'); 
     $this->load->helper('form'); 
    } 

    public function index() { 
     $this->form_validation->set_rules('post', 'Post'); 

     if ($this->form_validation->run() == FALSE) { 

     $this->load->view('common/header'); 
     $this->load->view('posts'); 
     $this->load->view('common/footer'); 

     } else { 

     // $this->post_model->add_post(); 
     // Or 
     // $this->post_model->edit_post(); 

     redirect('success-controller'); 

     } 
    } 

} 
0

在表單數據的情況下,您通常會訪問$_POST請求。在笨,你可以說:

$data = array(
    'username' => $this->input->post('username'), 
); 
0

如果你想存儲誰張貼的博客用戶,我假設你已經登錄的功能,你要存儲在會話的用戶ID,你是正確的,你要檢索用戶的ID登錄從會話將其與博客文章中date_posted關聯,並添加,做插入時,只需用PHP來添加當前時間,即:

new DateTime() 

,並確保你有用於user_id和date_posted在博客表中的字段

相關問題