如果没有价格,PHP添加符号

时间:2013-03-13 09:35:47

标签: php magento-1.7

有两种产品的数量和价格不同

    simple product -1 :         qty      price
                                 2        150
                                 3        145
                                 5        130
                                 10      100


    simple product -2 :         qty      price
                                 2        195
                                 5        175
                                 9        170

我想以下列格式显示数量和价格:

qty : 2          3          5       9         10

     $150       $145       $130      -        $100

     $195         -        $175     $170       -  

以下是显示价格和数量的代码

/*
$_tResult = [2, 3, 5, 9, 2, 5, 10]
*/


     /*
$_tierPrice value


    array(10) {
      ["price_id"] => string(2) "18"
      ["website_id"] => string(1) "0"
      ["all_groups"] => string(1) "1"
      ["cust_group"] => int(32000)
      ["price"] => string(8) "150.0000"
      ["price_qty"] => float(2)
      ["website_price"] => string(8) "150.0000"
      ["formated_price"] => string(34) "150.00"
      ["savePercent"] => float(4)
      ["formated_price_incl_tax"] => string(34) "150.00"
    }

    array(10) {
      ["price_id"] => string(2) "65"
      ["website_id"] => string(1) "0"
      ["all_groups"] => string(1) "1"
      ["cust_group"] => int(32000)
      ["price"] => string(8) "120.0000"
      ["price_qty"] => float(3)
      ["website_price"] => string(8) "120.0000"
      ["formated_price"] => string(34) "120.00"
      ["savePercent"] => float(23)
      ["formated_price_incl_tax"] => string(34) "120.00"
    }

    array(10) {
      ["price_id"] => string(2) "61"
      ["website_id"] => string(1) "0"
      ["all_groups"] => string(1) "1"
      ["cust_group"] => int(32000)
      ["price"] => string(8) "145.0000"
      ["price_qty"] => float(5)
      ["website_price"] => string(8) "145.0000"
      ["formated_price"] => int(5) "145.00"
      ["savePercent"] => float(7)
      ["formated_price_incl_tax"] => string(34) "145.00"
    }

    array(10) {
      ["price_id"] => string(2) "62"
      ["website_id"] => string(1) "0"
      ["all_groups"] => string(1) "1"
      ["cust_group"] => int(32000)
      ["price"] => string(8) "130.0000"
      ["price_qty"] => float(9)
      ["website_price"] => string(8) "130.0000"
      ["formated_price"] => int(5) "130.00"
      ["savePercent"] => float(17)
      ["formated_price_incl_tax"] => string(34) "130.00"
    }

    array(10) {
      ["price_id"] => string(2) "47"
      ["website_id"] => string(1) "0"
      ["all_groups"] => string(1) "1"
      ["cust_group"] => int(32000)
      ["price"] => string(8) "190.0000"
      ["price_qty"] => float(2)
      ["website_price"] => string(8) "190.0000"
      ["formated_price"] => int(5) "190.00"
      ["savePercent"] => float(5)
      ["formated_price_incl_tax"] => string(34) "190.00"
    }

    array(10) {
      ["price_id"] => string(2) "63"
      ["website_id"] => string(1) "0"
      ["all_groups"] => string(1) "1"
      ["cust_group"] => int(32000)
      ["price"] => string(8) "175.0000"
      ["price_qty"] => float(5)
      ["website_price"] => string(8) "175.0000"
      ["formated_price"] => int(5) "175.00"
      ["savePercent"] => float(13)
      ["formated_price_incl_tax"] => string(34) "175.00"
    }

    array(10) {
      ["price_id"] => string(2) "64"
      ["website_id"] => string(1) "0"
      ["all_groups"] => string(1) "1"
      ["cust_group"] => int(32000)
      ["price"] => string(8) "195.0000"
      ["price_qty"] => float(9)
      ["website_price"] => string(8) "195.0000"
      ["formated_price"] => int(5) "195.00"
      ["savePercent"] => float(3)
      ["formated_price_incl_tax"] => string(34) "195.00"
    }

    array(10) {
      ["price_id"] => string(2) "44"
      ["website_id"] => string(1) "0"
      ["all_groups"] => string(1) "1"
      ["cust_group"] => int(32000)
      ["price"] => string(8) "170.0000"
      ["price_qty"] => float(10)
      ["website_price"] => string(8) "170.0000"
      ["formated_price"] => int(5) "170.00"
      ["savePercent"] => float(15)
      ["formated_price_incl_tax"] => string(34) "170.00"
    }
    */




 <?php $_item->setData('tier_price',null); ?>
          <?php $_tierPrices = $this->getTierPrices($_item); ?>
          <?php //print_r($_tResult); ?>
          <?php foreach ($_tierPrices as $price): ?>
                  <td>
                        <?php if(in_array($price['price_qty'],$_tResult)) :?>
                               <?php echo $price['formated_price']; ?>
                        <?php else: ?>
                               <?php echo "-"; ?>
                        <?php endif; ?>
                   </td>    
           <?php endforeach; ?>

以下是代码的实际输出。

qty :     2          3          5       9         10 

         $150       $145       $130    $100

         $195       $175       $170      

如果数组中没有价格值,我该如何添加“ - ”

2 个答案:

答案 0 :(得分:0)

试试这个:

<?php $_item->setData('tier_price',null); ?>
          <?php $_tierPrices = $this->getTierPrices($_item); ?>
          <?php //print_r($_tResult); ?>
          <?php foreach ($_tierPrices as $price): ?>
            <td>
               <?= (!empty($price['price'])) ? $price['formated_price']: "-"; ?>
            </td>    
           <?php endforeach; ?>

如果不起作用:

替换:

<?= (!empty($price['price_qty'])) ? $price['formated_price']: "-"; ?>
with
<?= (!empty($price['formated_price'])) ? $price['formated_price']: "-"; ?>

评论或编辑您的问题。告诉我们输出的是什么:

echo "<pre>"; print_r($price); echo "</pre>";

答案 1 :(得分:-1)

代码中的主要逻辑错误是数组元素不等于上面的数字。

您需要做的是创建一个包含元素的数组,并在要显示的键中使用[-]