multiple query results in a loop to be passed in a view |
[eluser]rei[/eluser]
I'm having a hard time rewriting my working code to MVC. w/c implements cart options/variations that affects prices of a product using the CI cart library. what I did is query the products that are needed to be displayed and run a loop for the fetched products and query the option keys like color,size etc.. then make a loop again for the option keys and query the option values like for color: red,yellow, blue etc. etc.. But I think its really bad for performance because I'm making too many queries. Do u have a better idea of how will I do that in a better/nicer way? without making too many queries. heres the logic I did: 1. query the products 2. query the option_keys (color,size etc) for each product. 3. query the option_values (red,yellow,blue etc.. ) for each option_keys. Please suggest me a better solution than this one. Thanks ![]()
[eluser]boltsabre[/eluser]
Try using joins, you should be able to get everything you need from your database with one query. And then cache your result ;-)
[eluser]rei[/eluser]
How will I do that sir? hmm. I think I cannot do that for just using one query because 1 product has many option_keys (color,size,weight) and 1 option_key has many option_values(red,yellow,blue, small,medium,large,5kgs,2kgs, ) for example. and nwei my problem is similar to this one -- http://ellislab.com/forums/viewthread/80539/P15
[eluser]rei[/eluser]
Up. Can someone please help me? Hi all, I managed to solve my problem. It now supports product variations that affects the price of a product. The code is not yet clean because I just want to learn the concept of product variations so dont worry if the code is not yet optimized/clean. If you have time please have a look at the source code to see if the logic is okay/acceptable. Thanks in advance ![]() ![]() ![]()
[eluser]CroNiX[/eluser]
In your loop, instead of performing a new query, just store the IDs in an array. Then use where_in('key', $id_array) in a single query.
[eluser]rei[/eluser]
Thanks. here is my database table structure. products table: id, name, price option_keys table: id, prod_id, option_key option_values table: id, option_id, option_value, price Here's what I did: 1. query all the products and store the product_id's in an array 2 SELECT * FROM option_keys WHERE prod_id IN ($prod_ids); I'm getting this result: id prod_id option_key 1 5 size 3 5 color 4 6 weight 6 6 style 7 5 font Now the question is how can I assign the option_keys to be an array or object and assign them to their corresponding parent(prod_id)
[eluser]CroNiX[/eluser]
Code: $products = array();
[eluser]rei[/eluser]
Thanks. But I can only store 1 option key per product. What I need is to store many option_keys per 1 product. How can I do that sir? Thanks in advance ![]()
[eluser]rei[/eluser]
It works! Thank you sir... Here is my new code: Code: function get_all_products() This is the output of the products array for 1 product: Code: Array Now the problem is how can I display the contents of that array.hmmm
[eluser]rei[/eluser]
problem solved. Thanks sir for your help ![]() Here is the source w/c will be helpful for others who will also encounter my problem https://github.com/basco-johnkevin/Cart_Igniter/ |
Welcome Guest, Not a member yet? Register Sign In |