Jump to:

23486 Posts in 18995 Topics by 2878 members

General Questions

SilverStripe Forums » General Questions » Programatically count checkboxes checked in back-end

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

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

Page: 1
Go to End
Author Topic: 630 Views
  • bostonmark
    Avatar
    Community Member
    10 Posts

    Programatically count checkboxes checked in back-end Link to this post

    I have about 45 checkbox fields in a new tab in the back-end. I want to show something on the front-end template that says x of 45 checked. How can I programatically determine this in the PHP class to I can pass the checked count and total count to the template?

  • danzzz
    Avatar
    Community Member
    175 Posts

    Re: Programatically count checkboxes checked in back-end Link to this post

    hi bostonmark,

    how do you manage this checkboxes?
    did you extended "Page" or a special page-type with 45 boolean fields or do you use a own dataobject for the possible checkboxes?

  • bostonmark
    Avatar
    Community Member
    10 Posts

    Re: Programatically count checkboxes checked in back-end Link to this post

    I extended Page and added 45 checkbox fields to it. I may add more later too.

  • danzzz
    Avatar
    Community Member
    175 Posts

    Re: Programatically count checkboxes checked in back-end Link to this post

    hi,

    you better solve this with a dataobject and manage it with modeladmin.
    so you can add new checkboxes easy in the backend. and counting is also much easier.

    with you current solution
    your checkbox fields are columns in the Page table now. but there are also other columns of course.
    you have to find a way to count the relevant fields with value = 1.

    a solution may is to use the information_schema DB of mysql.
    you need a nameing convention for the checkbox fields, example:

    checkbox_1
    checkbox_2
    checkbox_3

    query to get all relevant fields:

    select column_name from information_schema.columns where table_name = 'Page' and column_name like 'checkbox_%'

    so you get a result with all checkbox_* fields. save the count of rows in a variable ($count_rows_checkboxes) and loop
    through the results and create this string:

    checkbox_1 = 1 OR checkbox_1 = 1 OR checkbox_3 = 1 // $filter

    then create this funciton in Page.php, in the Controller:

    public function getCheckboxCount() {

    $Count = DataObject::get('Page',$filer); // $filter = string created above
    return $Count->Count()." of ".$count_rows_checkboxes." checked.";

    }

    Then in your Template just call

    $getCheckboxCount

    I dont tested this, but should work ...

    630 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.