In this example, we will demonstrate business logic. Ordering a product will trigger the model to update the product's quantity and check whether it's smaller than a certain amount.
This example will be constructed from the following controllers, model, and view:
application/controllers/order.php
: This controller loads the model productmodel
$this->load->model(' productmodel')
: This controller renders the view orderview
, which displays all the products, and where each product has links to ordering a productapplication/models/productmodel.php
: This model contains functions, which retrieve products, updates its quantity, and checks its quantityapplication/views/ orderview.php
: The view displays all the products in a table, where each row has a link for ordering the productLet us assume the URI to the project root is http://mydomain.com/myproject
and http://mydomain.com/myproject/order
.
The controller PHP file is located at application/controllers/order.php
.
This controller is responsible for displaying the products and updates each product. If the product's quantity reaches a limit, it generates an error message.
The code and inline explanations are as follows:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed'), class Order extends CI_Controller { // This method retrieves the products list and returns an array // of objects each containing product details public function index() { // Loading the url helper $this->load->helper('url'), // Manually loading the database $this->load->database(); // Loading the model class $this->load->model('productmodel'), $view_params['products'] = $this->productmodel->get_products(); $this->load->view('orderview', $view_params); } // This method checks the product's quantity. // It updates the product row in the database or generates an // error message public function product($product_id) { // Loading the url helper $this->load->helper('url'), // Manually loading the database $this->load->database(); // Loading the model class $this->load->model('productmodel'), if (!$this->productmodel->update_quantity($product_id)) { mail($user_mail, 'product' . $product_id . "reached it's limit", 'Order product' . $product_id); } redirect('product'), } }
The model PHP file is located at application/models/productmodel.php
.
In this example, the methods of the CI object db
are called for generating and executing the SQL queries.
Please refer to the CI database's library available at http://ellislab.com/codeigniter/user-guide/database/index.html.
The code and inline explanations are as follows:
<?php class Productmodel extends CI_Model { // The model's constructor method public function __construct() { // Call the model constructor parent::__construct(); } // This method retrieves the products list and returns an array of // objects each containing product details. public function get_products() { // Calling the CI's db object's method for generating the // SQL queries. $query = $this->db->get('products'), // returns an array of products objects return $query->result(); } // This method retrieves a specific product's details // identified by $product_id as a parameter. public function get_product($product_id) { // Calling the CI's db object's methods for generating the // SQL queries. $this->db->select('*'), $this->db->from('products'), $this->db->where('product_id', $product_id); // Calling the CI's db object method for executing the query $query = $this->db->get(); // Returning array of one object element containing the product // details. return $query->result(); } // This method adds a product to the products table parameters. // $data - The data to insert into the table public function addProduct($data) { // Calling the CI's db object method for inserting a product data // into the products table. $this->db->insert('products', $data); } // This method updates a product row in the products table // parameters. // $product_id - The product id // $data - The updated data public function updateProduct($product_id, $data) { // Calling the CI's db object's methods for generating the // SQL queries. $this->db->where('product_id', $product_id); // Calling the CI's db object method for updating the product data // in the products table. $this->db->update('products', $data); } // This method checks whether the quantity exceeds it's limit. private function check_quantity($product_id) { // Calling the CI's db object's methods for generating the // SQL queries. $this->db->select('product_quantity'), $this->db->from('products'), $this->db->where('product_id', $product_id); // Calling the CI's db object method for executing the query. $query = $this->db->get(); // Calling the result's method row, which returns the SQL query // result row. $row = $query->row(); if ($row->product_quantity < 7) { return false; } else { return true; } } // This method updates a product quantity and return true or false, // if quantity reaches it's limit. public function update_quantity($product_id) { $sql = "UPDATE products SET product_quantity = product_quantity - 1 WHERE product_id=" $product_id; $this->db->query($sql); // Checking if the quantity reached it's limit. if ($this->check_quantity($product_id)) { return true; } else { return false; } } }
The PHP view file is located at application/views/orderview.php
. This view file displays a table with the products list.
The following are the code and inline explanations:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Products List</title> </head> <body> <table> <tr> <th>ID</th> <th>Name</th> <th>SKU</th> <th>Quantity</th> <th>Actions</th> </tr> <?php foreach ($products as $product): ?> <tr> <td><?php echo $product->product_id; ?></td> <td><?php echo $product->product_name; ?></td> <td><?php echo $product->product_sku ; ?></td> <td><?php echo $product->product_quantity ; ?></td> <td><a href="<?php echo site_url("order/product/" . $product->product_id); ?>">Order Product</a></td> </tr> <?php endforeach; ?> </table> </body> </html>
3.12.136.119