3

I have 2 controllers:

cart/
cart/buy

In both show the contents of the library Cart

    <tbody>
        <?php foreach($this->cart->contents() as $items): ?>
        <tr>
            <td><?php echo $items['name'] ?></td>
            <td>$ <?php echo $this->cart->format_number($items['price']); ?></td>
            <td><?php echo $items['qty'] ?></td>
            <td>$ <?php echo $this->cart->format_number($items['subtotal']); ?></td>
        </tr>
            <?php endforeach; ?>
    </tbody>

My problem is that the controller buy remains in cache when I add the first item to the cart. I mean, the controller cart/ have 5 items and the controller cart/buy have 1 item. I have to press Ctrl + F5 to see all items

I was able to solve this part to:

function buy()
    {

        $this->output->set_header('Last-Modified: ' . gmdate("D, d M Y H:i:s") . ' GMT');
        $this->output->set_header('Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0');
        $this->output->set_header('Pragma: no-cache');
        $this->output->set_header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');  

        if($this->cart->contents())
        {

            $this->load->view('web/products/buy_view');
        }
        else
        {
            redirect('cart');   
        }       

    }

But see, I want to know if there is data in the cart and redirect to another page if the cart is empty.

Apparently if($ this->cart->contents()), "remains in cache", as for example the cart can be filled in cart/ but empty in cart/buy and until I press Ctrl F5 condition was still failing.

Is there any way to fix this, or maybe I'm doing something wrong?


p.d. My add method in cart controller:

function add_item()
    {
        if($this->cart_model->validate_add_item() == TRUE)
        {  
            redirect('cart');
        }
    }

My add method in cart model:

function validate_add_item()
    {
        $id = $this->input->post('producto_id'); 
        $cantidad = $this->input->post('cantidad');

        $this->db->select('vNombre, dPrecio');
        $this->db->where('iIdProducto', $id);
        $query = $this->db->get('product', 1);

        if($query->num_rows > 0)
        { 

            foreach ($query->result() as $row)  
            {  

                $data = array(  
                        'id'      => $id,  
                        'qty'=> $cantidad,  
                        'price'  => $row->dPrecio,  
                        'name'  => $row->vNombre  
                );  


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

                return TRUE;    
            }
        }
        else
        {  
            return FALSE;  
        }   
    }
2
  • I use other method: add_item()
    – csotelo
    Commented Mar 26, 2012 at 21:13
  • That helps a lot, unfortunately I can't see the problem but it will help someone else to. I'll just give you an upvote and wish you luck.
    – Mog
    Commented Mar 26, 2012 at 21:21

1 Answer 1

1

I didnt find any problem in your code. I think its a cache issue as you mentioned so you can try this to clear your cache. This will clear the all previous caches created by codeigniter and also prevent to create cache in future:

    $this->load->driver('cache');
    $this->cache->clean();

    $this->output->cache(0);
3
  • Hi, where to enter these commands? Within how the method? Because I tried it and does not work. I found another solution "less clean" add a parameter with a random value within the method buy / this way you always see the values.
    – csotelo
    Commented Mar 27, 2012 at 13:06
  • you can add these lines just before loading the view
    – Code Prank
    Commented Mar 27, 2012 at 13:07
  • Yes I Know, but with no result :(
    – csotelo
    Commented Mar 27, 2012 at 13:23

Not the answer you're looking for? Browse other questions tagged or ask your own question.