mrkhan Posted December 27, 2014 Posted December 27, 2014 i have one product Template with following fields title product_name product_image product_type desc in my Template file i am using this code to get products $sid=$pages->find("template=product"); foreach($sid as $product){ { echo $product->product_name ."<br>"; echo $product->product_image->url ."<br>"; echo $product->desc ."<br>"; } this is working fine but i want to sort the products with product_type and display as bellow product_type 1 --- product_name --- product_image->url --- desc product_type 2 --- product_name --- product_image->url --- desc product_type 3 --- product_name --- product_image->url --- desc i hope i am able to explain how i need as output , can't really figure out how to do it in PW. Thanks
adrian Posted December 27, 2014 Posted December 27, 2014 I think it should be just as simple as this: $sid=$pages->find("template=product, sort=product_type");
mrkhan Posted December 27, 2014 Author Posted December 27, 2014 hello adrian, thanks for quick reply, how can i break loop or print "product_type" only once. Thanks
adrian Posted December 27, 2014 Posted December 27, 2014 You can do a check to see if the product_type is the same as it was during the last loop. Something like this: $last_product_type = ''; $sid=$pages->find("template=product, sort=product_type"); foreach($sid as $product){ if($product->product_type != $last_product_type) echo $product->product_type ."<br>"; echo $product->product_name ."<br>"; echo $product->product_image->url ."<br>"; echo $product->desc ."<br>"; $last_product_type = $product->product_type; } 2
mrkhan Posted December 27, 2014 Author Posted December 27, 2014 (edited) it work perfect thanks Edited December 27, 2014 by kongondo Sorry, marked Adrian's answer as the actual answer :-)
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now