Warm tip: This article is reproduced from stackoverflow.com, please click
function php call

Create function and call it

发布于 2020-03-29 21:02:51

I have a piece of code that I need to use multiple times, but I don't want to get a very long page with a lot of the same code.

I have this piece of code:

<div class="av-product-specs-list-container">
    <input type="checkbox" id="chck1" checked>
    <label class="av-product-specs-title" for="chck1">Algemene informatie</label>
    <div class="av-product-specs-table-list first-spec-list">

        <?php if( $av_merk != "" ) : ?>
            <div class="av-product-specs-table-row">
                <div class="av-product-specs-table-row-left">Merk</div>
                <div class="av-product-specs-table-row-right">
                    <?php echo $av_merk; ?>
                </div>
            </div>
        <?php endif; ?>

        <?php 
            $av_attribute_group = $_product->getAttributes(37);

            foreach ($av_attribute_group as $av_attribute_groups):

                $av_attr_name = $av_attribute_groups->getName();
                $av_attr_label = $_product->getResource()->getAttribute($av_attr_name)->getFrontend()->getLabel($_product);
                $av_attr_value = $_product->getResource()->getAttribute($av_attr_name)->getFrontend()->getValue($_product);

                if( $av_attr_value != "" ) :

                ?>

                    <div class="av-product-specs-table-row">
                        <div class="av-product-specs-table-row-left"><?php echo $av_attr_label; ?></div>
                        <div class="av-product-specs-table-row-right">
                            <?php 
                                if( $av_attr_value == "Yes" || $av_attr_value == "Ja" ) : ?>
                                    <div class="av-spec-yes"></div>
                                <?php elseif( $av_attr_value == "No" || $av_attr_value == "Nee" ) : ?>
                                    <div class="av-spec-no"></div>
                                <?php else : 
                                    echo $av_attr_value;
                                endif; 
                            ?>
                        </div>
                    </div>

                <?php endif; ?>             
        <?php endforeach ?>

    </div>
</div>

Instead of creating this piece of code over 40 times, I need to create a function that I can call and only change the $av_attribute_group = $_product->getAttributes(37); because I need to lead different id's.

How can I do that?

Questioner
n00bly
Viewed
69
Nick 2020-01-31 18:59

You can just enclose your code in a function declaration. You need to include the $_product and $av_merk variables as parameters, and also pass a $id value to pass to $_product->getAttributes. For example:

function show_av($_product, $av_merk, $id) {
?>
... your code
<?php
}

show_av($_product, $av_merk, 37);
?>

Inside your code, you would then replace

$av_attribute_group = $_product->getAttributes(37);

with

$av_attribute_group = $_product->getAttributes($id);