We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

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.



98.9k

Just access the sixth position:

echo $data[6]->id;


33.8k

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



5.7k

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; ?



98.9k

It would be:

{{ data[6].id }}


5.7k

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



98.9k

Are you using PHP 5.3?



98.9k

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



5.7k

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



98.9k
Accepted
answer
edited Aug '14

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



5.7k
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.