Skip to Content
PHPCODE
CodeIgniter Fixing bugs & adding date
codeigniter code / August 6, 2018

Fixing bugs & adding Date in codeinter

ALTER TABLE articles add COLUMN created_at timestamp

ALTER TABLE articles MODIFY created_at varchar(255)
step 1 :search in not work

public function search()
{
$this->load->library(‘form_validation’);
$this->form_validation->set_rules(‘query’,’Query’,’required’);

if(! $this->form_validation->run())
return $this->index();

$query=$this->input->post(‘query’);
return redirect(“user/search_results/$query”);

// $this->load->view(‘public/search_results’,compact(‘articles’));
}

step 2: add_article.php in view
<?php echo form_open(‘admin/store_article’);?>
<?php echo form_hidden(‘user_id’,$this->session->userdata(‘user_id’));?>
<?= form_hidden(‘created_at’,date(‘Y-m-d H:i:s’))?>

step 3: edit_article.php in view
<?php
class Admin extends MY_Controller
{
public function dashboard()
{
$this->load->library(‘pagination’);
$config=[
‘base_url’ => base_url(‘admin/dashboard’),
‘per_page’  => 5,
‘total_row’ => $this->articles->num_rows(),
‘full_tag_open’ => “<ul class=’pagination’>”,
‘full_tag_close’=> “</ul>”,
‘first_tag_opne’ => “<li>”,
‘first_tag_close’=> “</li>”,
‘last_tag_opne’ => “<li>”,
‘last_tag_close’ => “</li>”,
‘next_tag_opne’ => “<li>”,
‘next_tag_close’ => “</li>”,
‘pre_tag_open’  => “<li>”,
‘pre_tag_close’ => “</li>”,
‘num_tag_open’  => ‘<li>’,
‘num_tag_close’ => ‘</li>’,
‘cur_tag_open’  => “<li class=’active’><a>”,
‘cur_tag_close’ => ‘</a></li>’,
];

$this->pagination->initialize($config);
$articles=$this->articles->articles_list($config[‘per_page’],$this->uri->segment(3));
$this->load->view(‘admin/dashboard’,[‘articles’=>$articles]);
}
public function add_article()
{
$this->load->view(‘admin/add_article’);
}
public function store_article()
{
$this->load->library(‘form_validation’);

if($this->form_validation->run(‘add_article_rules’))
{
$post=$this->input->post();
return $this->_falshAndRedirect(
$this->articles->add_article($post),
“Article Added successfull.”,
“Article failed To Add, Please Try Again.”
);
}
else
{
$this->load->view(‘admin/add_article’);
}
}
public function edit_article($article_id)
{
$article=$this->articles->find_article($article_id);
$this->load->view(‘admin/edit_article’,[‘article’=>$article]);
}
public function update_article($article_id)
{
$this->load->library(‘form_validation’);
// $this->form_validation->set_error_delimiters(“<p class=’text-danger’>”,”</p>”);
if($this->form_validation->run(‘add_article_rules’))
{
$post=$this->input->post();
return $this->_falshAndRedirect(
$this->articles->update_article($article_id,$post),”Article Updated successfull.”,”Article failed To Update, Please Try Again.”
);
}
else
{
$this->load->view(‘admin/edit_article’);
}
}
public function delete_article()
{
$article_id=$this->input->post(‘article_id’);
return $this->_falshAndRedirect(
$this->articles->delete_article($article_id),
“Article Deleted successfull.”,
“Article failed To Delete, Please Try Again.”
);

}
public function __construct()
{
parent::__construct();
if(! $this->session->userdata(‘user_id’))
return redirect(‘login’);
$this->load->model(‘articlesmodel’,’articles’);
$this->load->helper(‘form’);
}
private function _falshAndRedirect($successful,$successMessage,$failureMessage)
{
if($successful)
{
$this->session->set_flashdata(‘feedback’,$successMessage);
$this->session->set_flashdata(‘feedback_class’,’alert-success’);
}
else
{
$this->session->set_flashdata(‘feedback’,$failureMessage);
$this->session->set_flashdata(‘feedback_class’,’alert-danger’);
}
return redirect(‘admin/dashboard’);

}
}
?>

PHPCODE © 2024