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

[eluser]shinokada[/eluser]
Thanks for your help.

I followed your instruction and I have the following model.
Code:
<?php
class MMenu2 extends Model{

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

    function generateTree(&$tree, $parentid = 0) {
        
        $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($r->id, $tree[$r->id]['children']);
            }
        }
    }

}
?>

However this give an error of this.

Code:
A Database Error Occurred

Error Number: 1054

Unknown column 'Array' in 'where clause'

SELECT * FROM (`menus`) WHERE `parentid` = Array
#12

[eluser]theprodigy[/eluser]
To more changes:
1. Change all your $r->id into $r['id'] as $r is now an array
2. You switched the parameters in the function declaration, but you are still calling it down below by the original way
Code:
function generateTree(&$tree, $parentid = 0) {...}
is correct, but you need to also change
Code:
$this->generateTree($r->id, $tree[$r->id]['children']);
to
Code:
$this->generateTree($tree[$r['id']]['children'], $r['id']);

Change those and let's see where we are at.
#13

[eluser]shinokada[/eluser]
I corrected to this.
Code:
function generateTree(&$tree, $parentid = 0) {
        
        $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']);
            }
        }
    }


And error is the following.
Code:
A Database Error Occurred

Error Number: 1054

Unknown column 'Array' in 'where clause'

SELECT * FROM (`menus`) WHERE `parentid` = Array
#14

[eluser]theprodigy[/eluser]
As I take a look at it, try print_r()ing $r inside the foreach loop (make sure you comment out the rest of the code so that you don't keep getting errors)

Code:
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']);
    print_r($r);
}
#15

[eluser]shinokada[/eluser]
Error with
Code:
A Database Error Occurred

Error Number: 1054

Unknown column 'Array' in 'where clause'

SELECT * FROM (`menus`) WHERE `parentid` = Array
#16

[eluser]theprodigy[/eluser]
found the problem.

Your controller didn't get changed around.

Code:
$data['list'] = $this->MMenu2->generateTree($parentid, $tree);

needs to be

Code:
$data['list'] = $this->MMenu2->generateTree($tree, $parentid);
#17

[eluser]shinokada[/eluser]
It outputs
Code:
Array ( [id] => 24 [name] => Main menu [shortdesc] => mainmenu [status] => active [parentid] => 0 [order] => 0 ) Array ( [id] => 25 [name] => Galleri 1 [shortdesc] => galleri1 [status] => active [parentid] => 0 [order] => 0 )

An error of
Code:
A PHP Error was encountered

Severity: Warning

Message: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\ci_menu\system\application\models\mmenu2.php:24)

Filename: codeigniter/Common.php

Line Number: 360

And another error
Code:
An Error Was Encountered

Unable to load the requested file: list2Array.php
#18

[eluser]theprodigy[/eluser]
ok, remove the print_r() line, and uncomment the rest of the foreach loop.
Code:
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']);
}
#19

[eluser]shinokada[/eluser]
This returns nothing.

Do I need to return $r or $data?
#20

[eluser]theprodigy[/eluser]
Quote:function makelist($parentid){
$this->load->model('MMenu2');
$tree = array();
$parentid = 0;
$data['list'] = $this->MMenu2->generateTree($parentid, $tree);

$this->load->view('list2'.$data);
}

In your controller, you are passing in $tree as a reference (based on your model function declaration)
Instead of:
Code:
$data['list'] = $this->MMenu2->generateTree($parentid, $tree);
try
Code:
$this->MMenu2->generateTree($parentid, $tree);
$data['list'] = $tree;

also change
Code:
$this->load->view('list2'.$data);
to
Code:
$this->load->view('list2',$data);
(change the period to a comma inside the parenthesis)




Theme © iAndrew 2016 - Forum software by © MyBB