Кино как глоток чистого воздуха

Корзина покупателя на Codeigniter

В данном коде показан пример корзины, реализованный с помощью библиотеки Cart фреймворка CodeIgniter.

Всё решение заключено в одном контроллере, без MVC, для большей наглядности.   

Создаём таблицу

CREATE TABLE `products` (
  `
product_idint(11NOT NULL auto_increment,
  `
category_idint(11NOT NULL default '0',
  `
product_namevarchar(50) default NULL,
  `
product_short_descvarchar(255) default NULL,
  `
product_long_descriptiontext,
  `
product_pricevarchar(50) default NULL,
  `
product_addedtimestamp NULL default CURRENT_TIMESTAMP,
  
PRIMARY KEY  (`product_id`)
ENGINE=MyISAM AUTO_INCREMENT=DEFAULT CHARSET=utf8;

insert into `productsvalues('4','0','Test Product','a short description','a long description','29.95',null),
 (
'5','0','Test Product 2','a short description','a long description','1.95',null); 

Код контроллера

class Shop extends Controller {

        
function __construct() 
        
{
                parent
::Controller();
                
$this->load->library('cart');
        
}
        
        
function index()
        
{
                
// show a couple products
                
$products $this->db->get('products');
                foreach (
$products->result() as $product)
                
{
                        
echo '<h3>' anchor('shop/detail/' $product->product_id$product->product_name) . '</h3>';
                        echo 
'<p><b>' $product->product_price '</b></p>';
                        echo 
'<p>' $product->product_short_desc '</p>';
                        echo 
'<hr>';
                
}
        }
        
        
function detail()
        
{
                
// show the product and an "add to cart" link
                
$product_id $this->uri->segment(3);
                
$product $this->db->where('product_id'$product_id)->get('products')->row();
                
                echo 
'<h1>' $product->product_name '</h1>';
                echo 
'<p>' $product->product_long_description '</p>';
                echo 
'<p><b>' $product->product_price '</b></p>';
                echo 
form_open('shop/add_to_cart');
                echo 
form_hidden('product_id',$product->product_id);
                echo 
form_hidden('product_name'$product->product_name);
                echo 
form_hidden('product_price'$product->product_price);
                echo 
form_input('qty''1');
                echo 
form_submit('''Add to Cart');
                echo 
form_close();
        
}
        
        
function add_to_cart()        
        
{
                
// add the selected product to the cart
                
$data = array(
                        
'id' => $this->input->post('product_id'),
                        
'qty' => $this->input->post('qty'),
                        
'price' => $this->input->post('product_price'),
                        
'name' => $this->input->post('product_name'),
                        
'options' => array()
                );
                
                
$this->cart->insert($data);                 
                
redirect('shop/show_cart');
        
}
        
        
        
function update_cart()
        
{
                
//print_r($this->input->post('1'));
                
for ($i 1$i <= $this->cart->total_items(); $i++)
                
{
                        $item 
$this->input->post($i);
                        
$data = array(
                                
'rowid' => $item['rowid']
                                
'qty' => $item['qty']
                        
);
                        
$this->cart->update($data);
                
}
                redirect
('shop/show_cart');
        
}
        
        
function show_cart()
        
{
                
//$this->load->view('public/shop/cart');
                
?>
                <?php 
echo form_open('shop/update_cart'); ?>

                
<table cellpadding="6" cellspacing="1" style="width:100%" border="0">
                
                <
p><?php echo anchor('shop''Continue Shopping'); ?></p>
                
                <
tr>
                  <
th>QTY</th>
                  <
th>Item Description</th>
                  <
th style="text-align:right">Item Price</th>
                  <
th style="text-align:right">Sub-Total</th>
                </
tr>
                
                
<?php $i 1?>
                
                <?php 
foreach($this->cart->contents() as $items): ?>
                
                    <?php 
echo form_hidden($i.'[rowid]'$items['rowid']); ?>
                    
                    
<tr>
                      <
td><?php echo form_input(array('name' => $i.'[qty]''value' => $items['qty']'maxlength' => '3''size' => '5')); ?></td>
                      <
td>
                        
<?php echo $items['name']?>
                                    
                            <?php 
if ($this->cart->has_options($items['rowid']) == TRUE): ?>
                                    
                                
<p>
                                    
<?php foreach ($this->cart->product_options($items['rowid']) as $option_name => $option_value): ?>
                                        
                                        
<strong><?php echo $option_name?>:</strong<?php echo $option_value?><br />
                                                        
                                    
<?php endforeach; ?>
                                
</p>
                                
                            
<?php endif; ?>
                                
                      
</td>
                      <
td style="text-align:right"><?php echo $this->cart->format_number($items['price']); ?></td>
                      <
td style="text-align:right">$<?php echo $this->cart->format_number($items['subtotal']); ?></td>
                    </
tr>
                
                
<?php $i++; ?>
                
                <?php 
endforeach; ?>
                
                
<tr>
                  <
td colspan="2"> </td>
                  <
td class="right"><strong>Total</strong></td>
                  <
td class="right">$<?php echo $this->cart->format_number($this->cart->total()); ?></td>
                </
tr>
                
                </
table>
                
                <
p><?php echo form_submit('''Update your Cart'); ?></p>
                
<?php
        }
        
        
function clear_cart()
        
{
                $this
->cart->destroy();
                
redirect('shop');
        
}
        


дата 30.06.2011
разное 
расскажите друзьям

Комментарии

отзыв к фильму Ljubin
# 30.05.2014 02:30
Разобрался! Codeigniter вне подозрений.)
отзыв к фильму Ljubin
# 29.05.2014 00:49
В Codeigniter 1.7.2 работает. А вот в 2.1.4 - нет!?
А именно в функции add_to_cart
эта строчка $this->cart->insert($data);
И как следствие нет записи в сесию, и показа результата в виде.