views:

24

answers:

2

I am trying to do the following:

<?php foreach($sqlResult as $row): ?>
    <tr>
        <?php foreach($formdata['columns'] as $column): ?>
            <td><?php echo $row->$column['name']; ?></td>
        <?php endforeach; ?>
    </tr>
<?php endforeach; ?>

This does not work.

$row is returned by my mysql query, it has the following : $row->id, $row->author, $row->date, $row->title and these work as they echo fine.

$columns is the following array:

'columns' => array
(
    1 => array
(
    'name' => 'id'
),
    2 => array
(
    'name' => 'author'
),
    3 => array
(
    'name' => 'date'
),
    4 => array
(
    'name' => 'title'
)

it also works fine as $column['name'] echoes id, author, date, title

my question is how would it be possible for me to access the $row->method (is it a method?) by passing it a name from the array??

+1  A: 

I'm not 100% sure I understand what you want to do but I think you want this:

 <td><?php echo $row->{$column['name']}; ?></td>

notice the curly brackets. They make sure $column["name"] is interpreted fully as the property you want to access. Right now, you are accessing the property named $column and its array key name.

Pekka
+1  A: 

Try doing:

$column_name = $column['name'];
echo $row->$column_name;

or

echo $row->{$column['name']}

whichever feels more readable.

Mat