tags:

views:

20

answers:

1

Hi,

So Im trying to make an object dynamically, please see below code from my item model

function get_total($param)
{

    $i = new Item();
    $items = $i->get();
    $total_value = 0;
    $query = "$item->".$param;
    foreach($items as $item)
    {
        if($item->qty > 0)
        {
            $total_value += $query * $item->qty;
        }
    }
    return $total_value;
}

So for instance, in my mysql table, I have cost and price mapped as properties of the $item object -- So for example Im trying to make that variable $query, output the following object -- $item->cost

So that I can call the function from my view like:

  echo $i->get_total('price');

or

  echo $i->get_total('cost');

To make my models selection more dynamic. But Im not sure the proper way to do it, because it seems that trying to create a dynamic object by concating the value for it based on the param, is a no no. So can someone please show me how to properly do it?

Thanks in advance for help!

So that on my

+1  A: 

Just replace:

$query = "$item->".$param;

by

$query = $item->$param;

Hope that helps=)

Guillermo
haha, ya it definitely does. I feel like quite a ritard, objects make much more sense in general now.
thrice801
Thanks.. I would appreciated if you "accept" my answer :=)
Guillermo