Looking to Build/Redesign a Website or Blog ?

To speak to Rakshit, call +91 968 704 2303

How to sort multidimensional array in Entry Categories plugin of expression engine

Today i face a situation where i need to sort my two dimensional array by column name(table_order).I am working in expression engine , installed one of expression engine plugin(Plugin name: Entry Categories). In this plugin, it is not giving any option if you want to get categories order by particular column name. Below is the way how to get the ordered data by particular table field name(below is the code for order by cat_order).

class TableSorter
{
    protected $column;
    function __construct($column)
    {
        $this->column = $column;
    }
    function sort($table)
    {
        usort($table, array($this, 'compare'));
        return $table;
    }
    function compare($a, $b)
    {
        if ($a[$this->column] == $b[$this->column])
        {
              return 0;
        }
        return ($a[$this->column] < $b[$this->column]) ? -1 : 1;
    }
}

Find file pi.entry_cats.php in system >> plugin dir , add this code just above class Entry_cats {.

$sorter = new TableSorter('cat_order'); // sort by cat_order
$entry_cats = $sorter->sort($entry_cats);

In the same file , find add this code above for ($i = 0; $i < count($entry_cats); $i++)

Below code is required only if you want to sort data by cat_order as in plugin(Plugin name: Entry Categories) the code will not give you cat_order value in array.

Find :
    SELECT group_id, parent_id, cat_name, cat_url_title, cat_description, cat_image
Replace :
    SELECT group_id, parent_id, cat_name, cat_url_title, cat_description, cat_image, cat_order
Find :
    $entry_cats[$i]['cat_image'] = $query2->row['cat_image'];
Replace :
    $entry_cats[$i]['cat_image'] = $query2->row['cat_image'];
    $entry_cats[$i]['cat_order'] = $query2->row['cat_order'];

Reference of sorting taken from : http://bit.ly/3fo7mk

To know more about programming,JavaScript issues,jQuery,Expression Engine,MYSQL database,php info,php editor,programming php,Open-source,php help and php script , subscribe to our feed by entering email address below. You will get updates via email about every tutorial posted on this site . It will not take more than a sec.

Enter your email address:
 
Feel free to ask any question. Just leave your comment below and we will answer your comment with in 24 hours.
Share:

2 Comments

Leave a Reply

Your email address will not be published.