Welcome Guest, Not a member yet? Register   Sign In
Problem with Selecting Data form Database
#1

[eluser]haris244808[/eluser]
Hi there,
I created a users_model.php where i select all users :
Code:
function select_all_users(){

  $query = $this->db->get('users');

  if ($query->num_rows() > 0 ) {
   return $query->result();
  }
  
}

then i have users.php controller where i am trying to get all the data from users table:
Code:
function select_all(){

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

  $data['query'] = $this->users_model->select_all_users();

  print_r($query);
  die();

  $this->load->view('newFile_view', $data);
}

but i get an error as query is undefined variable::
i tried $query->id , $query['id'] but still same problem... I am trying to stopre datas to an array, then get the specific ones...

Can someone help me with this?

Thnx
#2

[eluser]gojo[/eluser]
[quote author="haris244808" date="1362871954"]Hi there,
I created a users_model.php where i select all users :
Code:
function select_all_users(){

  $query = $this->db->get('users');

  if ($query->num_rows() > 0 ) {
   return $query->result();
  }
  
}

then i have users.php controller where i am trying to get all the data from users table:
Code:
function select_all(){

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

  $data['query'] = $this->users_model->select_all_users();

  print_r($query);
  die();

  $this->load->view('newFile_view', $data);
}

but i get an error as query is undefined variable::
i tried $query->id , $query['id'] but still same problem... I am trying to stopre datas to an array, then get the specific ones...

Can someone help me with this?

Thnx[/quote]

have you tried print_r($data['query']);
#3

[eluser]haris244808[/eluser]
Quote:have you tried print_r($data['query']);

actually that will work, but wha ti want is to pass all the data from db to $data['query'] array, and assign them to view ex: $this->load->view('page' , $data); So them i can show them through looping:
ex: foreach($query as $result);
print_r ($result);
#4

[eluser]TheFuzzy0ne[/eluser]
Welcome to the CodeIgniter forums!

If you put that logic in your view, it will work exactly as you expect.

When you pass $data to your view, each entry of the array is extracted and made available to your view. So $data['query'] becomes $query within your view. Outside of the view, you still need to access it using $data['query'].
#5

[eluser]haris244808[/eluser]
[quote author="TheFuzzy0ne" date="1362915452"]Welcome to the CodeIgniter forums!

If you put that logic in your view, it will work exactly as you expect.

When you pass $data to your view, each entry of the array is extracted and made available to your view. So $data['query'] becomes $query within your view. Outside of the view, you still need to access it using $data['query'].[/quote]

THank you for ur fast reply.

I know how data are shown in view, but thats the problem, because it says that $query is undevfined variable in my view.

Here is my controller so far:
Code:
$this->load->model('users_model');

  $data['query'] = $this->users_model->select_all_users();

  $this->load->view('newFile_view', $data);

so when i go ti newFil_view and try to print_r($query);
ir says that $query its undefined variable
#6

[eluser]TheFuzzy0ne[/eluser]
That's a very strange issue.

Do you have a the following file: ./application/libraries/MY_Loader.php? If so, what's in it?

Alternatively, does this work?

Code:
$this->load->model('users_model');
$this->load->vars(array(
    'query' => $this->users_model->select_all_users(),
));
$this->load->view('newFile_view');

Have you modified any of the files in the system directory? If you have, you shouldn't have, and you should delete it and copy it over again from the zip file.
#7

[eluser]haris244808[/eluser]
[quote author="TheFuzzy0ne" date="1362916066"]That's a very strange issue.

Do you have a the following file: ./application/libraries/MY_Loader.php? If so, what's in it?

Alternatively, does this work?

Code:
$this->load->model('users_model');
$this->load->vars(array(
    'query' => $this->users_model->select_all_users(),
));
$this->load->view('newFile_view');

Have you modified any of the files in the system directory? If you have, you shouldn't have, and you should delete it and copy it over again from the zip file.[/quote]

i didnt change anything in core files...also i didnt create any of my own loader...so application/libraries/ has nothing inside....
I am changing the System folder with the latest codeigniter just to make sure everything is set properly.

Yes i am wondering too why this problem is happening...i didn have problems before showing data in this form
#8

[eluser]haris244808[/eluser]
i changed the system folder with the latest codeigniter, still same problem;

here is users_model method that i am calling btw:
Code:
function select_all_users(){

  $query = $this->db->get('users');
  return $query->result();
}
#9

[eluser]TheFuzzy0ne[/eluser]
I see no reason why that shouldn't work. Are you sure you're calling the method you think you're calling?

Would it be possible to post the entire contents of your controller?

What you've posted looks fine to me, so the problem must be elsewhere.
#10

[eluser]haris244808[/eluser]
[quote author="TheFuzzy0ne" date="1362917399"]I see no reason why that shouldn't work. Are you sure you're calling the method you think you're calling?

Would it be possible to post the entire contents of your controller?

What you've posted looks fine to me, so the problem must be elsewhere.[/quote]

actually when i try to:
Code:
var_dump($data);
die();
in the controller, it shows the data, which means that data are returned well...put i cannot understand why it doesnt pass them to view

users.php controller:

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

class Users extends CI_Controller
{

function __construct()
{
  parent::__construct();
}

function index(){

  //$this->load->view('userManagement_view');
}

function select_all(){

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

  $data['query'] = $this->users_model->select_all_users();
  
  $this->load->view('newFile_view', $data);
}

}

users_model.php model:
Code:
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Users_model extends CI_Model
{

function __construct()
{
  parent::__construct();
}

function select_all_users(){

  $query = $this->db->get('users');
  return $query->result();
}
}




Theme © iAndrew 2016 - Forum software by © MyBB