Php 创建函数并调用它
我有一段代码需要多次使用,但我不想得到一个包含很多相同代码的很长的页面 我有一段代码:Php 创建函数并调用它,php,function,call,Php,Function,Call,我有一段代码需要多次使用,但我不想得到一个包含很多相同代码的很长的页面 我有一段代码: <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-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>
阿尔及利亚信息
梅克
我需要创建一个可以调用的函数,只更改$av\u attribute\u group=$\u product->getAttributes(37),而不是创建40次以上的这段代码;因为我需要领导不同的身份证
我该怎么做呢?您可以将代码封装在
函数
声明中。您需要包括$\u product
和$av\u merk
变量作为参数,还需要传递$id
值以传递给$\u product->getAttributes
。例如:
function show_av($_product, $av_merk, $id) {
?>
... your code
<?php
}
show_av($_product, $av_merk, 37);
?>
与
@这回答了你的问题吗?如果没有,您能提供更多信息帮助回答吗?谢谢您的回答,我需要在同一个文件或单独的文件中创建该函数吗?@n00bly很高兴听到。很抱歉之前没有回复,这里已经是晚上了。。。
$av_attribute_group = $_product->getAttributes(37);
$av_attribute_group = $_product->getAttributes($id);