4

I'm a starter in codeigniter and php, and im facing this problem

A PHP Error was encountered

Severity: Notice

Message: Undefined variable: query

Filename: inc/header_main_view.php

Line Number: 175

A PHP Error was encountered

VIEW:

<ul class="dropdown-menu">

    <li>
        <!-- inner menu: contains the actual data -->
        <ul class="menu">
            <?php foreach($query as $row) :?>
                <li><!-- Task item -->
                    <a href="#">
                        <h3>
                            <?php echo $row->id_task; ?><?php echo $row->name; ?>
                            <small class="pull-right">20%</small>
                        </h3>
                        <div class="progress xs">
                            <div class="progress-bar progress-bar-aqua" style="width: 20%" role="progressbar" aria-valuenow="20" aria-valuemin="0" aria-valuemax="100">
                                <span class="sr-only">20% Complete</span>
                            </div>
                        </div>
                    </a>
                </li><!-- end task item -->
            <?php endforeach; ?>
        </ul>
    </li>
    <li class="footer">
        <a href="<?=base_url()?>index.php/dashboard/g_tasks_menu">View all tasks</a>
    </li>
</ul>

CONTROLLER

public function get_todo($id = null)
    {
        $this->_require_login();
        $this->load->model('todo_model');
        if ($id != null) {
            $result = $this->todo_model->get([
                'id' => $id,
                'id_user' => $this->session->userdata('id_user')
            ]);
        } else {
            $result = $this->todo_model->get([
                'id_user' => $this->session->userdata('id_user')
            ]);
        }

        return $result;
    }

OTHER CONTROLLER

 public function index()
    {
        require('api.php');
        $api = new api();
        $query = $api->get_todo();

        $this->load->view('dashboard/inc/header_main_view', $query);
        $this->load->view('dashboard/admin_pages/dashboard_view');
        $this->load->view('dashboard/inc/footer_main_view');

    }

MODEL

 public function get($id = null, $order_by = null)
    {       
        if (is_numeric($id)) {
            $this->db->where($this->_primary_key, $id);
        } 

        if (is_array($id)) {
            foreach ($id as $_key => $_value) {
                $this->db->where($_key, $_value);
            }
        }

        $q = $this->db->get($this->_table);
        return $q->result_array();
    }

Can someone please tell me why I am getting the error.

2 Answers 2

4

It will be like this

$data['query'] = $api->get_todo();
$this->load->view('dashboard/inc/header_main_view', $data);
Sign up to request clarification or add additional context in comments.

Comments

2

You did not passed variable to view right way.$query is not available at your view.CI converts array or object keys as variable which is sent from controller.See Adding Dynamic Data to the View From the doc.

You need to fix your controller like this

$data['query'] = $api->get_todo();
$this->load->view('dashboard/inc/header_main_view', $data);    
//your view will get all key of $data as variable.
//your view will get $query from above code

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.