Need Help with a Query - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23) +--- Thread: Need Help with a Query (/showthread.php?tid=49925) |
Need Help with a Query - El Forum - 03-07-2012 [eluser]vincej[/eluser] Hi - I'm new to CI so please forgive what might be a silly question: I have a table ( sample below) with Order ID, Product ID's and Ordervalues. I want to be able to sum the order value for each Product ID which are repeated by OrderID so that I get a single result which I can pass through my controller into my view which might look like: Total Order Value: Chickens: $5000. Sample table: Code: orderid prodid ordervalue So far my approach has been to first run a query which pulls the ProdID and Ordervalue. Put that into an array. then run a second query, where I take the ProdID's from the array, and then do a sum on each one by ProdID. Needless to say, my code does not do what I want it to do hence why I'm here. The first section, pulls to the ProdID's fine but does not give the order values - I don't understand why, similar queries have given multiple results. My section 2 is probabaly completely wrong as all I am getting is the sum total of all products sold. Model: Code: function ProductTotal(){ If you help me out - I'd be super happy and super greatfull ! Many thanks ! Need Help with a Query - El Forum - 03-07-2012 [eluser]achilleusrage[/eluser] You can do this with one query using a GROUP BY clause: Code: $this->db->select('prodid'); Need Help with a Query - El Forum - 03-07-2012 [eluser]vincej[/eluser] Sometimes the obvious defeats us ! Many Thanks ! You are a super star ! |