[eluser]Unknown[/eluser]
Hi,
I am student and new to CodeIgniter and even PHP.
I do have an assignment on updating the books in the database(phpmyadmin).
and not sure what I am doing wrong.
I am attaching the files with this email.
Can anyone help me with this?
Part Five [Create/ Read / Update / Delete]
Apply the concepts were learned in the previous parts of the assignment to complete the following:
1.) Add “Edit/Delete” links next the records rendered in “book_view.php”
a. The link should take the user to an “Edit/Delete” form
i. Make use of anchor_popup() method to open the form
ii. Make use of the book record’s primary key to retrieve correct record to edit
2.) Create an “Edit/Delete” book form that allows users to edit, update and delete book information
a. Make use of the CI validation library
b. * Review primary key/foreign key database concepts
3.) Create a “Book Category” form that allows users to add book categories to the application
4.) Create a “Book Category” form that allows users to update and delete book categories from the application
5.) Modify “add_book_form_view.php” so that it makes use of book categories retrieved from the “bookCategories” table
a. Add code to existing controllers, views, and model as needed
controller(dashboard.php)
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Dashboard extends CI_Controller{
public function index(){
$this->load->view("dashboard_view");
}
public function get_add_books_form(){
$this->load->view("add_books_form_view");
}
public function edit_books_form_view(){
$this->load->view("get_edit_books_form");
}
public function save_books(){
$this->form_validation->set_rules('author', 'Author', 'required');
$this->form_validation->set_rules('bookTitle', 'Book Title', 'required');
$this->form_validation->set_rules('description', 'Description', 'required');
if($this->form_validation->run() == FALSE){
$this->get_add_books_form();
} else {
$this->load->model("BooksModel");
$this->BooksModel->save_books($_POST);
$this->session->set_flashdata('message' , 'Book Saved');
redirect("dashboard/get_add_books_form");
}
}
public function get_books(){
$this->load->model("BooksModel");
$data['bookinformation'] = $this->BooksModel->get_books();
$this->load->view("books_view", $data);
}
public function get_edit_books_form( $bookID="" ) {
if (empty( $bookID )) {
$bookID = $this->uri->segment(3, 0);
}
$this->load->model("BooksModel");
$data['bookinformation'] = $this->BooksModel->get_book( $bookID );
$this->load->view("edit_books_form_view", $data);
}
public function update_books() {
$this->load->model("BooksModel");
$this->BooksModel->update_books( $_POST );
$this->session->set_flashdata('message', 'Books data updated.');
redirect("dashboard/get_edit_books_form/" . $_POST['bookID']);
}
}
Model(booksmodel.php)
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class BooksModel extends CI_Model {
public function save_books($formData){
$this->db->insert("bookinformation", $formData);}
public function get_books(){
return $this->db->get('bookinformation');}
public function get_book( $bookID ){
return $this->db->get('bookinformation', $bookID);}
}
view(books_view.php)
Code:
<?php
$popupAttributes = array(
'width' => '800',
'height' => '600');
foreach( $bookinformation->result() as $bookinformation ) {
echo "<p>" . $bookinformation->bookID . "</p>";
echo "<p>" . $bookinformation->bookTitle . "</p>";
echo "<p>" . $bookinformation->author . "</p>";
echo "<p>" . $bookinformation->description . "</p>";
echo "<p>" . $bookinformation->isbn . "</p>";
echo anchor_popup('dashboard/get_edit_form_view'.$bookinformation->bookID, 'Edit/Delete', $popupAttributes );}?>
view(get_edit_books_form.php)
Code:
<?php
echo $this->session->flashdata('message');
echo validation_errors();
$id = array('id' => 'BookForm');
echo form_open('dashboard/update_books', $id);
$bookinformation = $bookinformation->row();
?>
<?php echo "EDIT/UPDATE" ?>
<br />
<br />
<input name="bookID" type="hidden" value="<?php echo $bookinformation->bookID; ?>" />
<label for="bookTitle">Book Title</label><br />
<input name="bookTitle" id="bookTitle" type="text" value="<?php echo $bookinformation->bookTitle; ?>" /><br />
<label for="author">Author: </label><br />
<input name='author' id="author" value="<?php echo $bookinformation->author; ?>" /><br />
<label for="ISBN">ISBN </label> <br />
<input name='ISBN' id="ISBN" type='text' value="<?php echo $bookinformation->isbn; ?>" /><br />
<label for="description">Description:<label><br />
<textarea rows="10" cols="30" id="description" name="description" value="<?php echo $bookinformation->description; ?>"></textarea><br /><br />
<label for="categoryID">Categories: </label> <br />
<select name="categoryID" id="categoryID">
<option value="1">1. Arts & Photography</option>
<option value="2">2. Children's Books</option>
<option value="3">3. Computers & Technology</option>
<option value="4">4. Science & Math</option>
</select>
<input id="submit" type="submit" value="Submit" />
</form>
<?php echo "<strong>Book ID</strong>: " . $bookinformation->bookID . "<br />"; ?>
<?php echo "<strong>Book Author</strong>: " . $bookinformation->author . "<br />"; ?>
<?php echo "<strong>Book Title</strong>: " . $bookinformation->bookTitle . "<br />"; ?>
<?php echo "<strong>Book ISBN</strong>: " . $bookinformation->isbn . "<br />"; ?>
<?php echo "<strong>Book Description</strong>: " . $bookinformation->description . "<br />"; ?>
<?php echo "<strong>Book Category</strong>: " . $bookinformation->categoryID . "<br />"; ?>
?php// echo "DELETE" ?>
<?php //echo anchor("dashboard/delete_books/$bookinformation->bookID", "Delete"); ?>