Category sorting for API => JavaScript

All about webserver configurations, PHP and databases.

Moderator: Thorsten

Post Reply
simonhudin
Posts: 5
Joined: Mon Sep 28, 2015 8:28 am

Category sorting for API => JavaScript

Post by simonhudin »

Hi all,

Just want to add a tip which might help someone using the API for front end.

Code: Select all

        
api.php?action=getCategories
Above call returns catagories ordered by their ID and since moving categories around in the top level will effectively change their ID, the ordering will break for when looping through the object in JavaScript. If you intend to create a sub-level UL LI UL LI for instance.

Easy fix.

In /phpmyfaq/inc/PMF/Catagory.php look for

Code: Select all

private function getOrderedCategories($withperm = true){}
and change

Code: Select all

GROUP BY
                fc.id, fc.lang, fc.parent_id, fc.name, fc.description, fc.user_id
            ORDER BY
                fc.id",
            PMF_Db::getTablePrefix(),
to

Code: Select all

GROUP BY
                fc.id, fc.lang, fc.parent_id, fc.name, fc.description, fc.user_id
            ORDER BY
                fc.parent_id,fc.id",
            PMF_Db::getTablePrefix(),
Result will be an ordering where all top level categories are situated first within the returned JSON but still ordered as per ordering in Admin. Followed by their closest sub-level categories, followed by their closest sub-level categories etcetera.

Cheers
/Simon
Thorsten
Posts: 15560
Joined: Tue Sep 25, 2001 11:14 am
Location: #phpmyfaq
Contact:

Re: Category sorting for API => JavaScript

Post by Thorsten »

Hi Simon,

thanks, I added this into phpMyFAQ 2.9.0-beta :-)

bye
Thorsten
phpMyFAQ Maintainer and Lead Developer
amazon.de Wishlist
Post Reply