0

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
  • Why is $item-> in quotes at the $query assignment line? Commented Aug 14, 2010 at 0:25

1 Answer 1

1

Just replace:

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

by

$query = $item->$param;

Hope that helps=)

Sign up to request clarification or add additional context in comments.

1 Comment

haha, ya it definitely does. I feel like quite a ritard, objects make much more sense in general now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.