• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Order by name not work with array.

#1
I am trying to be able to use two order by in my get_forums function

This is my out put I am try order the parents names so it would show like the codeigniter categories first but shows php categories first.

$this->db->order_by('fid', 'asc'); // This is working

$this->db->order_by('name', 'asc');


Code:
Array
(
    [0] => Array
        (
            [fid] => 41
            [pid] => 0
            [name] => PHP
        )

    [1] => Array
        (
            [fid] => 45
            [pid] => 41
            [name] => str_replace
        )

    [2] => Array
        (
            [fid] => 46
            [pid] => 41
            [name] => password_hash
        )

    [3] => Array
        (
            [fid] => 47
            [pid] => 0
            [name] => Codeigniter
        )

    [4] => Array
        (
            [fid] => 48
            [pid] => 47
            [name] => How to install
        )

    [5] => Array
        (
            [fid] => 49
            [pid] => 47
            [name] => Controllers
        )

    [6] => Array
        (
            [fid] => 50
            [pid] => 49
            [name] => Testing
        )


Model


PHP Code:
<?php

class Forum_model extends CI_Model {

    public function get_forums($pid '0') {
        $categories_data = array();

        $this->db->where('pid''0');
        $this->db->or_where('pid >'$pid);
        $this->db->order_by('fid''asc');
        $this->db->order_by('name''asc');
        $query $this->db->get('forum');

        foreach ($query->result() as $result) {

            $categories_data[] = array(
                'fid' => $result->fid,
                'pid' => $result->pid,              
                
'name' => $result->name
            
);

            if ($result->pid 0) {

                $category_children[] = $this->make_parent_list($result->pid);

                if ($category_children) {
                       $categories array_merge($category_children$categories_data);
                }      

            
}

        }
          
        return $categories_data
;
    }

    function make_parent_list($fid) {
        $forum_data = array();

        $this->db->where('fid'$fid);
        $forum_query $this->db->get('forum');

        foreach ($forum_query->result() as $forum) {

            $forum_data[] = array(
                'fid' => $forum->fid,
                'pid' => $forum->pid,              
                
'name' => $forum->name
            
);

            if ($forum->pid 0) {
            
                $forum_data
[] = array(
                    'fid' => $forum->fid,
                'pid' => $forum->pid,              
                
'name' => $forum->name
                
);

                $forum_children $this->make_parent_list($forum->pid);

                if ($forum_children) {
                    $forum_data array_merge($forum_children$forum_data);
                }           

            
}
        }

        return $forum_data;
    }



Attached Files
.php   Forum_model.php (Size: 2.27 KB / Downloads: 118)
There's only one rule - please don't tell anyone to go and read the manual.  Sometimes the manual just SUCKS!
Reply

#2
You're ordering by "fid" first and "name" second. Unless there's a duplicate "fid" in the result set, the "name" ordering will never be used. What did you expect?
Reply

#3
(12-07-2016, 03:20 AM)Narf Wrote: You're ordering by "fid" first and "name" second. Unless there's a duplicate "fid" in the result set, the "name" ordering will never be used. What did you expect?

In my make parent list function which is called in get_forums I have another fid I am not sure if that is effecting it I have attached the Model file to the OP. Not sure if I done it all correct if any improvements can make let me know cheers.
There's only one rule - please don't tell anyone to go and read the manual.  Sometimes the manual just SUCKS!
Reply

#4
i dont think is that possible to sort two column through a single query but you can do one thing first receive the data which you got from the database then again sort the array value using rsort().
Reply

#5
(12-07-2016, 03:25 AM)wolfgang1983 Wrote:
(12-07-2016, 03:20 AM)Narf Wrote: You're ordering by "fid" first and "name" second. Unless there's a duplicate "fid" in the result set, the "name" ordering will never be used. What did you expect?

In my make parent list function which is called in get_forums I have another fid I am not sure if that is effecting it I have attached the Model file to the OP. Not sure if I done it all correct if any improvements can make let me know cheers.

I don't know what you're talking about ... I don't need to look at your entire model, you've shown this:

Code:
$this->db->order_by('fid', 'asc');
$this->db->order_by('name', 'asc');

You're ordering by fid first and name second. That's what I'm saying.

(12-07-2016, 03:54 AM)kishorpant1991 Wrote: i dont think is that possible to sort two column through a single query but you can do one thing first receive the data which you got from the database then again sort the array value using rsort().

Yes you can, that's his entire problem.
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.