You are here:  » Sorting Categories and Sub Categories


Sorting Categories and Sub Categories

Submitted by Nic0904 on Mon, 2016-02-29 13:24 in

Hi David,

The new hierarchical categories are really useful, but with large numbers of categories can be difficult for the user.

To get around this problem I just added a single number in front of each category name, like this

1 Catname
1 Subname
2 Subname
3 Subname
2 Catname
1 Subname

This does work, but doesn't look great and can cause unexpected results on some of the API calls as the number is passed in there too.

Is there a way to add a sequence number to the admin/db so that I could order the categories without having to rely on visible numbers?

Thanks
Dave

Submitted by support on Mon, 2016-02-29 13:40

Hello Dave,

A number of users have mentioned this so I am going to include category sequencing in the next distribution. In the mean time, if using a numerical prefix to force the order this can be removed from display easily. Firstly, edit includes/tapestry.php and add the following new code to the end of the file, just before the closing ?> tag:

  function tapestry_displayCategory($category)
  {
    $words = explode(" ",$category);
    array_shift($words);
    return implode(" ",$words);
  }

With this in place, anywhere that category names are displayed you can wrap the variable within a call to tapestry_displayCategory($category) for example, in categories.php look for the following code at line 32:

  $item["name"] = $category["name"];

...and REPLACE with:

  $item["name"] = tapestry_displayCategory($category["name"]);

And for the expanding drop-down menu, edit html/menu_categories.php and look for the following code at line 32:

  print "<a href='#'>".$category["name"]."</a>";

...and REPLACE with:

  print "<a href='#'>".tapestry_displayCategory($category["name"])."</a>";

And also the following code at line 36:

  print "<a href='".$itemHREF."'>".$category["name"]."</a>";

..and REPLACE with:

  print "<a href='".$itemHREF."'>".tapestry_displayCategory($category["name"])."</a>";

Cheers,
David.
--
PriceTapestry.com

Submitted by Nic0904 on Mon, 2016-02-29 15:09

Hi David,

Thanks David it worked like a dream, only issue I had is not all of the categories are number prefixed so I changed the code in tapestry.php to only drop the first word if it prefixed with a number. No PHP expert so probably a better way of doing it, but it works:

function tapestry_displayCategory($category)
{
  if (ctype_digit($category[0])) {
    $words = explode(" ",$category);
    array_shift($words);
    return implode(" ",$words);
  }
  else {
    return $category;
  }
}

Cheers
Dave

Submitted by support on Mon, 2016-02-29 15:14

Looks fine, Dave!

If you wanted to extend the numerical checking to the whole first "word", you could use something like;

  function tapestry_displayCategory($category)
  {
    $words = explode(" ",$category);
    if (is_numeric($words[0]))
    {
      array_shift($words);
      return implode(" ",$words);
    }
    else
    {
      return $category;
    }
  }

Cheers,
David.
--
PriceTapestry.com