Welcome Guest, Not a member yet? Register   Sign In
how to use cart.php on CI?
#1

[eluser]yudahebat[/eluser]
please give me an example how to use cart.php (shopping cart class)new! on CI 1.7.1.

I can't understand the example on user guide that I download..

thx for your attention
#2

[eluser]Thorpe Obazee[/eluser]
yudahebat, it's probably best if you could try it out yourself and ask when you bump into problems?
#3

[eluser]yudahebat[/eluser]
i've been test the example on shopping cart class
but when I change the update function

from
Code:
$data = array(
               'rowid' => 'b99ccdf16028f015540f341130b6d8ec',
               'qty'   => 3
            );

$this->cart->update($data);

into

Code:
$data = array(
               'rowid' => 'b99ccdf16028f015540f341130b6d8ec',
               'qty'   => $this->input->post('qty')
            );

$this->cart->update($data);

to get an input from form input, but nothing change..

explain please
#4

[eluser]Stefano G[/eluser]
...maybe if you could post some more info: webpage where you downloaded the Class, the doc page and so on...
#5

[eluser]Dam1an[/eluser]
Not to mention maybe a bit more of the controller logic and the view
#6

[eluser]Thorpe Obazee[/eluser]
[quote author="Stefano G" date="1242396819"]...maybe if you could post some more info: webpage where you downloaded the Class, the doc page and so on...[/quote]

I think he was talking about the new Cart class in SVN.

Anyway, I've never worked with the Cart class. Like what Dam1am said, it's actually helpful if you tell us more information about the problem. It's more like a 'Help us help you' kind of statement.
#7

[eluser]yudahebat[/eluser]
the controller
Code:
<?
class Cart extends Controller {

     function Cart()
    {
        parent::Controller();

    }
    
    function index(){
       $data = array(
               'id'      => 'sku_123ABC',
               'qty'     => 1,
               'price'   => 39.95,
               'name'    => 'T-Shirt',
               'options' => array('Size' => 'L', 'Color' => 'Red')
            );

        $this->cart->insert($data);
    $this->load->view('cart',$data);
    }
    
    function update(){
       $data = array(
               'rowid' => 'd0c00b4e4b747d8475d1c93ff8067138',
               'qty'   => $this->input->post('qty')
            );

        $this->cart->update($data);
    $this->load->view('cart',$data);
    }
    
}

the view

Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
&lt;html &gt;
&lt;head&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /&gt;
&lt;title&gt;Untitled Document&lt;/title&gt;
&lt;/head&gt;

&lt;body&gt;
&lt;?php echo form_open('cart/update'); ?&gt;

<table cellpadding="6" cellspacing="1" style="width:100%" border="0">

<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>

&lt;?php foreach($this->cart->contents() as $items): ?&gt;

    &lt;?php echo form_hidden('rowid', $items['rowid']); ?&gt;
    
    <tr>
      <td>&lt;?php echo form_input(array('name' => 'qty', 'value' => $items['qty'], 'maxlength' => '3', 'size' => '5')); ?&gt;</td>
      <td>
        &lt;?php echo $items['name']; ?&gt;
                    
            &lt;?php if ($this->cart->has_options($items['rowid']) == TRUE): ?&gt;
                    
                <p>
                    &lt;?php foreach ($this->cart->product_options($items['rowid']) as $option_name => $option_value): ?&gt;
                        
                        <strong>&lt;?php echo $option_name; ?&gt;:</strong> &lt;?php echo $option_value; ?&gt;<br />
                                        
                    &lt;?php endforeach; ?&gt;
                </p>
                
            &lt;?php endif; ?&gt;
                
      </td>
      <td style="text-align:right">&lt;?php echo $this->cart->format_number($items['price']); ?&gt;</td>
      <td style="text-align:right">$&lt;?php echo $this->cart->format_number($items['subtotal']); ?&gt;</td>
    </tr>


&lt;?php endforeach; ?&gt;

<tr>
  <td colspan="2"> </td>
  <td class="right"><strong>Total</strong></td>
  <td class="right">$&lt;?php echo $this->cart->format_number($this->cart->total()); ?&gt;</td>
</tr>

</table>

<p>&lt;?php echo form_submit('', 'Update your Cart'); ?&gt;</p>
&lt;/body&gt;
&lt;/html&gt;

and I attach the shopping cart class
#8

[eluser]Stefano G[/eluser]
ok I am sorry I thought it was one of the classes form PHPClasses or similar Smile
#9

[eluser]@Frédéric Quié - bleekom.org[/eluser]
Hello,

I think you forgot something from the user guide example on Cart in CI 1.7.2 :

Here is the controller :

Code:
&lt;?php
class testcart extends Controller {

  function __construct() //constructeur
  {
    parent::Controller();   // appel l'objet parent Controller
    $this->load->library('cart');
    $this->load->helper(array('form', 'url'));
    //$this->load->library('form_validation');
    
  }
  function index()
  {
  //$this->load->library('form');
  $this->output->enable_profiler(TRUE);
  $data = array(
               array(
                       'id'      => 'sku_123ABC',
                       'qty'     => 1,
                       'price'   => 39.95,
                       'name'    => 'T-Shirt',
                       'options' => array('Size' => 'L', 'Color' => 'Red')
                    ),
               array(
                       'id'      => 'sku_567ZYX',
                       'qty'     => 1,
                       'price'   => 9.95,
                       'name'    => 'Coffee Mug'
                    ),
               array(
                       'id'      => 'sku_965QRS',
                       'qty'     => 1,
                       'price'   => 29.95,
                       'name'    => 'Shot Glass'
                    )
            );

  $this->cart->insert($data);
  
  $this->load->view('v_cart');
  }
  
  function update()
  {
  //$this->load->library('form');
  $this->output->enable_profiler(TRUE);

  $this->cart->update($_POST[1]);
  $this->cart->update($_POST[2]);
  $this->cart->update($_POST[3]);
  
  $this->load->view('v_cart');
  }
}
?&gt;

And the view :
Code:
&lt;?php echo form_open('testcart/update'); ?&gt;

<table cellpadding="6" cellspacing="1" style="font-size:12px;width:50%" border="0">

<tr BGCOLOR="#AAA">
  <th style="text-align:left">Item Description</th>
  <th style="width:10%;text-align:left">QTY</th>
  <th style="width:15%;text-align:right">Item Price</th>
  <th style="width:15%;text-align:right">Sub-Total</th>
</tr>

&lt;?php $i = 1; ?&gt;

&lt;?php foreach($this->cart->contents() as $items): ?&gt;

    &lt;?php echo form_hidden($i.'[rowid]', $items['rowid']); ?&gt;
    
    <tr BGCOLOR="#CCC">
      <td>
        &lt;?php echo $items['name']; ?&gt;
                    
            &lt;?php if ($this->cart->has_options($items['rowid']) == TRUE): ?&gt;
                    
                <br>
                    &lt;?php foreach ($this->cart->product_options($items['rowid']) as $option_name => $option_value): ?&gt;
                        
                        <strong>&lt;?php echo $option_name; ?&gt;:</strong> &lt;?php echo $option_value; ?&gt;<br />
                                        
                    &lt;?php endforeach; ?&gt;
                
                
            &lt;?php endif; ?&gt;
                
      </td>
        <td valign="top">&lt;?php echo form_input(array('name' => $i.'[qty]', 'value' => $items['qty'], 'maxlength' => '3', 'size' => '2')); ?&gt;</td>
      <td  valign="top" style="text-align:right">&lt;?php echo $this->cart->format_number($items['price']); ?&gt; €</td>
      <td  valign="top" style="text-align:right">&lt;?php echo $this->cart->format_number($items['subtotal']); ?&gt; €</td>
    </tr>

&lt;?php $i++; ?&gt;

&lt;?php endforeach; ?&gt;

<tr>
  <td colspan="2"> </td>
  <td BGCOLOR="#111" style="color:#fff;text-align:center"><strong>Total</strong></td>
  <td BGCOLOR="#111" style="color:#fff;text-align:right"><strong>&lt;?php echo $this->cart->format_number($this->cart->total()); ?&gt; €</strong></td>
</tr>

</table>

<p>&lt;?php echo form_submit('', 'Update your Cart'); ?&gt;</p>

I guess it's the '$i' you got rid of... Because for me it works perfectly well !!! ;-)
#10

[eluser]yudahebat[/eluser]
thx wanadoobtdc....

I've fix it and it works....




Theme © iAndrew 2016 - Forum software by © MyBB