Codeigniter cache controller issue
I have 2 controllers:
cart/ cart/buy
Both show the contents of the Cart library
<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 the buy controller stays in the cache when I add the first item to the cart. I mean the cart / controller has 5 items and the cart / buy controller has 1 item. I have to press Ctrl + F5 to see all items
I was able to solve this part:
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, you 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 the cache", for example, the cart might be filled in cart / but empty in cart / buy , and until I press Ctrl F5, the condition still doesn't work.
Is there a way to fix this, or maybe I am doing something wrong?
pd My add method to cart controller:
function add_item()
{
if($this->cart_model->validate_add_item() == TRUE)
{
redirect('cart');
}
}
My method of adding to 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;
}
}
source to share
There were no problems in your code. I think this is a cache issue as you mentioned, so you can try this to clear the cache. This will clear all previous caches created by the coderigner and also prevent future cache creation:
$this->load->driver('cache');
$this->cache->clean();
$this->output->cache(0);
source to share