I would like to find out if my code is correct. It doesn't look right to me. The code is in response to part of an assignment which asks 3 things:

  1. Create Model Class file "booksmodel.php" i.e. "BooksModel".
  2. Add public method called save_books() to model class.
  3. Accept $_POST array as method argument.

This is my code:

   <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

    class BooksModel extends CI_Model {
        public function save_books () {
        $save_books = $_POST['BooksModel'];

Accepting a Post Array as method is what confuses me and I can't seem to find clarification on it. Thanks for checking and any feedback.

Edited by Miss Scarlet

5 Years
Discussion Span
Last Post by Miss Scarlet

A few points to consider.
CI documentation suggests using an underscore naming convention (Books_model) vs camelcasing like BooksModel. Models should also start with a capital letter and be followed only by lowercase letters.

Also your models should call the parent constructor.

public function __construct() {

Finally for a method to accept a post array as a method argument it means your method should look like:

public function fooBar( $post ){

And would be used as:

$this->model->fooBar( $_POST ); //or the post object in CI

Edited by mschroeder


Thanks mschroeder. It does help. I've never worked with PHP or had any oop, and now they threw in Codeigniter into the mix. :)

Thanks for including the function_construct which I would not have thought of. I saw it over at the Codeigniter Manual too. thanks for the help.


Thanks cereal, I'll take a look at that too. We are also learning this in conjunction with phpMyAdmin.
I'm going to take the rest of the night off from it all; a little burned out on it tonight!


This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.