0

I have a view file where I need to echo db column values using foreach method.

I have column e.g. column_a, column_b, column_c....

I am able to get values if I use <?= $variable_1->column_a; ?>

But I need to echo them like <?= $variable_1->column_.$variable_2; ?>

If I use it the second way, gives error

Message: Undefined property: stdClass::$column_

CodeIgniter_Learner
  • 527
  • 1
  • 6
  • 19
  • Possible duplicate of http://stackoverflow.com/questions/13707836/php-foreach-arrays-and-objects – rsz Feb 15 '16 at 22:18

2 Answers2

3

Try

<?php $colname = "column_{$variable_2}"; ?>
<?= $variable_1->{$colname}; ?>
Alan Horrocks
  • 324
  • 1
  • 6
0

Thanks for @Alan Horrocks as he showed me the right direction:

I worked by doing

<?php $column = $session_data['default']; ?> <?php $colname = 'column_'.$column; ?> <?= $listing->$colname; ?>

CodeIgniter_Learner
  • 527
  • 1
  • 6
  • 19