Welcome Guest, Not a member yet? Register   Sign In
Newbie database help
#1

[eluser]the future darlo manager[/eluser]
I started messing around with a controller / model / view at work which would load a list of results. I thought I had the basics down and decided to move onto something real. However I seem to have run up against a brick wall and despite copying what I had working earlier I just can't get this to work.... would anyone care to point me in the right direction? I've put the code below. This will eventually evolve into my admin login but I'm building it up slowly.

Here is my controller which is located in controllers/admin/login.php

Code:
class Login extends Controller {

    function index()
    {
        parent::Controller();
    
        //Loads the form helper and url helpers
        $this->load->helper(array('form', 'url'));
        //Loads the validation library
        $this->load->library('validation');
        
        //Validation Rules (Username and Password both required)
        $rules['username']    = "required|min_length[2]|max_length[20]";
        $rules['password']    = "required|max_length[30]";
        $this->validation->set_rules($rules);
        
        //Sets the entries to repopulate the html form in the event of an error
        $fields['username']    = 'Username';
        $fields['password']    = 'Password';
        $this->validation->set_fields($fields);
        
        //If the validation rules are not passed redisply the form
        if ($this->validation->run() == FALSE) {
            //Loads up the login_index_view
            $this->load->view('admin/login_index_view');
        } else {
            //Validation passed. Grab the form content and put it in an array            
            $userdetails['username'] = $this->input->post('username');
            $userdetails['password'] = $this->input->post('password');
                        
            //Loads the model and the query
            $this->load->model('Admin_model', '', TRUE);
            $data['userquery'] = $this->Admin_model->find_selected_user();
            
            if ($data->num_rows() > 0):
                foreach ($data->result() as $row):
                    echo $row->user_id;
                    echo $row->username;
                    echo $row->password;
                endforeach;
            endif;
            
        }
        
    }
    
}

The validation works fine and I can get the content of the form in my view into the actual script. Now the problems start.

I then have my model which is located in models/amin/admin_model.php

Code:
class Admin_model extends Model {

    function find_selected_user()
    {    
        //Finds the selected user
        $query = $this->db->query('SELECT * FROM loidland_admin_users');
        return $query;
    }
    
}

Obviously I want to pass the variables on from the form to this model to do the query. However first things first I just want to do a query to pull up each of my records and have the controller display them (yes I know I should do this in a view but I just want to get it working first...)

I am not seeing any results and I know the model loads okay because if I put an echo statement in there I see the results in the browser. I am stumped because this is basically a carbon copy of stuff I've managed to get successfully working before.
#2

[eluser]Michael Wales[/eluser]
Code:
$data['userquery'] = $this->Admin_model->find_selected_user();
            
if ($data->num_rows() > 0):

You are assigning the SQL query to an array ($data['userquery']) but then trying to access the properties of that query via the array itself (not an array key).
#3

[eluser]the future darlo manager[/eluser]
Thanks for the reply. Still feeling a bit non the wiser though.
#4

[eluser]Michael Wales[/eluser]
You are executing a query and storing it in one variable - then you are trying to perform actions on it's parent array.

You are basically doing this - which simply won't work:
Code:
// Set a value
$people['bob'] = 30;
// Perform an action
substr($people, 1); // <-- This fails

$people is not the same as $people['bob']. $people is an array, where as $people['bob'] is a key within that array.

Think of it like a box (named $people), and then we split that box up into sections (we'll call one half 'bob' and the other half 'mary'). Then we place Bob and Mary's ages in their sections of the box ($people['bob'] = 30; $people['mary'] = 32Wink. Now - we can't go asking the box the age of Bob, the box doesn't know. We need to ask Bob's section of the box.

In your code - you have made a box, named $people. You then created a section of that box named userquery. What you are intending to do is ask the userquery section of the box how many rows it returned (and then all of those rows). Instead you are asking the box itself.

Try this (no need to get an array involved in this, just confusing things, increasing memory, etc):

Code:
$users = $this->Admin_model->find_selected_user();
            
if ($users->num_rows() > 0) {
  foreach ($users->result() as $row) {
    echo $row->user_id;
    echo $row->username;
    echo $row->password;
  }
}
#5

[eluser]the future darlo manager[/eluser]
Thanks its working now. I think I've been looking at it for far too long because thats bloody obvious. Cheers, I'm going to take a break before looking at making the cookies :-)




Theme © iAndrew 2016 - Forum software by © MyBB