Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

Get specific result from object by index

Hey,

I get all rows from the database with $data = Data::find(); and I bind them to a var for the views ($this->view->setVar("data", $data);).

Now in the views I want the data from row 6. How can I get the data by index? As it is an object and not an array..

I don't want to run a for each it.



83.2k

Just access the sixth position:

echo $data[6]->id;


18.3k

Or Data::find(6) if you only want one row, isn't it?



2.9k

I think I made the question too general.

In reality I have a few where and a order within it. So Data::find(6) wouldn't work.

@Phalcon what would be the volt syntax for echo $data[6]->id; ?



83.2k

It would be:

{{ data[6].id }}


2.9k

@Phalcon I tried that before and it gives me: Parse error: syntax error, unexpected '->' (TOBJECTOPERATOR), expecting ',' or ';' in...



83.2k

Are you using PHP 5.3?



83.2k

Could you please open the generated file and post the generated code?



2.9k

@Phalcon this is the generated output: <h3><?php echo ($data[5])->id; ?></h3>



83.2k
Accepted
answer
edited Aug '14

I think PHP doesn't like that syntax: you can use {% set value = data[6] %} {{ value.id }}



2.9k
edited Aug '14

mhh, ok thanks:

What I did to fix it temp was to convert the object with a foreach loop to an array in the controller and than I can access everything fine with the volt syntax.

I think I will use the <?php ?> way than. Seems to be the cleanest one for me.