eldan88 Posted June 17, 2014 Share Posted June 17, 2014 Hey Guys. I crearted a class that goes loops through the session cart and displays the items. The problem that I am facing is when a return the values from a method it only brings back the first item that was added to the cart. It doesn't out out all the values. However when I echo the the values straight from inside the method it shows all the items it looped through. I have tried every possibly thing to have all the items be displayed but I couldn't find to seem a solution. Please note that I skipped a lot of the code in the example below for breviety. Such as instantiation, and other things. There maybe some syntax errors as well. I just did a breif copy and paste Any help would be really appreciated!!! class coreCartFunctions { protected function DisplayMenuItems($menu_item_id_session){ $qty_output = "<span style=\"font-size:18px\">X{$this->item_qty}</span>"; $this->menu_item_name = $this->getItems($this->menu_item_id,"menu_item_name"); if($this->DisplayPrice()){ $update_order_url ="item-detail.php?update_order=true&menu_item_id={$menu_item_id_session} &callback_url={$this->url}&store={$_SESSION['store_name']}"; $output = "<a class='{$this->menu_items_class_prop_a}' href='{$update_order_url}'> <div class='{$this->menu_items_class}'>{$qty_output} {$this->menu_item_name}</div>"; $output .= "<div class='menu_item_price'>\${$this->menu_item_price}</div></a>"; return $output; } else { $output = "<a class='{$this->menu_items_class_prop_a}' href='#'><div class='menu_item_name'>" .$qty_output." ".$this->menu_item_name ."</div></a>"; return $output; } } class displayCartItems extends coreCartFunctions { public function GetMenuItems() { foreach ($this->items as $menu_item_id_session) { //assign all the properties that belong to core class $output = $this->DisplayMenuItems($menu_item_id_session); return $output } } $display_items = $display_menu_item->GetMenuItems(); echo $display_items Quote Link to comment Share on other sites More sharing options...
Solution Zane Posted June 17, 2014 Solution Share Posted June 17, 2014 Because you are returning your function inside the loop. Also, you aren't concatenating $output, you're just overwriting it every time. Quote Link to comment Share on other sites More sharing options...
eldan88 Posted June 18, 2014 Author Share Posted June 18, 2014 Hey Zane! I kinda get what you are saying. How can return all the values outside of the function?? Quote Link to comment Share on other sites More sharing options...
eldan88 Posted June 18, 2014 Author Share Posted June 18, 2014 Hey Zane! Never mind i got it. What I did is I initiated the $output with a $output = "" variable outside the the foreach loop and returned it also outside the foreach loop. Thanks alot man! P.S i replied back to your email. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.