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

[eluser]shinokada[/eluser]
I am hoping that someone takes me to the right direction with the following code.

I am getting the following errors.

Quote:A PHP Error was encountered

Severity: Warning

Message: Illegal offset type

Filename: models/mmenu2.php

Line Number: 19
A PHP Error was encountered

Severity: Notice

Message: Trying to get property of non-object

Filename: models/mmenu2.php

Line Number: 21

...


Model

Code:
<?php
class MMenu2 extends Model{

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

    function generateTree($parentid = 0, &$tree) {
        
        $this->db->where ('parentid',$parentid);
        
        // $sql = 'SELECT * FROM menus WHERE parentid = $parentid';
        $res = $this->db->get('menus');
        if ($res) {
            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
                $tree = $this->generateTree($r->id, $tree[$r->id]['children']);
            }
        }
    }

}
?>

Controller

Code:
<?php

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

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

[eluser]theprodigy[/eluser]
Code:
$tree = $this->generateTree($r->id, $tree[$r->id]['children']);

Your last line in your foreach loop may be the problem.
In your foreach, you asked for result_array(), but here you are trying to access it is an object.
#3

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

I changed it to

Code:
$tree = $this->generateTree($r['id'], $tree[$r]['id']['children']);

I get errors on the following lines.

$tree[$r]['id'] = $r;

$tree[$r]['id']['children'] = array();

etc.

I also tried
Code:
class MMenu2 extends Model{

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

    function generateTree($parentid = 0, &$tree) {
        
        $this->db->where ('parentid',$parentid);
        
        // $sql = 'SELECT * FROM menus WHERE parentid = $parentid';
        $res = $this->db->get('menus');
        if ($res) {
            foreach ($res->result() 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
                $tree = $this->generateTree($r->id, $tree[$r->id]['children']);
            }
        }
    }

}

But it gives the following error.
Quote:Fatal error: Cannot use object of type stdClass as array in C:\xampp\htdocs\ci_menu\system\application\models\mmenu2.php on line 19

And line 19 is

$tree[$r->id]['children'] = array();

Any more suggestions?
#4

[eluser]theprodigy[/eluser]
try changing
Code:
if($res)
to
Code:
if($res->num_rows() > 0)
#5

[eluser]shinokada[/eluser]
It gives the same error.
#6

[eluser]theprodigy[/eluser]
ok, humor me for a sec.

At the bottom of your generateTree, you have
Code:
$tree = $this->generateTree($r->id, $tree[$r->id]['children']);

but that function does not return anything.
Try just calling the function and not setting $tree equal to it
Code:
$this->generateTree($r->id, $tree[$r->id]['children']);
#7

[eluser]shinokada[/eluser]
It still gives the same error.
#8

[eluser]theprodigy[/eluser]
just for verification, can you please post your MMenu model, and the full error message.
#9

[eluser]shinokada[/eluser]
Model

Full

Code:
<?php
class MMenu2 extends Model{

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

    function generateTree($parentid = 0, &$tree) {
        
        $this->db->where ('parentid',$parentid);
        
        // $sql = 'SELECT * FROM menus WHERE parentid = $parentid';
        $res = $this->db->get('menus');
        if ($res) {
            foreach ($res->result() 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
                $tree = $this->generateTree($r->id, $tree[$r->id]['children']);
            }
        }
    }

}
?>

Error

Full

Code:
Fatal error: Cannot use object of type stdClass as array in C:\xampp\htdocs\ci_menu\system\application\models\mmenu2.php on line 19

Full DB
Code:
CREATE TABLE IF NOT EXISTS `menus` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL,
  `shortdesc` varchar(255) NOT NULL,
  `status` enum('active','inactive') NOT NULL,
  `parentid` int(11) NOT NULL,
  `order` int(11) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=62 ;

--
-- Dumping data for table `menus`
--

INSERT INTO `menus` (`id`, `name`, `shortdesc`, `status`, `parentid`, `order`) VALUES
(24, 'Main menu', 'mainmenu', 'active', 0, 0),
(25, 'Galleri 1', 'galleri1', 'active', 0, 0),
(61, 'Design sub1', '', 'active', 59, 0),
(59, 'Design', '', 'active', 24, 0),
(60, 'illustrasjon 1', '', 'active', 25, 0);

-- --------------------------------------------------------

--
-- Table structure for table `tasks`
--

CREATE TABLE IF NOT EXISTS `tasks` (
  `task_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `parent_id` int(10) unsigned NOT NULL DEFAULT '0',
  `task` varchar(100) NOT NULL,
  `date_added` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `date_completed` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
  PRIMARY KEY (`task_id`),
  KEY `parent` (`parent_id`),
  KEY `added` (`date_added`),
  KEY `completed` (`date_completed`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=29 ;

--
-- Dumping data for table `tasks`
--

INSERT INTO `tasks` (`task_id`, `parent_id`, `task`, `date_added`, `date_completed`) VALUES
(25, 17, 'Grandchild of parent 1', '2009-12-08 12:37:09', '2009-12-08 12:37:09'),
(24, 17, 'Grandchild of parent 1', '2009-10-08 16:32:02', '0000-00-00 00:00:00'),
(22, 0, 'Parent 4', '2009-10-08 16:28:51', '0000-00-00 00:00:00'),
(23, 11, 'Child 2 of parent 1', '2009-10-08 16:29:07', '0000-00-00 00:00:00'),
(21, 0, 'Parent 3', '2009-10-08 16:28:43', '0000-00-00 00:00:00'),
(17, 11, 'Child 1 of parent 1', '2009-12-08 12:37:16', '2009-12-08 12:37:16'),
(19, 0, 'Parent 1', '2009-10-07 07:15:19', '0000-00-00 00:00:00'),
(20, 0, 'Parent 2', '2009-10-07 08:19:05', '2009-10-07 08:19:05'),
(11, 0, 'Parent1', '2009-10-05 21:30:16', '0000-00-00 00:00:00'),
(26, 11, 'write report', '2009-12-08 13:50:12', '0000-00-00 00:00:00'),
(27, 26, 'make exam', '2009-12-08 13:51:40', '0000-00-00 00:00:00'),
(28, 26, 'Pre IB exam', '2009-12-08 13:51:52', '0000-00-00 00:00:00');
#10

[eluser]theprodigy[/eluser]
Quote:foreach ($res->result() 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
$tree = $this->generateTree($r->id, $tree[$r->id]['children']);
}

I think the problem lies in:
Code:
// push found result onto existing tree
$tree[$r->id] = $r;
// create placeholder for children
$tree[$r->id]['children'] = array();

$r is an object. So
Code:
$tree[24] = stdObject()
$tree[25] = stdObject()
etc

Then you turn around and try to add another dimension onto the array with the next line
Code:
$tree[$r->id]['children'] = array();

You were getting a different error when you were using result_array(), then you are now with result().

Try changing back to result_array()
Change your if to
Code:
if ($res->num_rows() > 0) {...}

and remove the '$tree =' from
Code:
$tree = $this->generateTree($r->id, $tree[$r->id]['children']);

It's also bad form to have an optional parameter in front of a required parameter in a function declaration
Code:
function generateTree( $parentid = 0, &$tree ){...}
should be
Code:
function generateTree( &$tree, $parentid = 0 ){...}




Theme © iAndrew 2016 - Forum software by © MyBB