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.

Recommended Answers

All 4 Replies

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() {
    parent::__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

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!

Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.