Return 来自数组/Foreach 循环中下一个和最后一个元素的键
Return a Key from the Next and Last Element in Array / Foreach Loop
我希望修改电子商务平台(OpenCart - PHP 基于 MVC)的折扣显示方式。
默认行为是折扣将显示为:
- 5 个或更多:$20.00
- 10 个或更多:18.00 美元
- 20 或更多:16.00 美元
我更愿意:
- 5 - 9:20.00 美元
- 10 - 19 岁:18.00 美元
- 20 岁以上:16.00 美元
通过模板文件(下面提供的代码)剥离 "or more" 文本非常简单。
对于除最后一个元素之外的所有元素,这将需要从下一个元素中获取数量键($discount['quantity'])并应用基本数学函数 (- 1),然后 return除了原始值之外还添加了这个新值。
对于最后一个元素,我只需要 return 最后一个数量值并添加“+”文本。
原代码(控制器):
$discounts = $this->model_catalog_product->getProductDiscounts($this->request->get['product_id']);
$this->data['discounts'] = array();
foreach ($discounts as $discount) {
$this->data['discounts'][] = array(
'quantity' => $discount['quantity'],
'price' => $this->currency->format($this->tax->calculate($discount['price'], $product_info['tax_class_id'], $this->config->get('config_tax')))
);
}
原代码(模板):
<?php if ($discounts) { ?>
<div class="discount">
<?php foreach ($discounts as $discount) { ?>
<span><?php echo sprintf($text_discount, $discount['quantity'], $discount['price']); ?></span>
<?php } ?>
</div>
<?php } ?>
修改代码以从模板中删除 "or more" 文本(注意:单独的回显用于允许 table 格式化 - 为了简单起见,不包括这些标签):
<?php if ($discounts) { ?>
<div class="discount">
<?php foreach ($discounts as $discount) { ?>
<?php echo $discount['quantity']; ?><?php echo $discount['price']; ?>
<?php } ?>
</div>
<?php } ?>
如何进一步修改此代码以return首选格式的数量?
注意:数组很小,但我仍然会优先考虑性能。
编辑:
感谢ttony提供以下解决方案。这是我在模板文件中用于自定义 table 格式(没有 sprintf/formatted 字符串函数)的代码。
<?php for ($i=0; $i < count($discounts) -1; $i++) { ?>
<tr>
<td><?php echo $discounts[$i]['quantity']; ?> - <?php echo (int)$discounts[$i+1]['quantity'] - 1; ?></td>
<td><?php echo $discounts[$i]['price']; ?></td>
</tr>
<?php } ?>
<?php if (count($discounts)) { ?>
<tr>
<td><?php echo $discounts[$i]['quantity']; ?>+</td>
<td><?php echo $discounts[$i]['price']; ?></td>
</tr>
<?php } ?>
你可以试试这个,很简单,在product.php
控制器文件
$discounts = $this->model_catalog_product->getProductDiscounts($this->request->get['product_id']);
$discounts_formated = array();
for ($i=0; $i < count($discounts) -1; $i++) {
$discounts_formated[] = sprintf("%s - %s", $discounts[$i]['quantity'], (int)$discounts[$i+1]['quantity'] - 1);
}
// Last discount: 30+
$discounts_formated[] = sprintf("%s+", $discounts[$i]['quantity']);
var_dump($discounts_formated);
输出:
array (size=3)
0 => string '10 - 19' (length=7)
1 => string '20 - 29' (length=7)
2 => string '30+' (length=3)
编辑:
编辑文件 product.tpl
,我用 OC 1.5.6.4 测试过,它可以工作
<?php if ($discounts) { ?>
<br />
<div class="discount">
<?php for ($i=0; $i < count($discounts) -1; $i++) { ?>
<?php echo sprintf("%s - %s: %s", $discounts[$i]['quantity'], (int)$discounts[$i+1]['quantity'] - 1, $discounts[$i]['price']); ?><br />
<?php } ?>
<?php if (count($discounts)) { // last discount ?>
<?php echo sprintf("%s+: %s", $discounts[$i]['quantity'], $discounts[$i]['price']); ?><br />
<?php } ?>
</div>
<?php } ?>
将打印如下内容:
10 - 19: .00
20 - 29: .00
30+: .00
我希望修改电子商务平台(OpenCart - PHP 基于 MVC)的折扣显示方式。
默认行为是折扣将显示为:
- 5 个或更多:$20.00
- 10 个或更多:18.00 美元
- 20 或更多:16.00 美元
我更愿意:
- 5 - 9:20.00 美元
- 10 - 19 岁:18.00 美元
- 20 岁以上:16.00 美元
通过模板文件(下面提供的代码)剥离 "or more" 文本非常简单。
对于除最后一个元素之外的所有元素,这将需要从下一个元素中获取数量键($discount['quantity'])并应用基本数学函数 (- 1),然后 return除了原始值之外还添加了这个新值。
对于最后一个元素,我只需要 return 最后一个数量值并添加“+”文本。
原代码(控制器):
$discounts = $this->model_catalog_product->getProductDiscounts($this->request->get['product_id']);
$this->data['discounts'] = array();
foreach ($discounts as $discount) {
$this->data['discounts'][] = array(
'quantity' => $discount['quantity'],
'price' => $this->currency->format($this->tax->calculate($discount['price'], $product_info['tax_class_id'], $this->config->get('config_tax')))
);
}
原代码(模板):
<?php if ($discounts) { ?>
<div class="discount">
<?php foreach ($discounts as $discount) { ?>
<span><?php echo sprintf($text_discount, $discount['quantity'], $discount['price']); ?></span>
<?php } ?>
</div>
<?php } ?>
修改代码以从模板中删除 "or more" 文本(注意:单独的回显用于允许 table 格式化 - 为了简单起见,不包括这些标签):
<?php if ($discounts) { ?>
<div class="discount">
<?php foreach ($discounts as $discount) { ?>
<?php echo $discount['quantity']; ?><?php echo $discount['price']; ?>
<?php } ?>
</div>
<?php } ?>
如何进一步修改此代码以return首选格式的数量?
注意:数组很小,但我仍然会优先考虑性能。
编辑:
感谢ttony提供以下解决方案。这是我在模板文件中用于自定义 table 格式(没有 sprintf/formatted 字符串函数)的代码。
<?php for ($i=0; $i < count($discounts) -1; $i++) { ?>
<tr>
<td><?php echo $discounts[$i]['quantity']; ?> - <?php echo (int)$discounts[$i+1]['quantity'] - 1; ?></td>
<td><?php echo $discounts[$i]['price']; ?></td>
</tr>
<?php } ?>
<?php if (count($discounts)) { ?>
<tr>
<td><?php echo $discounts[$i]['quantity']; ?>+</td>
<td><?php echo $discounts[$i]['price']; ?></td>
</tr>
<?php } ?>
你可以试试这个,很简单,在product.php
控制器文件
$discounts = $this->model_catalog_product->getProductDiscounts($this->request->get['product_id']);
$discounts_formated = array();
for ($i=0; $i < count($discounts) -1; $i++) {
$discounts_formated[] = sprintf("%s - %s", $discounts[$i]['quantity'], (int)$discounts[$i+1]['quantity'] - 1);
}
// Last discount: 30+
$discounts_formated[] = sprintf("%s+", $discounts[$i]['quantity']);
var_dump($discounts_formated);
输出:
array (size=3)
0 => string '10 - 19' (length=7)
1 => string '20 - 29' (length=7)
2 => string '30+' (length=3)
编辑:
编辑文件 product.tpl
,我用 OC 1.5.6.4 测试过,它可以工作
<?php if ($discounts) { ?>
<br />
<div class="discount">
<?php for ($i=0; $i < count($discounts) -1; $i++) { ?>
<?php echo sprintf("%s - %s: %s", $discounts[$i]['quantity'], (int)$discounts[$i+1]['quantity'] - 1, $discounts[$i]['price']); ?><br />
<?php } ?>
<?php if (count($discounts)) { // last discount ?>
<?php echo sprintf("%s+: %s", $discounts[$i]['quantity'], $discounts[$i]['price']); ?><br />
<?php } ?>
</div>
<?php } ?>
将打印如下内容:
10 - 19: .00
20 - 29: .00
30+: .00