12-07-2016, 03:16 AM
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');
Model
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;
}
}
There's only one rule - please don't tell anyone to go and read the manual. Sometimes the manual just SUCKS!