I'm developing a module for displaying products that are added to Joomla session. The products are being added to the session by using the code:
$app = JFactory::getApplication();
$input = $app->input;
$itemid = $input->get('itemid');
$quan = $input->get('quan');
$session = JFactory::getSession();
$cart = $session->get('cart', array());
$cart['items'][] = array('itemid' => $itemid, 'quan' => $quan);
$session->set('cart', $cart);
and I can get these products for displaying by using the code:
$cart_result = $session->get('cart');
$keys = array_keys($cart_result);
$tmp = array_values($cart_result);
$count = count(array_shift($tmp));
for ($i = 0; $i<$count; $i++)
{
$results = array();
foreach ($keys as $key)
{
$results[$key] = $cart_result[$key][$i];
}
foreach ($results as $result)
{
echo $result['itemid'];
echo $result['quan'];
}
}
That works perfectly. But I need to display the products as names and prices that are retrieved from the DB with quantities that are stored in the session. So in mod_mymodule/helper.php I need something like:
...
$ids = array();
foreach ($results as $result)
{
$ids[] = $result['itemid'];
}
$db = JFactory::getDBO();
$query = $db->getQuery(true);
$query->select($db->quoteName(array('name','price')))
->from($db->quoteName('#__products'))
->where($db->quoteName('id')." IN (".implode(',', $ids).")");
$db->setQuery($query);
$items = $db->loadObjectList();
and return $items;
but including quantities i.e. with $result['quan'];
and to display in mod_mymodule/tmpl/default.php like:
foreach($items as $item) {
echo $item->name.$item->price.$item->quan;
}
Unfortunately, even when I display products without quantities by using the above query I get only the last product instead of all the products that are stored in the session. So I have two problems: I don't know how to display ALL the products and how to display them with quantities from the session.
var_dump($ids);
?$items = array();foreach ($results as $result){$items[] = $result['itemid'];}return $items;
in helper.php and usevar_dump($items);
in tmpl/default.php I getarray(1) { [0]=> string(1) "2" }
2
is the only ID that is being returned by$result['itemid']
, hence why your database query is only displaying 1 result