Welcome Guest, Not a member yet? Register   Sign In
having trouble looping through query results
#1

[eluser]jvittetoe[/eluser]
Hello, in my application, once a user logs in they are directed to their dashboard controller...
Code:
<?php

class Dashboard extends Controller {

    function Dashboard(){
        parent::Controller();
        $this->load->helper( array('url', 'form', 'date') );
        $this->load->library('validation');
        $this->load->model('userItems');
                
    }
    
    function index(){        
        $this->userItems->qryOverview();

        $data['title'] = "My Finance ~ v1.00 ~ Dashboard";
        $data['status'] = "PLEASE LOGIN";
        $data['query'] = $query;
        
        $template['mainContent'] = $this->load->view('dspDashboard', $data, true);
        $this->load->view('layDash', $template);
        
    }
}

from here i am loading the model, and running the function qryOverview which queries the db for all user items associated with the user. here is the qry.
Code:
function qryOverview(){
        
        $this->CI =& get_instance();
        //query db for current password
        /*
        $query = $this->db->select('*');
        $query = $this->db->from('user_items');
        $query = $this->db->where('user_id', $this->CI->session->userdata('id'));
        $query = $this->db->get();
        */
        $query = $this->db->query('SELECT * FROM user_items WHERE user_id = '.$this->CI->session->userdata("id").'');
        echo $query->num_rows();
        
        return $query;
        
    }
the query is returning the correct rows but i cant seem to figure out how to return the results back to the controller dashboard/index. any ideas.. thanks.
#2

[eluser]coolfactor[/eluser]
You weren't storing the returned query into the $query variable. You need something like this:

Quote:$query = $this->userItems->qryOverview();
#3

[eluser]jvittetoe[/eluser]
ah, thanks. now ive got my code looking like this...
Code:
function index(){        
        $data['query'] = $this->userItems->qryOverview();

        $data['title'] = "My Finance ~ v1.00 ~ Dashboard";
        $data['status'] = "PLEASE LOGIN";
        
        $template['mainContent'] = $this->load->view('dspDashboard', $data, true);
        $this->load->view('layDash', $template);
        
    }

then in dspDashboard view file,

Code:
<?php

        foreach ($data['query']->result_array() as $row){
            echo $row['item_title'];
            echo $row['item_type'];
            echo $row['item_value'];
            echo '<br />';
            
        }

?&gt;

this is resulting in a white blank page...
#4

[eluser]Nillian[/eluser]
Remove the $data['query'] in your view file and use simply $query. CI converts the array into individual variables for you to use in your view file Smile
#5

[eluser]jvittetoe[/eluser]
so the variable $query will be automaically passsed to the view file? then would i run that loop in my view file? im getting a white page when i do that.
#6

[eluser]coolfactor[/eluser]
He means to do this:
Code:
foreach ($query->result_array() as $row){
#7

[eluser]jvittetoe[/eluser]
yea, thats returning a blank white page :/
#8

[eluser]Nillian[/eluser]
Try this thread: http://ellislab.com/forums/viewthread/59674/

Or this one: http://ellislab.com/forums/viewthread/56059/

They might not help, but I hope they do Smile




Theme © iAndrew 2016 - Forum software by © MyBB