我正在对订单总数进行OpenCart修改。这是代码的片段:
<?php
foreach ($data['totals'] as $total) {
if (
$this->db->escape($total['code'])=="sub_total" ||
$this->db->escape($total['title'])="Sub-Total"
) {
$this->db->query("INSERT INTO " . DB_PREFIX . "order_total SET order_id = '" . (int)$order_id . "', code = '" . $this->db->escape($total['code']) . "', title = '" . $this->db->escape($total['title']) . "', text = '" . $this->db->escape($total['text']) . "', `value` = '" . (float)$data['total'] . "', sort_order = '" . (int)$total['sort_order'] . "'");
} else {
$this->db->query("INSERT INTO " . DB_PREFIX . "order_total SET order_id = '" . (int)$order_id . "', code = '" . $this->db->escape($total['code']) . "', title = '" . $this->db->escape($total['title']) . "', text = '" . $this->db->escape($total['text']) . "', `value` = '" . $_SESSION['GCFinalOrderTotalIncludingDelivery'] . "', sort_order = '" . (int)$total['sort_order'] . "'");
}
}
?>
这是伪代码。 IF / Else可以嵌套在foreach语句中吗?
<?php
foreach ($d['totals'] as $total) {
if ($query=="x" || $query=="y") {
a();
} else {
b();
}
}
?>
答案 0 :(得分:7)
当然可以。
很难用30个字符来表达。
答案 1 :(得分:1)
当然可以。不清楚你的问题是什么,但假设你在这一行中有一个错字:
$this->db->escape($total['title']) = "Sub-Total"
应该是这样的:
$this->db->escape($total['title']) == "Sub-Total"
答案 2 :(得分:1)
是的,你可以这样做。这是非常常见的。确保在测试时使用==
;你有=
作为考试。
答案 3 :(得分:1)
我想你现在知道:“是的,他们可以!”。但是:如果你的循环不是那么大,你可能想重新考虑两个分支之间的实际差异。以你的伪代码为例:如果根据一个不是从循环本身提供的变量派生的值决定分支,你最好移动分支:
foreach($someArr as $k=>$v)
{
if ($someOtherVar === true)
{
echo 'The value is '.$v;
}
else
{
echo 'The key is '.$k;
}
}
可以(并且应该)写成:
$str = 'The '.($someOtherVar === true ? 'value' : 'key').' is ';
foreach($someArr as $k=>$v)
{
echo $str.$v;
}
答案 4 :(得分:0)
是绝对的。请注意使用编程语言会有什么意义。
答案 5 :(得分:0)
在OpenCart中,您尝试编辑的文件我假设是订单模型吗?
<强> /catalog/model/checkout/order.php 强>
FAngel提到错过了两个==
,但是问题出在你的浮动上。数据属性在此函数中被删除。因为数据数组不再位于该代码段内。它依赖于TOTAL:
所以你的行:
$this->db->query("INSERT INTO " . DB_PREFIX . "order_total SET order_id = '" . (int)$order_id . "', code = '" . $this->db->escape($total['code']) . "', title = '" . $this->db->escape($total['title']) . "', text = '" . $this->db->escape($total['text']) . "', `value` = '" . (float)$data['total'] . "', sort_order = '" . (int)$total['sort_order'] . "'");
应该是:(注意新的(float)$total['value']
)
$this->db->query("INSERT INTO " . DB_PREFIX . "order_total SET order_id = '" . (int)$order_id . "', code = '" . $this->db->escape($total['code']) . "', title = '" . $this->db->escape($total['title']) . "', text = '" . $this->db->escape($total['text']) . "', `value` = '" . (float)$total['value'] . "', sort_order = '" . (int)$total['sort_order'] . "'");
这意味着:
#######
// WAS (float)$data['value']
// NOW (float)$total['value']
#######
foreach ($data['totals'] as $total) {
if (
$this->db->escape($total['code'])=="sub_total" ||
$this->db->escape($total['title'])=="Sub-Total"
) {
$this->db->query("INSERT INTO " . DB_PREFIX . "order_total SET order_id = '" . (int)$order_id . "', code = '" . $this->db->escape($total['code']) . "', title = '" . $this->db->escape($total['title']) . "', text = '" . $this->db->escape($total['text']) . "', `value` = '" . (float)$total['value'] . "', sort_order = '" . (int)$total['sort_order'] . "'");
} else {
$this->db->query("INSERT INTO " . DB_PREFIX . "order_total SET order_id = '" . (int)$order_id . "', code = '" . $this->db->escape($total['code']) . "', title = '" . $this->db->escape($total['title']) . "', text = '" . $this->db->escape($total['text']) . "', `value` = '" . $_SESSION['GCFinalOrderTotalIncludingDelivery'] . "', sort_order = '" . (int)$total['sort_order'] . "'");
}
}