I have a database table which has a column called code.
I would like to know using codeigniter active record if any row that has a code still present for more that 15 min then it will delete that row.
I am not sure if I have my clear_unconfirmed_post correct?
![[Image: 3BKCemcmTc8i.png]]()
Model
Controller
Newthread.php (Size: 2.34 KB / Downloads: 2)
Newthread_model.php (Size: 1.17 KB / Downloads: 1)
I would like to know using codeigniter active record if any row that has a code still present for more that 15 min then it will delete that row.
I am not sure if I have my clear_unconfirmed_post correct?
PHP Code:
public function clear_unconfirmed_post() {
$this->db->where('code IS NOT NULL', null);
$query = $this->db->get($this->db->dbprefix . 'post');
if ($query->num_rows() > 0) {
// Delete rows if code have been there for 15 min
return $query->num_rows();
}
}
![[Image: 3BKCemcmTc8i.png]](http://ibin.co/3BKCemcmTc8i.png)
Model
PHP Code:
<?php
class Newthread_model extends CI_Model {
public function check_if_code_set($code = NULL) {
$this->db->where('code', $code);
$query = $this->db->get($this->db->dbprefix . 'post');
if ($query->num_rows() > 0) {
return TRUE;
} else {
return FALSE;
}
}
public function get_tmp_post() {
$this->db->where('post_id', $this->session->userdata('post_id'));
$query = $this->db->get($this->db->dbprefix . 'post');
if ($query->num_rows() > 0) {
return $query->row_array();
} else {
return FALSE;
}
}
public function insert_tmp_post($data = array()) {
$this->db->set($data);
$this->db->insert($this->db->dbprefix . 'post');
return $this->db->insert_id();
}
public function update_tmp_post($data = array()) {
$this->db->set($data);
$this->db->where('post_id', $this->session->userdata('post_id'));
$this->db->update($this->db->dbprefix . 'post');
}
public function delete_post() {
$this->db->where('post_id', $this->session->userdata('post_id'));
$this->db->delete($this->db->dbprefix . 'post');
$this->session->unset_userdata('post_id');
}
public function clear_unconfirmed_post() {
}
}
Controller
PHP Code:
<?php
class Newthread extends MX_Controller {
public function __construct() {
parent::__construct();
$this->load->library('form_validation');
$this->load->helper('string');
$this->load->model('catalog/forum/newthread_model');
}
public function index($forum_id = NULL) {
$code = random_string('alpha', 32);
$data['forum_id'] = $forum_id;
$this->form_validation->set_rules('subject', 'subject', 'trim|required');
$this->form_validation->set_rules('message', 'message', 'trim|required');
if ($this->form_validation->run() == TRUE) {
if ($this->input->post('newthread_preview')) {
$tmp_post_data = array(
'forum_id' => '1',
'user_id' => '1',
'subject' => $this->input->post('subject'),
'message' => $this->input->post('message'),
'date_modified' => date('Y-m-d H:i:s'),
'code' => $this->input->post('code')
);
if ($this->newthread_model->check_if_code_set($this->input->post('code'))) {
$this->newthread_model->update_tmp_post($tmp_post_data);
} else {
$post_id = $this->newthread_model->insert_tmp_post($tmp_post_data);
$this->session->set_userdata(array('post_id' => $post_id));
}
}
if ($this->input->post('newthread_delete')) {
$this->newthread_model->delete_post();
redirect(base_url('forum'));
}
}
$post_data = $this->newthread_model->get_tmp_post();
if ($this->input->post('code')) {
$data['code'] = $this->input->post('code');
} elseif (!empty($post_data)) {
$data['code'] = $post_data['code'];
} else {
$data['code'] = $code;
}
if ($this->input->post('subject')) {
$data['subject'] = $this->input->post('subject');
} elseif (!empty($post_data)) {
$data['subject'] = $post_data['subject'];
} else {
$data['subject'] = '';
}
if ($this->input->post('message')) {
$data['message'] = $this->input->post('message');
} elseif (!empty($post_data)) {
$data['message'] = $post_data['message'];
} else {
$data['message'] = '';
}
$data['header'] = Modules::run('catalog/common/header/index');
$data['footer'] = Modules::run('catalog/common/footer/index');
$data['preview'] = Modules::run('catalog/forum/newthread_preview/index', $post_data);
$this->load->view('template/forum/newthread_view', $data);
}
}
Newthread.php (Size: 2.34 KB / Downloads: 2)
Newthread_model.php (Size: 1.17 KB / Downloads: 1)