1
votes

Is this posible to send data from controller like this?

Controller test.php

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

    class Test extends CI_Controller {

        public $data = array(
            'modul'         => 'user',
            'title_meta'    => 'User',
            'breadcrumb'    => 'User',
            'pesan'         => '',
            'pagination'    => '',
            'main_view'     => 'administrator/user',
            'form_action'   => '',
            'form_value'    => '',
        );

        public function __construct()
        {
            parent::__construct();      
            $this->load->model('test_model', 'test', TRUE);
        }

        public function index()
        {
            $data['query'] = $this->test->get_all();
            $this->load->view('test',$data);
        }

    }

Model test_model.php

<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Test_model extends CI_Model {

    function get_all() {
        $query = $this->db->get('snb_user');
        return $query->result_array();
    }
}

View test.php

<?php
foreach($query as $row)
{
    echo $row['UserID'];
    echo $row['UserName'];
    echo $row['UserEmail'];
}

echo $breadcrumb;
?>

When i run this, i get this error:

A PHP Error was encountered

Severity: Notice

Message: Trying to get property of non-object

Filename: views/test.php

Line Number: 4 A PHP Error was encountered

Severity: Notice

Message: Trying to get property of non-object

Filename: views/test.php

Line Number: 4 A PHP Error was encountered

Severity: Notice

Message: Trying to get property of non-object

Filename: views/test.php

Line Number: 4 A PHP Error was encountered

Severity: Notice

Message: Trying to get property of non-object

Filename: views/test.php

Line Number: 4 A PHP Error was encountered

Severity: Notice

Message: Undefined variable: breadcrumb

Filename: views/test.php

Line Number: 7

I want to parse data from variable $data on controller and the result of sql query from model, is it posible? Any help would be awesome, thanks!

1
check num_rows() of your result-set and instead of $data user $this->data $this->data['query'] = $this->test->get_all(); - Prashant Srivastav
i did this return $query->num_rows(); the result is 4, view test.php echo $breadcrumb; echo $query; , but still get this: A PHP Error was encountered Severity: Notice Message: Undefined variable: breadcrumb Filename: views/test.php Line Number: 2 - Hari Prasetyo Aji
use $this->load->view('test',$this->data); - Prashant Srivastav
i have tried $this->load->view('test',$this->data);, $breadcrumb variable sucecsfully showing but sql result wont showing. i get this error Message: Undefined variable: query and Message: Invalid argument supplied for foreach() - Hari Prasetyo Aji

1 Answers

0
votes

You haven't declare varible named $breadcrumb in view file or in controller.if you want to remove this notice then you should decalare varible or use isset() or empty() method before using $breadcrumb varible.