循环数组不返回正确的值

时间:2015-02-15 18:58:37

标签: php arrays for-loop oscommerce

我已经盯着这段时间了一段时间,但无法弄清楚出了什么问题。首先是我的代码:

//Show the products information line by line
$item_count = 0 ;
for ($i = 0, $n = sizeof($order->products); $i < $n; $i++) {
    $data2[$i][0] = $order->products[$i]['qty'];

    if (strlen($order->products[$i]['name']) > 40 && strlen($order->products[$i]['name']) < 50){
        $data2[$i][1] = $order->products[$i]['name'];
    } else if (strlen($order->products[$i]['name']) > 50) {
        $data2[$i][1] = substr($order->products[$i]['name'],0,50);
    } else {
        $data2[$i][1] = $order->products[$i]['name'];
    }

    $data2[$i][2] = $order->products[$i]['model'];

    $data2[$i][3] = str_replace('&nbsp;', ' ',$currencies->format($order->products[$i]['final_price'], true, $order->info['currency'], $order->info['currency_value']));

    // Add to $data2 if needed (adjust/renumber subsequent array keys!):

    // For VAT purposes:
    // Check if product is an 'electronic service'. If so, determine the customer's country.

    // NOTE (Debug): the comparison of " pov.products_options_values_name = $order->products[$i]['attributes'][$j]['value'] " only holds if the option description is not changed/replaced/deleted.

    if (isset($order->products[$i]['attributes']) && (($k = sizeof($order->products[$i]['attributes'])) > 0)) {
        for ($j = 0; $j < $k; $j++) {
            $virtual_check_query = tep_db_query("select count(*) as total from " . TABLE_PRODUCTS_ATTRIBUTES . " pa, " . TABLE_PRODUCTS_ATTRIBUTES_DOWNLOAD . " pad, " . TABLE_PRODUCTS_OPTIONS_VALUES . " pov where pa.products_id = '" . $order->products[$i]['id'] . "' and pov.products_options_values_name = '" . $order->products[$i]['attributes'][$j]['value'] . "' and pa.products_attributes_id = pad.products_attributes_id");
            $virtual_check = tep_db_fetch_array($virtual_check_query);
            if ($virtual_check['total'] > 0) {
                $product_type_check = 'virtual';
                $order_type_check = 'virtual or mixed';
                break;
            }
        }
    }

    if ($product_type_check == 'virtual') {
        if ($customer_country == 'home country') {
            $data2[$i][4] = str_replace('&nbsp;', ' ',tep_display_tax_value($order->products[$i]['tax']) . '%');
        } elseif ($customer_country == 'eu country') {
            $data2[$i][4] = '***';
        } else {
            $data2[$i][4] = str_replace('&nbsp;', ' ',tep_display_tax_value($order->products[$i]['tax']) . '%');
        }
    } else {
        $data2[$i][4] = str_replace('&nbsp;', ' ',tep_display_tax_value($order->products[$i]['tax']) . '%');
    }

    unset($product_type_check);

    //      $data2[$i][3] = str_replace('&nbsp;', ' ',$currencies->format($order->products[$i]['final_price'], true, $order->info['currency'], $order->info['currency_value']));

    // Add to $data2 if needed (adjust/renumber subsequent array keys!):
    //      $data2[$i][5] = str_replace('&nbsp;', ' ',$currencies->format(tep_add_tax($order->products[$i]['final_price'], $order->products[$i]['tax']), true, $order->info['currency'], $order->info['currency_value']));

    $data2[$i][5] = str_replace('&nbsp;', ' ',$currencies->format($order->products[$i]['final_price'] * $order->products[$i]['qty'], true, $order->info['currency'], $order->info['currency_value'])) ;

    // Add to $data2 if needed (adjust/renumber subsequent array keys!):
    //      $data2[$i][5] = str_replace('&nbsp;', ' ',$currencies->format(tep_add_tax($order->products[$i]['final_price'], $order->products[$i]['tax']) * $order->products[$i]['qty'], true, $order->info['currency'], $order->info['currency_value']));


    // Show the products attributes
    $data3 = array();

    //get attribs
    if (isset($order->products[$i]['attributes']) && (($z = sizeof($order->products[$i]['attributes'])) > 0)) {
        $attribute_line = 'true';
        for ($m = 0; $m < $z; $m++) {
            if ($order->products[$i]['attributes'][$m]['price'] != '0') {
                if (strlen('- ' . $order->products[$i]['attributes'][$m]['option'] . ': ' . $order->products[$i]['attributes'][$m]['value'] . ' (' . $order->products[$i]['attributes'][$m]['prefix'] . $currencies->format($order->products[$i]['attributes'][$m]['price'] * $order->products[$i]['qty'], true, $order->info['currency'], $order->info['currency_value']) . ')') > 50) {
                    $data3[$i][$m][1] = substr('- ' . $order->products[$i]['attributes'][$m]['option'] . ': ' . $order->products[$i]['attributes'][$m]['value'],0,40) . ' (' . $order->products[$i]['attributes'][$m]['prefix'] . $currencies->format($order->products[$i]['attributes'][$m]['price'] * $order->products[$i]['qty'], true, $order->info['currency'], $order->info['currency_value']) . ')';
                } else {
                    $data3[$i][$m][1] = '- ' . $order->products[$i]['attributes'][$m]['option'] . ': ' . $order->products[$i]['attributes'][$m]['value'] . ' (' . $order->products[$i]['attributes'][$m]['prefix'] . $currencies->format($order->products[$i]['attributes'][$m]['price'] * $order->products[$i]['qty'], true, $order->info['currency'], $order->info['currency_value']) . ')' ;
                }
            } else {
                if (strlen('- ' . $order->products[$i]['attributes'][$m]['option'] . ': ' . $order->products[$i]['attributes'][$m]['value']) > 50) {
                    $data3[$i][$m][1] = substr('- ' . $order->products[$i]['attributes'][$m]['option'] . ': ' . $order->products[$i]['attributes'][$m]['value'],0,50);
                } else {
                    //        $data3[$i][$m][1] = '- ' . $order->products[$i]['attributes'][$m]['option'] . ': ' . $order->products[$i]['attributes'][$m]['value'];
                    $data3[$i][$m][1] = '- ' . $order->products[$i]['attributes'][$m]['value'];
                }
            }
        }
    }
}

上面的代码应该是:

  • 将常规产品数据放入数组$ data2

  • 将每个产品的属性(可以多于一个)放入数组$ data3

数组$ order-&gt;产品包含以下数据:

Array (

[0] => Array ( 
[qty] => 1 
[id] => 4 
[name] => Product A
[model] => 
[tax] => 21.0000 
[price] => 19.9900 
[final_price] => 19.9900 
[attributes] => Array ( 
[0] => Array ( 
[option] => Type 
[value] => X 
[prefix] => + 
[price] => 0.0000 
) 
) 
) 

[1] => Array ( 
[qty] => 1 
[id] => 4 
[name] => Product A
[model] => 
[tax] => 21.0000 
[price] => 19.9900 
[final_price] => 19.9900 
[attributes] => Array ( 
[0] => Array ( 
[option] => Type 
[value] => Y
[prefix] => 
[price] => 0.0000 
) 
) 
) 
)

代码在数组$ data3中输入以下数据:

Array ( 
[1] => Array (
[0] => Array (
[1] => - Y
) 
) 
) 

这意味着缺少第一个产品($ order-&gt; products数组中带有键0的产品A)的属性!

我的直觉告诉我,在执行代码的“显示产品属性”部分之前,代码已经移动了$ order-&gt; products数组。

该代码基于 checkout_confirmation.php (osCommerce)获取的代码段:

for ($i=0, $n=sizeof($order->products); $i<$n; $i++) {
echo '          <tr>' . "\n" .
 '            <td class="infoBoxMargin">' . tep_draw_separator('pixel_trans.gif', '10', '1') . '</td>' . "\n" .
     '            <td class="main" align="left" valign="top" width="10%">' . $order->products[$i]['qty'] . '&nbsp;x</td>' . "\n" .
     '            <td class="main" align="left" valign="top" width="60%">' . $order->products[$i]['name'];

if (STOCK_CHECK == 'true') {
  echo tep_check_stock($order->products[$i]['id'], $order->products[$i]['qty']);
}

if ( (isset($order->products[$i]['attributes'])) && (sizeof($order->products[$i]['attributes']) > 0) ) {
  for ($j=0, $n2=sizeof($order->products[$i]['attributes']); $j<$n2; $j++) {
echo '<br /><nobr><small>&nbsp;<i> - ' . $order->products[$i]['attributes'][$j]['option'] . ': ' . $order->products[$i]['attributes'][$j]['value'] . '</i></small></nobr>';
  }
}

任何人都可以向我解释哪里出了问题吗?

亲切的问候,

丹尼斯

3 个答案:

答案 0 :(得分:1)

使用$order->products的示例数据,$order->products[$i]['attributes'][$m]['price']始终为零。此外,由于strlen('- ' . $order->products[$i]['attributes'][$m]['option'] . ': ' . $order->products[$i]['attributes'][$m]['value'])始终<50,因此//get attribs部分中与您的示例数据一起执行的唯一代码是$data3[$i][$m][1] = '- ' . $order->products[$i]['attributes'][$m]['value']; ...这就是为什么$data3[$i][$m][1]是只有填充的值。

但是,如果假设您的两个}遗失,那么$data3$i所在的$data3 = array();的每个值都会被覆盖。< / p>

如果您将$data3 = array();移到代码顶部(for循环之前),则至少不会覆盖每个版本产品的$ data3中的值。

如果您希望$ data3包含每个属性的副本,请尝试将$data3[$i][$m][1] = '- ' . $order->products[$i]['attributes'][$m]['value'];更改为$data3[$i][$m] = $order->products[$i]['attributes'];

答案 1 :(得分:1)

@ lazy.lizard

如果您知道自己在做什么,那就太容易了!

代码现在以:

开头
$data2=array();
$data3 = array();

//Show the products information line by line
$item_count = 0 ;
for ($i = 0, $n = sizeof($order->products); $i < $n; $i++) {

$data2[$i][0] = $order->products[$i]['qty'];

答案 2 :(得分:0)

多年来处理osCommerce代码我不得不说,有很多奇怪的代码,如果你不长时间使用它,你最好转移到新的东西,至少不使用register_globals。迁移的另一个原因是没有MVC模型 - 你总是看到混合的PHP代码,html布局和SQL查询,尤其是在管理部分。迁移的另一个重要原因是表结构,或者更确切地说,是缺少或错误的索引。

至于我,我已经完全重写了很多代码,包括全新的产品类和产品属性。现在,例如,我在任何地方都使用产品对象,包括产品列表,购物车,心愿单等。相信我,我不会对单独的SQL查询感到头疼,这些查询可以获得不同的产品。数据

现在问你。我想这个问题就在这里:

$data3 = array();

每次循环$ order-&gt;产品时,您的变量都设置为空数组。因此,尝试在开始迭代$ order-&gt;产品之前定义 。例如:

$data3 = array();
for ($i = 0, $n = sizeof($order->products); $i < $n; $i++) {
// your code
}

并且不要忘记删除$ data3 = array();来自循环。

希望这有帮助。

祝你好运。