Welcome Guest, Not a member yet? Register   Sign In
Model sharing variables in functions? [solved]
#1

[eluser]ywftdg[/eluser]
I seem to be having a hard time on this dang project, one after another. I have two functions in my model, but for no matter what, I cannot figure how to pass the query result to the other one.

Code:
function get_royalty_rate($id)    {
        $this->db->select('dPercentages');
        $this->db->where('designer.dDesignerId', $id);
        $this->db->limit(1);

        $this->db->from('designer');
        $query = $this->db->get();
            $row = $query->row();
            $rate = $row->dPercentages; // trying to set a variable for the result
        return $query->result();
       }

function get_products($id)    {
        
        $this->db->select('pSku,oSku,oName,oPrice,rTimeStamp,pDesignerId');
        $this->db->where('products.pDesignerId', $id);
        $this->db->group_by('oSku,oPrice');
        
        $this->db->select('pSku,pName,oPrice,oSku,rTimeStamp, sum(oQuantity) as quan, ((oPrice*' . $rate . '/100)*sum(oQuantity)) as salesProfit');
// the $rate in this query is my problem
        

        $this->db->from('products');
        
        $this->db->join('orderdetail', 'orderdetail.oSku = products.pSku');
        $this->db->join('receipt', 'receipt.rOrderNum = orderdetail.oOrderNum');
        
        
        $query = $this->db->get();
        return $query->result();
        
       }

Any ideas?
#2

[eluser]ywftdg[/eluser]
Nevermind, i'm retarded, you declare it at the very beging of the model, for example:

Code:
class Reports_model extends Model {

        var $rate = '';
        
       /////////////////////////////////////////////
       function Reports_model()    {
         parent::Model();
        
    }
  
    
    /////////////////////////////////////////////
    function get_royalty_rate($id)    {
#3

[eluser]xwero[/eluser]
The get_products query seems very odd are you sure you didn't forget a part of the code?

I assume the products.pDesignerId isn't the same as the designer.dDesignerId.

But to get the rate in the get_royalty_rate you have to return $row->dPercentages instead of $query->result() and then you can do this in the get_products method
Code:
$this->db->select('pSku,pName,oPrice,oSku,rTimeStamp, sum(oQuantity) as quan, ((oPrice*' . $this->get_royalty_rate($id) . '/100)*sum(oQuantity)) as salesProfit');
#4

[eluser]ywftdg[/eluser]
Not sure, all I know is change that query to like yours,and it broke again, haha. My current code is working, is:

Code:
class Reports_model extends Model {

        var $rate = '';
        
       /////////////////////////////////////////////
       function Reports_model()    {
         parent::Model();
        
    }
  
    
    /////////////////////////////////////////////
    function get_royalty_rate($id)    {
        $this->db->select('dPercentages');
        $this->db->where('designer.dDesignerId', $id);
        $this->db->limit(1);

        $this->db->from('designer');
        $query = $this->db->get();
        
        //    Set Rate
        $row = $query->row();
        $this->rate = $row->dPercentages;
        
        return $query->result();
       }
    
        
    function get_products($id)    {
        
        $this->db->select('pSku,oSku,oName,oPrice,rTimeStamp,pDesignerId');
        $this->db->where('products.pDesignerId', $id);
        $this->db->group_by('oSku,oPrice');
        
        $this->db->select('pSku,pName,oPrice,oSku,rTimeStamp, sum(oQuantity) as quan, (oPrice*sum(oQuantity)) as salesTotal, ((oPrice*' . $this->rate . '/100)*sum(oQuantity)) as salesProfit');
        
        $this->db->order_by('quan', 'desc');
        $this->db->from('products');
        
        $this->db->join('orderdetail', 'orderdetail.oSku = products.pSku');
        $this->db->join('receipt', 'receipt.rOrderNum = orderdetail.oOrderNum');
        
        $query = $this->db->get();
        return $query->result();
        
       }
    
    
    
    
       /////////////////////////////////////////////
    }

Basically I gotta grab stuff from 3 tables, staring with finding things related to the user id (comes from session sent from controller, $id). That then says, ok get what is the rate they make of sales, then get whatever products they have, where are those products on the orders, count how many times that happens and amounts to, and boom show it in the view. Total circus act really, old system, but it works I assume..




Theme © iAndrew 2016 - Forum software by © MyBB