Welcome Guest, Not a member yet? Register   Sign In
beginner-question OOP - CI
#1

[eluser]Unknown[/eluser]
Hi /*/g,
I'am new to CI and almost new to OOP. I`ve decided building our new Online-Shop the oop-way using a MVC-Framework. I wonder how I write the following with CI.

I have (for examble) two objects: a shoppingcart and a article.

Code:
<?php

class artikel {

        public $ArticleNumber;
        public $Artikel;

        public function __construct($ArtikelNumber, $Artikel) {

                $this -> ArtikelNumber = $ArtikelNumber;
                $this -> Artikel = $Artikel;
        }

        public function __tostring() {

                return "Artikel {$this -> ArtikelNumber} {$this -> Artikel}";
        }

}

class cart {

        private $inventory;

        public function __construct() {

        }

        public function add($item) {

                $this -> inventory[] = $item;
                print $item . " added\n";
        }

        public function show() {

                foreach ($this -> inventory as $item) {

                        print "Warenkorb: {$item}\n";
                }
        }
}

$cart = new cart();

$cart -> add(new artikel(10, 'Buch'));
$cart -> add(new artikel(110, 'Handtuch'));
$cart -> add(new artikel(210, 'Zahnpasta'));
$cart -> add(new artikel(510, 'iPod'));


$cart -> show();

?>

How do I write this with CI? I understand I have to make a shoppingcart-model (extends Model) and a article-model and load it with

Code:
$this->load->model(cart);

but how can I work with these loaded objects? how do I add article-objects to my cart?

greetings from germany to the CI-Community!
Bad Pussycat ;-)
#2

[eluser]Pascal Kriete[/eluser]
Hey ho, another German Smile .

It's a little odd to see it hardcoded that way, we'll need to modify it to be a little more web friendly. We will assume that all our items are stored in a database each with a unique id. The problem with your example is that the cart is static, which obviously isn't what we want.

We'll have three parts to this. A view that will contain a list of all your items and with an add link. A controller to perform the actual logical operation. And the model. The model will connect to the database table of all our stored items.

There is also a view that shows the cart's contents. I'm leaving that out for now.

So where does the cart go? The cart we'll put into our session. Since every item has a unique id, we can just store those ids. Note that I'm keeping this as simple as possible. I'll leave out how to actually do the session insertions, but you might consider using a comma seperated list.

Here goes...
The view (made static for simplicity's sake, further below we actually pass all db data to this view):
Code:
<html>
...

<a href="..../cart/add/5">Schoko Keks</a>
...
&lt;/html&gt;

We'll assume that our chocolate cookie has the id 5 in our database. When you click the link the add function of the cart controller is called, and the number 5 is passed in as an argument.

Code:
class Cart extends Controller {

    /* Constructor */
    function Cart() {...}

    /* Show the catalogue */
    function index() {
        $this->load->model('items');
        $stuff = $this->Items->get_all();
        $this->load->view('catalogue', $stuff);
    }

    /* Add an item to the cart */
    function add($id) {
        $this->load->library('session');
        // Add id to session

        // Show success message / redirect
    }

    /* Show the cart's contents */
    function show_cart() {
        $this->load->library('session');
        $this->load->model('items');

        $item_ids = // Get all ids from session and store them as an array

        $in_cart = array();

        foreach($item_ids as $item_id) {
            $in_cart[] = $this->Items->get($item_id);
        }

        $this->load->view('show_cart', $in_cart);
    }
}

As you can see we don't actually need our database to add an item, but we do need it for our views. The index function is the one I'm using to display the catalogue, it gets all the items from the db and shows them. The show_cart function only shows the items we've added to our session. Both times the results are arrays passed to the view as described in the user guide.

So all we need now is the model:
Code:
class Items extends Model {

    /* Constructor */
    function Items { ... }

    /* Get all items */
    function get_all() {
        $query = $this->db->get('items_table');
        return $query->result_array();
    }

    /* Get specific item */
    function get($id) {
        $query = $this->db->get_where('items_table', array('id' => $id) );
        return $query->row_array();
    }
}

Not tested and written 100% in this little box so excuse any mistakes. Also shortened where I thought appropriate.

Good luck, and welcome.




Theme © iAndrew 2016 - Forum software by © MyBB