【问题标题】:Create function and call it创建函数并调用它
【发布时间】:2020-01-31 10:52:26
【问题描述】:

我有一段代码需要多次使用,但我不希望得到一个包含很多相同代码的很长的页面。

我有这段代码:

<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>

我不需要创建超过 40 次这段代码,而是需要创建一个我可以调用的函数,并且只更改 $av_attribute_group = $_product->getAttributes(37);因为我需要领导不同的id。

我该怎么做?

【问题讨论】:

    标签: php function call


    【解决方案1】:

    您可以将您的代码包含在 function 声明中。您需要将$_product$av_merk 变量作为参数包含在内,并且还需要将$id 值传递给$_product-&gt;getAttributes。例如:

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

    在您的代码中,您将替换

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

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

    【讨论】:

    • @n00bly 这回答了你的问题吗?如果没有,您能否提供更多信息来帮助回答?
    • 感谢您的回答,我需要在同一个文件或单独的文件中创建该函数吗?
    • @n00bly 很高兴听到。很抱歉之前没有回复,这里已经是晚上了......
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-28
    • 1970-01-01
    相关资源
    最近更新 更多