Welcome Guest, Not a member yet? Register   Sign In
Solved : Newbie question - Working with models
#11

[eluser]ywftdg[/eluser]
Anyone? Is it possible/proper to do multiple queries in one model function? If so, how do you pass the results to each one?
#12

[eluser]m4rw3r[/eluser]
I cannot see why it would be forbidden.

Here is an example of using data from the first query in the second:
Code:
// first query, fetch a page:
$q = $this->db->get_where('pages', array('title' => 'About'), 1);
$page = $q->row();

// get all comments
$q = $this->db->get_where('comments', array('page_id' => $page->id));
$page->comments = $q->result();                     //   ^^^^^^^^^ That fetches the data from the page we previously loaded

return $page;
#13

[eluser]ywftdg[/eluser]
There seems no obvious way to rewrite this query, I am just going to ditch this or hire someone. No matter how many ways I try to say 'while result' or 'foreach' it kills my controller or kills my models, seems CI doesn't want to do things this way. I am close to tossing y computer out the window, not worth the stress. I do not understand how something so basic as a loop can be so confusing.

Model
Code:
function get_bundles()    {
        $this->db->select('pSku, pName, pCost, pPrice, pSkulist');
        $this->db->where("pSkulist <> ''");
        $this->db->from('products');
        $query = $this->db->get();
        return $query->result();
        
    }

Controller
Code:
$query = $this->reports->get_bundles();
        
        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $nothing = '';
            }
        }

Also, when I try to run the if statement in the model instead, it wont work, nor do I understand how to get it to"add" this results to my return$query last line.

Code:
function get_bundles()    {
        $this->db->select('pSku, pName, pCost, pPrice, pSkulist');
        $this->db->where("pSkulist <> ''");
        $this->db->from('products');
        $query = $this->db->get();
        
        foreach ($query->result() as $row) { // ok if above found item, run this also
            $thelist = $query->row('pSkulist');
            $this->db->select('sum(pPrice) as pricesum');
            $this->db->where_in('pSku', $thelist);
            $this->db->from('products');
            $queryb = $this->db->get(); // now thatI got the result, how can it be joined to the first queries result?
        }
        
        return $query->result();
        
    }

No matter what sort of scenario I try here, this wont work. Its just amazing how ridiculous this is, because it was so easy to write in my old code. Im looking at this wrong somehow, at this point i'm at a dead stop, I have tried endless methods, none of these work ad im just talking to myself on here now, hahaha.
#14

[eluser]ywftdg[/eluser]
Solved: http://ellislab.com/forums/viewthread/88156/




Theme © iAndrew 2016 - Forum software by © MyBB