News:

You may pay someone to create your store, or you visit our seminar and become a professional yourself with the silver certification

Main Menu

[ SOLVED ]How to show all categories a product belong to ??

Started by encreplus, July 27, 2012, 22:49:29 PM

Previous topic - Next topic

PRO

Quote from: encreplus on March 15, 2016, 21:17:59 PM
Good day,

Im using this code :

<?php $categories=$this->product->categoryItem;
foreach ($categories as $category){
$caturl = JRoute::_ ( 'index.php?option=com_virtuemart&view=category&virtuemart_category_id=' . $category['virtuemart_category_id'] );
$catname=$category['category_name'];
echo '<a href="'.$caturl.'" title="'.$catname.'">'.$catname.'</a><br/>';
}
?>

What i need is to be all after each other .. and sorted ...  ie : cat1, cat2, cat3, cat4, cat5, ect...

How can i do this ;)

see what happens when you place this code above that code

<?php
function sortByOption($a, $b) {
   return strcmp($a['category_name'], $b['category_name']);
}
usort($this->product->categoryItem['category_name'], "sortByOption");
?>

encreplus

With the sort code above ... nothing change ...

the category stays like this ...

HP Deskjet 1000 , HP DeskJet 1010 , HP Deskjet 1051 , HP DeskJet 1510 , HP DeskJet 1056 , HP DeskJet 1055 , HP DeskJet 1050 , HP DeskJet 1513 , HP Deskjet 1512 , HP DeskJet 2050 , HP Deskjet 2510 , HP Deskjet 2512 , HP Deskjet 2514 , HP DeskJet 2540 , HP DeskJet 2541 , HP DeskJet 2542 , HP DeskJet 2543 , HP DeskJet 2544 , HP DeskJet 2547 , HP Deskjet 3000 , HP DeskJet 3050 , HP DeskJet 3050A , HP DeskJet 3051A e-All-in-One J611h , HP DeskJet 3052A Wireless e-All-In-One , HP DeskJet 3054 , HP DeskJet 3056A , HP DeskJet 3510 , HP DeskJet 3511 , HP DeskJet 3512 , HP DeskJet 3516 , HP ENVY 4500 e-All-in-One , HP ENVY 4501 e-All-in-One , HP ENVY 4502 e-All-in-One , HP ENVY 4503 e-All-in-One , HP ENVY 4504 e-All-in-One , HP ENVY 4505 e-All-in-One , HP ENVY 5530 e-All-in-One , HP ENVY 5531 e-All-in-One , HP ENVY 5538 e-All-in-One , HP OfficeJet 2620 , HP OfficeJet 4630 , HP OfficeJet 4632 , HP OfficeJet 4634 , HP OfficeJet 4635 , ,

Studio 42

Try usort($this->product->categoryItem, "sortByOption");
else :
<?php 
$categories
= array();
foreach (
$this->product->categoryItem as $category$categories[$category['category_name']]=$category ;
ksort($categories);
foreach (
$categories as $category){
  
$caturl JRoute::'index.php?option=com_virtuemart&view=category&virtuemart_category_id=' $category['virtuemart_category_id'] );
  
$catname=$category['category_name'];
  echo 
'<a href="'.$caturl.'" title="'.$catname.'">'.$catname.'</a><br/>';
}

encreplus

it work in some part ... but it put for this example all the HP DeskJet before the HP Deskjet ... ie ... it put the CAPS of J before the j ...

is there a way to fix this ?

Thanks a lot for this help ... really appreciated

Jörgen

Hello

You could change the flags for the sort:
ksort($categories, SORT_NATURAL | SORT_FLAG_CASE);

More info here:
http://php.net/manual/en/function.sort.php

regards

Jörgen @ Kreativ Fotografi

Joomla 3.9.18
Virtuemart 3.4.x
Olympiantheme Hera (customized)
This reflects current status when viewing old post.