Wrox Programmer Forums
| Search | Today's Posts | Mark Forums Read
BOOK: Professional CodeIgniter ISBN: 978-0-470-28245-8
This is the forum to discuss the Wrox book Professional CodeIgniter by Thomas Myer; ISBN: 9780470282458
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Professional CodeIgniter ISBN: 978-0-470-28245-8 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old November 5th, 2009, 01:07 PM
Authorized User
 
Join Date: Nov 2009
Posts: 26
Thanks: 3
Thanked 0 Times in 0 Posts
Default Shouldn't they be in a controller?

models/morders have the following codes.

And I am wondering, since there are nothing to do with a database, they can be in a controller.

Shouldn't they?

Code:
class MOrders extends Model{
 function  __construct(){
    parent::Model();
 }

function updateCart($productid,$fullproduct){
	//pull in existing cart first!
	$cart = isset($_SESSION['cart']) ? $_SESSION['cart'] : array();
	// $cart = $_SESSION['cart'];//$this->session->userdata('cart');
	$productid = id_clean($productid);
	$totalprice = 0;
	if (count($fullproduct)){
		if (isset($cart[$productid])){
			$prevct = $cart[$productid]['count'];
			$prevname = $cart[$productid]['name'];
			$prevprice = $cart[$productid]['price'];
			$cart[$productid] = array(
					'name' => $prevname,
					'price' => $prevprice,
					'count' => $prevct + 1
					);
		}else{
			$cart[$productid] = array(
					'name' => $fullproduct['name'],
					'price' => $this->format_currency($fullproduct['price']),
					'count' => 1
					);			
		}
	
		foreach ($cart as $id => $product){
			$totalprice += $product['price'] * $product['count'];
		}		
		
		$_SESSION['totalprice'] = $this->format_currency($totalprice);
		//$this->session->set_userdata('totalprice', $totalprice);
		$_SESSION['cart'] = $cart;
		//$this->session->set_userdata('cart',true);
		$this->session->set_flashdata('conf_msg', "We've added this product to your cart."); 
	}

}

function removeLineItem($id){
	$id = id_clean($id);
	$totalprice = 0;
	$cart = $_SESSION['cart'];//$this->session->userdata('cart');
	if (isset($cart[$id])){
		unset($cart[$id]);
		foreach ($cart as $id => $product){
			$totalprice += $product['price'] * $product['count'];
		}		
		$_SESSION['totalprice'] = $this->format_currency($totalprice);
		$_SESSION['cart'] = $cart;
		//$this->session->set_userdata('totalprice', $totalprice);
		//$this->session->set_userdata('cart',true);
		echo "Product removed.";
	}else{
		echo "Product not in cart!";
	}
}

function updateCartAjax($idlist){
	$cart = $_SESSION['cart'];//$this->session->userdata('cart');
	//split idlist on comma first
	$records = explode(',',$idlist);
	$updated = 0;
	$totalprice = $_SESSION['totalprice'];
	//$this->session->userdata('totalprice');
	if (count($records)){
		foreach ($records as $record){
			if (strlen($record)){
				//split each record on colon
				$fields = explode(":",$record);
				$id = id_clean($fields[0]);
				$ct = $fields[1];
				
				if ($ct > 0 && $ct != $cart[$id]['count']){
					$cart[$id]['count'] = $ct;
					$updated++;
				}elseif ($ct == 0){
					unset($cart[$id]);
					$updated++;
				}
			
			}
			
		}
		
		
		if ($updated){
			$totalprice=0;
			foreach ($cart as $id => $product){
				$totalprice += $product['price'] * $product['count'];
			}		

			$_SESSION['totalprice'] = $this->format_currency($totalprice);
			$_SESSION['cart'] = $cart;
			//$this->session->set_userdata('totalprice', $totalprice);		
			//$this->session->set_userdata('cart',true);
			
		
			switch ($updated){
				case 0:
				$string = "No records";
				break;
				
				case 1:
				$string = "$updated record";
				break;
				
				default:
				$string = "$updated records";
				break;
			}
			echo "$string updated";
			//$this->session->set_flashdata('update_count', $string ." updated");
		}else{
			echo "No changes detected";
			//$this->session->set_flashdata('update_count', "No changes detected");
		}
	}else{
		echo "Nothing to update";
		//$this->session->set_flashdata('update_count', "Nothing to update");
	}
}
  #2 (permalink)  
Old November 6th, 2009, 09:51 AM
Wrox Author
 
Join Date: May 2008
Location: Austin, TX, USA.
Posts: 53
Thanks: 0
Thanked 5 Times in 5 Posts
Default this is totally up to you

some folks, like myself, like to do all my data processing in the model. others prefer to do it in the controller. others put stuff like this in libraries or plugins or helpers.....
__________________
Thomas Myer
Author, Professional CodeIgniter
http://www.tripledogs.com
  #3 (permalink)  
Old November 6th, 2009, 05:24 PM
Authorized User
 
Join Date: Nov 2009
Posts: 26
Thanks: 3
Thanked 0 Times in 0 Posts
Default Ok, I got your point.

Thanks. I got your point.


Similar Threads
Thread Thread Starter Forum Replies Last Post
Controller Question susca BOOK: Professional CodeIgniter ISBN: 978-0-470-28245-8 1 July 6th, 2009 06:09 PM
PHP Controller cyber_kaley20 Beginning PHP 0 July 8th, 2007 10:12 PM
controller to controller tigransh Spring 1 November 11th, 2006 12:48 AM
throwaway controller mmasters BOOK: Professional Java Development with the Spring Framework 0 June 27th, 2006 10:56 AM
Model View Controller gpartin33324 Java GUI 0 October 4th, 2004 04:00 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.