Counting answers

Danuta Welz asked on March 23, 2016 12:36

Could you tell me how to count items in Custom Table? I have two tables one for questions and one for answers. I would like to count answers and put number of them near the question. Should I use API? Regards

Recent Answers


Anton Grekhovodov answered on March 23, 2016 12:56 (last edited on March 23, 2016 12:58)

Hi Danita,

Read about Custom Data API here Kentico 8 Technology – Custom tables data API

You can use:

 CustomTableItemProvider.GetItems("customtable.sample")
    .WhereEquals("QuestionID", QuestionID)
    .Count()
3 votesVote for this answer Mark as a Correct answer

Roman Hutnyk answered on March 23, 2016 21:44

Danuta, approach Anton suggested would work, however there are other ways to get it without using API:

for example you could implement custom query for your custom table, that will retrieve questions + count of answers for each question. Having this data you could easily present it on the page using repeater with custom query.

0 votesVote for this answer Mark as a Correct answer

Trevor Fayas answered on March 24, 2016 00:48 (last edited on December 10, 2019 02:30)

You can also do this via a K# Macro:

{% CMSContext.Current.GlobalObjects.CustomTables["customtable.SampleTable"].Items.Where("QuestionID = 1").Count |(identity)GlobalAdministrator%}

Of course you would put your Answers table in there, and i assume the answers have a QuestionID.

0 votesVote for this answer Mark as a Correct answer

Danuta Welz answered on March 30, 2016 08:32

Thanks for answers, I used two options without API. They work in repeater. Is it possible also to count answers in smart search results, I think they are used that way in "Kentico Questions & Answers". I have problem with it. Could it be becouse of type of transformation?

0 votesVote for this answer Mark as a Correct answer

Danuta Welz answered on March 31, 2016 18:48

Finally I used Anton suggestion in smart search transformation.

<%#
CMS.CustomTables.CustomTableItemProvider.GetItems("customtable.Answers") .WhereEquals("ItemQuestionID", GetSearchValue("ItemID")) .Count() %>

It works perfect.

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.