2

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.

4
  • Whats happens when you use var_dump($ids);?
    – Lodder
    Commented May 5, 2015 at 14:49
  • If I use $items = array();foreach ($results as $result){$items[] = $result['itemid'];}return $items; in helper.php and use var_dump($items); in tmpl/default.php I get array(1) { [0]=> string(1) "2" }
    – stckvrw
    Commented May 5, 2015 at 15:31
  • So that's your answer. 2 is the only ID that is being returned by $result['itemid'], hence why your database query is only displaying 1 result
    – Lodder
    Commented May 5, 2015 at 15:34
  • It was your solution by the link joomla.stackexchange.com/a/9510/4042 and it really works if I echo IDs. But why does it not work if I push them to array?
    – stckvrw
    Commented May 5, 2015 at 15:39

2 Answers 2

2

Query: Fetch item id as well.

$query->select($db->quoteName(array('id', 'name','price')))
      ->from($db->quoteName('#__products'))
      ->where($db->quoteName('id')." IN (".implode(',', $ids).")");

Loop: Loop over the items fetched and compare with array in session

foreach($items as $item) {

    foreach ($results as $result){

       if($item->id == $result['itemid'])
       {
          echo $item->name.$item->price.$result['quan'];
       }
    }
}
1
  • Thank you, it works. But it's a module and I need to return $items only in helper.php and loop over the items only in tmpl/default.php. I created two methods in helper: one for passing $items and one for passing $results to tmpl. But any simpler or better way would be appreciated
    – stckvrw
    Commented May 6, 2015 at 7:42
0

Ok, I resolved the first problem with the code:

$ids = array();
foreach($cart_result['items'] 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();
return $items;

Now the second problem regarding quantities remains

2
  • Since this update is not an "acceptable" answer, perhaps I can persuade you to remove this post and add this code block to your question as an edit, then explain the remaining issue so that Nagarjun and other volunteers can post something that is worthy of the green tick. Commented Apr 27, 2018 at 3:25
  • [Comment bump ] Commented Mar 17, 2019 at 11:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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