Welcome Guest, Not a member yet? Register   Sign In
Model problem
#21

[eluser]shinokada[/eluser]
I have this in controller.


Code:
class Menu2 extends Controller {
  
  function Menu2(){
    parent::Controller();
  
  }

  function makelist($parentid){
    $this->load->model('MMenu2');
    $tree = array();
    $parentid = 0;
    $this->MMenu2->generateTree($parentid, $tree);
    $data['list'] = $tree;
    $this->load->view('list2',$data);
  }
}

And the model

Code:
class MMenu2 extends Model{

    function MMenu(){
    parent::Model();
    }

    function generateTree(&$tree, $parentid = 0) {
        $this->db->select('id,parentid,name');
        $this->db->where ('parentid',$parentid);
        
        // $sql = 'SELECT * FROM menus WHERE parentid = $parentid';
        $res = $this->db->get('menus');
        if ($res->num_rows() > 0) {
            foreach ($res->result_array() as $r) {
              
            // push found result onto existing tree
                $tree[$r['id']] = $r;
                // create placeholder for children
                $tree[$r['id']]['children'] = array();
                // find any children of currently found child
               $this->generateTree($tree[$r['id']]['children'],$r['id']);
            }
        }
    }

}

When I visit http://127.0.0.1/ci_menu/index.php/menu2/makelist/24,
it gives an error.
Code:
A Database Error Occurred

Error Number: 1054

Unknown column 'Array' in 'where clause'

SELECT `id`, `parentid`, `name` FROM (`menus`) WHERE `parentid` = Array
#22

[eluser]theprodigy[/eluser]
sorry, when I copied and pasted the code from the controller, I forgot to switch the $parentid and $tree fields in the call
Change
Code:
$this->MMenu2->generateTree($parentid, $tree);
to
Code:
$this->MMenu2->generateTree($tree, $parentid);
#23

[eluser]shinokada[/eluser]
I am greatly thankful for your help.

Regards.
#24

[eluser]theprodigy[/eluser]
Does this mean that it's working now?
Good stuff!!
Good luck on the rest of your project!
Let us know if there is anything else you need help with.
#25

[eluser]shinokada[/eluser]
I applied it to my CMS and it produces the following array.
Code:
Array
(
    [24] => Array
        (
            [id] => 24
            [name] => Main menu
            [shortdesc] => mainmenu
            [status] => active
            [parentid] => 0
            [order] => 0
            [children] => Array
                (
                    [65] => Array
                        (
                            [id] => 65
                            [name] => Main 4
                            [shortdesc] =>
                            [status] => active
                            [parentid] => 24
                            [order] => 0
                            [children] => Array
                                (
                                )

                        )

                    [64] => Array
                        (
                            [id] => 64
                            [name] => Main 3
                            [shortdesc] =>
                            [status] => active
                            [parentid] => 24
                            [order] => 0
                            [children] => Array
                                (
                                )

                        )

                    [62] => Array
                        (
                            [id] => 62
                            [name] => Main 2
                            [shortdesc] =>
                            [status] => active
                            [parentid] => 24
                            [order] => 0
                            [children] => Array
                                (
                                )

                        )

                    [59] => Array
                        (
                            [id] => 59
                            [name] => Main 1
                            [shortdesc] =>
                            [status] => active
                            [parentid] => 24
                            [order] => 0
                            [children] => Array
                                (
                                    [66] => Array
                                        (
                                            [id] => 66
                                            [name] => Main 1-1
                                            [shortdesc] =>
                                            [status] => active
                                            [parentid] => 59
                                            [order] => 0
                                            [children] => Array
                                                (
                                                )

                                        )

                                )

                        )

                )

        )

    [25] => Array
        (
            [id] => 25
            [name] => Galleri 1
            [shortdesc] => galleri1
            [status] => active
            [parentid] => 0
            [order] => 0
            [children] => Array
                (
                )

        )

)

I want to display it in unordered list and table.

I tried it the unordered list first.

The goal is
Code:
<ul>
   <li>Main menu
        <ul>
            <li>Main 4</li>
            <li>Main 3</li>
            ...
            <li>Main 1
                <ul>
                    <li>Main 1-1</li>
                </ul>
            </li>
        </ul>
   </li>
   <li>Galleri 1</li>

</ul>


I attempted the following view.

Code:
if (count($navlist)){
  echo "\n<ul>";
  foreach ($navlist as $key => $list){
      foreach ($list as $topkey => $toplist){
        echo "\n<li class='cat'>\n";
        echo anchor("welcome/cat/",$toplist['name']);
        
        if (isset($toplist['children'])){
        echo "\n<ul>";
            foreach ($toplist['children'] as $subkey => $subname){
                echo "\n<li class='subcat'>\n";
                echo anchor("welcome/cat/",$subname);    
                echo "\n</li>";
            }
        echo "\n</ul>";
        }
    echo "\n</li>\n";
    }
  }
  echo "\n</ul>\n";
}

And this gives 2, M, m etc and errors.
Code:
A PHP Error was encountered

      Severity: Warning

      Message: Invalid argument supplied for foreach()

      Filename: views/admin_menu_home.php

      Line Number: 18

If you can tell me what I am wrong, I will appreciate it.
#26

[eluser]theprodigy[/eluser]
your controller is showing $data['list'], but your view is using $navlist, did you rename the variable?
#27

[eluser]theprodigy[/eluser]
Quote:foreach ($navlist as $key => $list){
foreach ($list as $topkey => $toplist){
echo "\n<li class='cat'>\n";
echo anchor("welcome/cat/",$toplist['name']);

if (isset($toplist['children'])){
echo "\n<ul>";
foreach ($toplist['children'] as $subkey => $subname){
echo "\n<li class='subcat'>\n";
echo anchor("welcome/cat/",$subname);
echo "\n</li>";
}
echo "\n</ul>";
}
echo "\n</li>\n";
}
}
you're doing a foreach directly inside a foreach without making sure that the current element is an array. Only one element in $list is an array (the children element).

Your first loop through the first foreach, $key is set to id, and $list is set to 24. You then try to do a foreach on 24, which is throwing the error.
#28

[eluser]Jondolar[/eluser]
Change
Code:
if (isset($toplist['children'])){

to the following:
Code:
if (sizeof($toplist['children']) > 0){

The real fix is to not assign ['children'] as an array in your model if there are no children.

The fix above "assumes" that it is always an array but sometimes it is a size of 0 and therefore don't process the array.

Good luck with your project.
#29

[eluser]shinokada[/eluser]
My controller is this.
Code:
class Menus extends Controller {
  function Menus(){
    parent::Controller();
    session_start();
    
    if (!isset($_SESSION['userid']) || $_SESSION['userid'] < 1){
        redirect('welcome/verify','refresh');
    }
  }
  

  function index(){
    $data['title'] = "Manage Menus";
    $data['main'] = 'admin_menu_home';
        $tree = array();
        $parentid = 0;
    $this->MMenus->generateTree($tree,$parentid);
        $data['navlist'] = $tree;
    $this->load->vars($data);
    $this->load->view('dashboard');
    
  }

...
...

I changed it as Jondolar suggested. However it still gives errors.

Suggestions will be appreciated.
#30

[eluser]theprodigy[/eluser]
can you post a new view please.




Theme © iAndrew 2016 - Forum software by © MyBB