Jump to content

sort the results in loop


mrkhan
 Share

Recommended Posts

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

Link to comment
Share on other sites

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;
}
  • Like 2
Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...