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.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

database row count


Go to End


3 Posts   1855 Views

Avatar
ryeze

Community Member, 6 Posts

2 August 2011 at 2:59pm

Edited: 02/08/2011 3:00pm

hi. I have a database table like that in the attached file. I want count number of database rows. if in sql may look something like this, SELECT count (KotaPenerima) FROM `student` WHERE KotaPenerima = 'Jawa Barat'. How do I put this function, and if I want to display query results on the front page.

thanks.

Attached Files
Avatar
Devlin

Community Member, 344 Posts

2 August 2011 at 11:39pm

Page.php

function Students() {
  $students = DataObject::get('Student', "KotaPenerima = 'Jawa Barat'");
  return !empty($students) ? $students : false;
}

Page.ss

<% if Students %>
  student count: $Students.Count
  <% control Students %>
    student lastname: $Lastname
  <% end_control %>
<% end_if %>

http://doc.silverstripe.org/sapphire/en/topics/datamodel
http://doc.silverstripe.org/sapphire/en/reference/advanced-templates

Avatar
ryeze

Community Member, 6 Posts

3 August 2011 at 4:34pm

Hi devlin .. thanks for your quick response.
Wow ..
it's work, Thank you very much.

I'm still a newbie in using SilverStripe.
but I found another problem, I would like the 'KotaPenerima' a variable that contains all of the 'KotaPenerima, not just 'JawaBarat'. may be an array, I do not know what to put where the array functions?

so that I can display the count of each 'KotaPenerima'.