Hi all , could you help me to create update function from these code:
Controller:
<?php
class BooksController extends CI_Controller{
public function __construct() {
parent::__construct();
// $this->load->helper('url');
// $this->load->helper('form');
// $this->load->library('form_validation');
// $this->load->helper('html');
// $this->load->model("BooksModel");
}
function getAuthors(){
$config['base_url']=site_url("/BooksController/getAuthors/");
$config['total_rows']=$this->BooksModel->record_count();
$config['per_page']=3;
$this->pagination->initialize($config);
$data['Authors'] = $this->BooksModel->getAuthors(3,$this->uri->segment(3));
foreach ($data['Authors'] as &$row) {
$id=$row['AuthorID'];
$img = $row['Image'];
$row['Image'] = img("assets/images/thumbs/".$img);
$row['actions']='<a href="'. site_url("BooksController/deleteAuthor/".$id).'"onclick="return checkDelete();">Delete</a>';
$row['actionsOnUpdate']='<a href="'. site_url("BooksController/updateAuthor/".$id).'"onclick="return checkUpdate();">Update</a>';
}
$this->load->library('table');
$this->load->view('viewAllAuthors',$data);
}
function deleteAuthor($authorToDelete){
$deleteRows = $this->BooksModel->deleteAuthor($authorToDelete);
$data['title']="Delition attempt" ;
if($deleteRows >0){
$data['msg']=$deleteRows."author(s) has been deleted";
$this->load->view('messagePage',$data);
}
else {
$data['img'] ="There was an error deliting the author with an id of".$authorToDelete;
$this->load->view('messagePage',$data);
}
}
function updateAuthor($authorToUpdate){
$updateRows = $this->BooksModel->updateAuthor($authorToUpdate);
$data['title']="Update attempt" ;
if($updateRows >0){
$data['msg']=$updateRows."author(s) has been updated";
$this->load->view('messagePage',$data);
}
else {
$data['img'] ="There was an error updating the author with an id of".$authorToUpdate;
$this->load->view('messagePage',$data);
}
}
function getAuthorByID(){
$this->load->helper('form');
if($this->input->post('submitAuthorID')){
//echo "form submitted";
$data =$this->BooksModel->getAuthorByID();
// $this->load->helper('form');
if($data == null)
{
$data['msg']=" The author ".$this->input->post('AuthorID'). "could not be found";
$this->load->view('error',$data);
return;
}
$this->load->view('showAuthorDetails',$data);
return;
}
$this->load->view('getAuthorByID');
}
function handleInsert(){
if($this->input->post('submitInsert'))
{
($this->form_validation->set_rules('AuthorID','Author ID','required'));
($this->form_validation->set_rules('firstName','First Name','required'));
($this->form_validation->set_rules('lastName','Last Name','required'));
($this->form_validation->set_rules('yearBorn','Year Born','required'));
$data['AuthorID']=$this->input->post('AuthorID');
$data['firstName']=$this->input->post('firstName');
$data['lastName']=$this->input->post('lastName');
$data['yearBorn']=$this->input->post('yearBorn');
if($this->form_validation->run()==FALSE){
$this->load->view('insertAuthor',$data);
return;
}
if ($this->BooksModel->insertAuthor()) {
$data['message'] = "The insert has been successful";
} else {
$data['message'] = "Error on insert";
}
$this->load->view('displayMessage',$data);
return;
}
$data['AuthorID'] = "";
$data['firstName'] = "";
$data['lastName'] = "";
$data['yearBorn'] = "";
$this->load->view('insertAuthor',$data);
}
}
Model:
<?php
class BooksModel extends CI_Model{
public function __construct() {
$this->load->database();
}
public function getAuthors($limit,$offset){
$this->db->limit($limit,$offset);
$resultSet = $this->db->get("authors");
return $resultSet->result_array();
}
function getAuthorByID(){
$id =$this->input->post('authorID');
$resultSet = $this->db->get_where('authors',array('authorID'=>$id));
return $resultSet->row_array();
}
public function insertAuthor(){
$recordToInsert = array(
'authorID'=>$this->input->post('authorID'),
'firstName'=>$this->input->post('firstName'),
'lastName'=>$this->input->post('lastName'),
'yearBorn'=>$this->input->post('yearBorn'),
);
$this->db->insert('authors',$recordToInsert);
$affectedRows = $this->db->affected_rows();
if ($affectedRows == 0) {
return false;
} else {
return true;
}
}
public function record_count(){
return $this->db->count_all("authors");
}
function deleteAuthor($authorToDelete){
$this->db->where('authorID',$authorToDelete);
return $this->db->delete('authors');
}
function updateAuthor($authorToUpdate){
$this->db->where('authorID',$authorToUpdate);
return $this->db->update('authors');
}
}
?>
insertAuthor.php
<?php
class BooksModel extends CI_Model{
public function __construct() {
$this->load->database();
}
public function getAuthors($limit,$offset){
$this->db->limit($limit,$offset);
$resultSet = $this->db->get("authors");
return $resultSet->result_array();
}
function getAuthorByID(){
$id =$this->input->post('authorID');
$resultSet = $this->db->get_where('authors',array('authorID'=>$id));
return $resultSet->row_array();
}
public function insertAuthor(){
$recordToInsert = array(
'authorID'=>$this->input->post('authorID'),
'firstName'=>$this->input->post('firstName'),
'lastName'=>$this->input->post('lastName'),
'yearBorn'=>$this->input->post('yearBorn'),
);
$this->db->insert('authors',$recordToInsert);
$affectedRows = $this->db->affected_rows();
if ($affectedRows == 0) {
return false;
} else {
return true;
}
}
public function record_count(){
return $this->db->count_all("authors");
}
function deleteAuthor($authorToDelete){
$this->db->where('authorID',$authorToDelete);
return $this->db->delete('authors');
}
function updateAuthor($authorToUpdate){
$this->db->where('authorID',$authorToUpdate);
return $this->db->update('authors');
}
}
?>
viewAllAuthors.php
<html> <title> All Authors</title> <body> <h3>These are all my authors</h3> <br> <!-- <table border ="1"> <th>Author ID</th> <th>First Name</th> <th>Last Name</th> <th>Year Born</th>--> <?php
$this->table->set_caption("Authors List");
$this->table->set_heading(' ID','First Name','Last Name','Year Born','Picture');
echo $this->table->generate($Authors);
echo $this->pagination->create_links();
?> <br> <br><br> <h4>Page rendered in<i>0.1985</i>seconds</h4> <script>
function checkDelete(){
if(confirm("Are you sure you want to delete this author?"))
return true;
else
return false;
}
</script> <script>
function checkUpdate(){
if(confirm("Are you sure you want to update this author?"))
return true;
else
return false;
}
</script> <!-- </table>--> </body> </html>