Welcome Guest, Not a member yet? Register   Sign In
Using database results in the controller
#1

[eluser]bcarter[/eluser]
Hi there

I hope someone can help me as I am really stuck with this. I am using Ion_auth to build my login to an application but the principle is universal, I just don’t know it!!!

Problem
I want to query a ‘users’ table and use the results to insert into a different table.

Here’s the Model…

Code:
class Login_model extends CI_Model {
    
     function __construct() {
        // Call the Model constructor
        parent::__construct();
    }
    
    function get_user()
    {
        $username = $this->session->userdata('username');
        $password = $this->session->userdata('password');
        
        $this->db->where('email', $username);
        $this->db->where('password', $password);
        $query = $this->db->get('users', 1);
        
        if($query->num_rows == 1)
        {
            $results = $query->row();
            return     $results;
        }        
    }

And the controller…

Code:
class Campaign extends CI_Controller {

    function __construct()
    {
        parent::__construct();
        $this->is_logged_in();
    }
    
    function add()
    {
        //Load the model
        $this->load->model('Search_model');

                $user = $this->Search_model->get_user();
        $sector = $user->sector;

        $insert = array(
                'c_sector'=>$sector, //this is the problem field!!!!
                'c_title'=>$this->input->post('title'),
                'c_start'=>dbdate_converter($this->input->post('campaign_start')),
                'c_end'=>dbdate_converter($this->input->post('campaign_end')),
            );
            
            $this->db->insert('campaigns', $insert);
            $this->session->set_userdata('campaign', $this->db->insert_id());
            redirect('campaign/goals');        
            
        }
    }

Can anyone help or explain (as clearly as possible please, like you’re talking to an idiot ;-) !) how to use data you retrieve from a query in the controller?

Thanks!
#2

[eluser]andyy[/eluser]
Why are you loading and calling methods from Search_model when you cited a model named Login_model?

Have you tried using $query->row_array() instead of $query->row()? I haven't tested it but I don't know if you can return objects from the model to the controller - I've only ever returned arrays.

Assuming you used $query->row_array():

Code:
function add()
    {
        //Load the model
        $this->load->model('Search_model');

        $result = $this->Search_model->get_user(); // this returns an array

        $insert = array(
                'c_sector'=>$result['sector'], //this is the problem field!!!!
                'c_title'=>$this->input->post('title'),
                'c_start'=>dbdate_converter($this->input->post('campaign_start')),
                'c_end'=>dbdate_converter($this->input->post('campaign_end')),
            );
            
            $this->db->insert('campaigns', $insert);
            $this->session->set_userdata('campaign', $this->db->insert_id());
            redirect('campaign/goals');        
            
        }
    }
#3

[eluser]haileCI[/eluser]
$this->load->model('Search_model');? it is not the Model you difind
$query = $this->db->get('users', 1);? i don't know what you want to do,why do you add second parameter?
#4

[eluser]andyy[/eluser]
[quote author="haileCI" date="1301689208"]
Code:
$query = $this->db->get('users', 1);
? i don't know what you want to do,why do you add second parameter?
[/quote]

The second parameter limits the number of results.

http://ellislab.com/codeigniter/user-gui...tml#select
#5

[eluser]bcarter[/eluser]
Less haste more speed!

The reason why a different model is defined is because it's an example error! I just trimmed the code out to use as an example of the problem I have. It's just a principal really, how to use data from a query in the controller!

Thanks
#6

[eluser]andyy[/eluser]
[quote author="bcarter" date="1301690758"]Less haste more speed!

The reason why a different model is defined is because it's an example error! I just trimmed the code out to use as an example of the problem I have. It's just a principal really, how to use data from a query in the controller!

Thanks[/quote]
Alright good to know Smile.

Did you solve your problem?




Theme © iAndrew 2016 - Forum software by © MyBB