Forum

Thread tagged as: Question, Problem

Fetch Perch Checkbox by `name` attribute

Hi guys,

I'm currently working on this filtering system for a site, and I'm trying to use Perch checkboxes with it. I'm having some trouble, as it looks as though we have to use different IDs for each checkbox (which does totally make sense, but is causing trouble).

Is there a way to fetch multiple checkboxes with one attribute?

I'm trying to use name at the moment but I'm not really sure how I'd call that in on the other end.. Please see below for my code:


<!-- Range --> <perch:content name=="range[]" id="range_essentials" type="checkbox" label="Essentials" value="essentials" suppress="true" divider-before="Range" /> <perch:content name=="range[]" id="range_signature" type="checkbox" label="Signature" value="signature" suppress="true" /> <perch:content name=="range[]" id="range_platinum" type="checkbox" label="Platinum" value="platinum" suppress="true" /> <!-- Style --> <perch:content name="style[]" id="style_classic" type="checkbox" label="Classic" value="classic" suppress="true" divider-before="Style" /> <perch:content name="style[]" id="style_contemporary" type="checkbox" label="Contemporary" value="contemporary" /> <perch:content name="style[]" id="style_eclectic" type="checkbox" label="Eclectic" value="eclectic" /> <perch:content name="style[]" id="style_painted" type="checkbox" label="Painted" value="painted" /> <!-- Color --> <perch:content name="color[]" id="color_alabaster" type="checkbox" label="Alabaster" value="alabaster" suppress="true" divider-before="Colour" /> <perch:content name="color[]" id="color_almond" type="checkbox" label="Almond" value="almond" /> <perch:content name="color[]" id="color_anthracite" type="checkbox" label="Anthracite" value="anthracite" /> <perch:content name="color[]" id="color_aubergine" type="checkbox" label="Aubergine" value="Aubergine" /> <perch:content name="color[]" id="color_beige" type="checkbox" label="Beige" value="beige" /> <perch:content name="color[]" id="color_black" type="checkbox" label="Black" value="black" /> <perch:content name="color[]" id="color_brown_grey" type="checkbox" label="Brown Grey" value="brown_grey" /> <perch:content name="color[]" id="color_cashmere" type="checkbox" label="Cashmere" value="cashmere" /> <perch:content name="color[]" id="color_chalk_blue" type="checkbox" label="Chalk Blue" value="chalk_blue" /> <perch:content name="color[]" id="color_cream" type="checkbox" label="Cream" value="cream" /> <perch:content name="color[]" id="color_fjord" type="checkbox" label="Fjord" value="fjord" /> <perch:content name="color[]" id="color_dove_grey" type="checkbox" label="Dove Grey" value="dove_grey" /> <perch:content name="color[]" id="color_light_grey" type="checkbox" label="Light Grey" value="dove_grey" /> <perch:content name="color[]" id="color_dust_grey" type="checkbox" label="Dust Grey" value="dust_grey" /> <perch:content name="color[]" id="color_stone_grey" type="checkbox" label="Stone Grey" value="stone_grey" /> <perch:content name="color[]" id="color_graphite" type="checkbox" label="Graphite" value="graphite" /> <perch:content name="color[]" id="color_knotty_oak" type="checkbox" label="Knotty Oak" value="knotty_oak" /> <perch:content name="color[]" id="color_lava" type="checkbox" label="Lava" value="lava" /> <perch:content name="color[]" id="color_light_olive" type="checkbox" label="Light Olive" value="light_olive" /> <perch:content name="color[]" id="color_natural_oak" type="checkbox" label="Natural Oak" value="natural_oak" /> <perch:content name="color[]" id="color_mussel" type="checkbox" label="Mussel" value="mussel" /> <perch:content name="color[]" id="color_pale_smoke" type="checkbox" label="Pale Smoke" value="pale_smoke" /> <perch:content name="color[]" id="color_porcelain" type="checkbox" label="Porcelain" value="porcelain" /> <perch:content name="color[]" id="color_stone" type="checkbox" label="Stone" value="stone" /> <perch:content name="color[]" id="color_white" type="checkbox" label="White" value="white" /> <!-- finish --> <perch:content name="finish[]" id="finish_matte" type="checkbox" label="Matte" value="matte" suppress="true" divider-before="Finish" /> <perch:content name="finish[]" id="finish_super_matte" type="checkbox" label="Super Matte" value="super_matte" suppress="true" /> <perch:content name="finish[]" id="finish_gloss" type="checkbox" label="Gloss" value="gloss" /> <perch:content name="finish[]" id="finish_high_gloss" type="checkbox" label="High Gloss" value="high_gloss" /> <perch:content name="finish[]" id="finish_vinyl_wrap" type="checkbox" label="Vinyl Wrap" value="vinyl_wrap" /> <perch:content name="finish[]" id="finish_wood_grain" type="checkbox" label="Wood Grain" value="wood_grain" /> <perch:content name="finish[]" id="finish_painted" type="checkbox" label="Painted" value="painted" /> <perch:content name="finish[]" id="finish_natural_oak" type="checkbox" label="Natural Oak" value="natural_oak" />

Thanks guys!

Harry Ray

Harry Ray 0 points

  • 3 years ago

Hi all,

Just to let you know I've resolved this by doing the below:

perch_content_custom("Kitchen Ranges",[
            "template" => "kitchen-range-basic.html",
            'each' => function ($kitchen) {
                // Process Range
                $range = array();
                foreach ($kitchen as $key => $value) {
                    if ((strpos($key, 'range') === 0)&&($value !== NULL)) {
                        $range[$key] = $value;
                    }
                }
                $range = implode(", ",$range);
                $kitchen[ 'range' ] = $range;

                // Process Style
                $style = array();
                foreach ($kitchen as $key => $value) {
                    if ((strpos($key, 'style') === 0)&&($value !== NULL)) {
                        $style[$key] = $value;
                    }
                }
                $style = implode(", ",$style);
                $kitchen[ 'style' ] = $style;

                // Process Color
                $color = array();
                foreach ($kitchen as $key => $value) {
                    if ((strpos($key, 'color') === 0)&&($value !== NULL)) {
                        $color[$key] = $value;
                    }
                }
                $color = implode(", ",$color);
                $kitchen[ 'color' ] = $color;

                // Process Finish
                $finish = array();
                foreach ($kitchen as $key => $value) {
                    if ((strpos($key, 'finish') === 0)&&($value !== NULL)) {
                        $finish[$key] = $value;
                    }
                }
                $finish = implode(", ",$finish);
                $kitchen[ 'finish' ] = $finish;
                return $kitchen;
            }
        ]);