Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Data Model Questions /

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

Return ArrayList and number of rows to template


Go to End


3 Posts   1562 Views

Avatar
mlevens

Community Member, 5 Posts

8 August 2014 at 11:20am

Edited: 08/08/2014 11:28am

Hi, hoping someone maybe able to point me in the right direction.

Currently I have a query returning results to a template using the following LatestResources method

$sqlQuery = new SQLQuery();
$sqlQuery->selectField("field1, field2")
...
$result = $sqlQuery->execute();

The results are then pushed onto an ArrayList

...
foreach($result as $row) {
  $returnedRecords->push(new ArrayData($row));
}
...

and the results are displayed in the template using a control.

What I want to know is how to also return the number of rows with the data. I can get the number of rows using:

...
$result = $sqlQuery->execute();
$result->numRecords();
...

The question being how can I get this and my data out into my template without having to query the database again just to get the number of rows.

Hope someone can help

Thanks

Avatar
Kirk

Community Member, 67 Posts

8 August 2014 at 11:33am

Have you tried just appending .Count to the end of the variable in the template

<% control Property %>
... content ...
<% end_control %>
<p>$Property.Count properties(s)</p>

Avatar
mlevens

Community Member, 5 Posts

8 August 2014 at 11:39am

Thanks for that - worked like charm!! - so simple.