Page 1 of 1

Category sorting for API => JavaScript

Posted: Thu Nov 05, 2015 10:41 am
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

Re: Category sorting for API => JavaScript

Posted: Thu Nov 05, 2015 9:45 pm
by Thorsten
Hi Simon,

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

bye
Thorsten