1
votes

I am newbie in CodeIgniter. When I try to data passing with controller and view and model, I have got this:

Syntax error: Unexpected '$data'(T_VARIABLE) in c:\xampp\htdocs\ci\application\controllers\users.php on line 10

Here is my controller users.php code:

<?php
class Users extends CI_Controller
    {
        public function show()
            {
                $data['results']=$this->user_model->get_users();
                $this->load->view('user_view',$data);
             }
    }
?>

At views folder user_view.php code:

<body>
<?php
foreach($results as $object){
    echo $object->username;
}
?>
</body>

At model folder user_model.php code

<?php
class User_model extends CI_Model
    {
        public function get_users()
            {
                $query=$this->db->get('users');
                return $query->result();
            }
    }
?>

How can I solve this error?

6

6 Answers

1
votes

Why do your error says c:\xampp\htdocs\ci\application\users.php shouldn't it have been c:\xampp\htdocs\ci\application\controller\users.php`

0
votes

In your controller

public function show()
{
    $data['results']=$this->user_model->get_users();
    $this->load->view('user_view',$data);
}

In your model

public function get_users(){
    $query=$this->db->get('users');
    $result = $query->result_array(); //added
    return $result; //added
}

In view

<body>
<?php
    foreach($results as $new_results) //changed
    {
        echo $new_results['username']; //changed
    }
?>
</body>
0
votes

You are trying to access wrong ARRAY in your view. In your controller array is $data['results'] not $data['result']. So you need to access $results in foreach loop.

In your view Change from

foreach($result as $object){

To

foreach($results as $object){
0
votes

Use $results in foreach loop and check

0
votes

First of all, you have to load your model as:

$this->load->model('User_model','user_model');

Then you can use the function defined in that model as :

$this->user_model->get_users();
0
votes

You have one more error in your code i.e. you have not loaded the model in the function

update your code to add the following line in first line of show() function

$this->load->model('user_model');