PHP variable variables (CodeIgniter)
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 d开发者_如何学Co (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?
I believe the syntax you want is
$row->${$laser.'_work_area'};
Try forming the name of the variable as its own variable:
$workArea = "{$laser}_work_area";
$val = $row->$workArea;
精彩评论