i’m creating a small shopping cart and i’m saving my products in a session (no database). My session sets the product id. But how can get the correct product from my database that matches the product id in my stored session?
Code:
foreach ( (isset($shopcart) && is_array($shopcart) && count($shopcart) > 0) ? $shopcart : array() as $item )
{
$this->template->shopcart = $shopcart;
}
.
<?php if( isset( $shopcart ) ): ?>
<?php foreach ($shopcart as $item): ?>
<?php echo $item['id'] ?>
<?php endforeach ?>
<?php endif; ?>
EDIT:
foreach ( (isset($shopcart) && is_array($shopcart) && count($shopcart) > 0) ? $shopcart : array() as $item )
{
$this->template->shopcart = DB::select()->where('id', 'IN', $item['id']) ->from('products')->execute();
//$this->template->shopcart = $shopcart;
}
Database_Exception [ 1064 ]: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ”5” at line 1 [ SELECT * FROM products WHERE id IN ‘5’ ]
EDIT 2:
$this->template->shopcart = array();
if (count($shopcart))
{
$this->template->shopcart =
DB::select()
->where('id', 'IN', $item)
->from('products')
->execute();
}
Template:
<?php if( isset( $shopcart ) ): ?>
<?php foreach ($shopcart as $item): ?>
<?php echo $item['id'] ?> //This is the product id in my saved session but i need to get the name from the database
<?php endforeach ?>
<?php endif; ?>
Are you using ORM or the query builder? If you are using the ORM, you could do:
if you are using the query builder:
In your view, now iterate of your
$shopcartas you would.Using your case, (take it out of your loop)