0

I have a products detail page and I need to be able to call the appropriate work area from the database. My columns are called: ils1275_work_area, ils975_work_area, etc.

All I need are a couple of words, so I am only returning a row. Ordinarily to call the column with my SQL, I would do (in CodeIgniter):

function get_misc($item) 
{   
    $this->db->select($item);
    $this->db->where('lang', 'en');
    return $this->db->get('all_misc')->row();
}

And then in my PHP I would echo $row->ils1275_work_area. Since on a single products detail page for multiple products, I need more flexibility than that, I need to do something like:

$row->{$laser}_work_area

But that doesn't work. (I am supplying the value for $laser in my controller). What syntax should I use?

2 Answers 2

1

I believe the syntax you want is

$row->${$laser.'_work_area'};
Sign up to request clarification or add additional context in comments.

Comments

0

Try forming the name of the variable as its own variable:

$workArea = "{$laser}_work_area";
$val = $row->$workArea;

1 Comment

@eykanal's answer is much more consise ;-)

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.