-1

我正在对订单总额进行 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();
    }
}
?>
4

6 回答 6

7

他们当然可以。

而且很难用30个字来表达。

于 2012-09-06T10:46:50.043 回答
1

当然可以。不清楚你的问题是什么,但假设你在这一行有错字:

$this->db->escape($total['title']) = "Sub-Total" 

应该是这样的:

$this->db->escape($total['title']) == "Sub-Total" 
于 2012-09-06T10:46:56.877 回答
1

是的,你可以这样做。这也很常见。确保==在测试时使用;你有=你的测试。

于 2012-09-06T10:48:25.773 回答
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;
}
于 2012-09-06T10:55:06.937 回答
0

是的,绝对是。否则拥有编程语言的意义何在。

于 2012-09-06T10:47:50.277 回答
0

在 OpenCart 中,您尝试编辑的文件我假设是订单模型?

/catalog/model/checkout/order.php

FAngel==提到了缺少两个问题,但问题在于您的浮点数。data 属性在此函数中被删除。因为 Data 数组不再在该片段中。它依赖于 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'] . "'");

应该是:(注意 NEW (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'] . "'");
    }
}
于 2012-09-06T10:52:58.043 回答