dongtongjian3127 2011-02-04 17:24
浏览 42
已采纳

在foreach循环中加价(PHP) - 不加总

... It's not as silly as it sounds...

I have the following code, which is used by my ajax table script to display database stuff on the page in a table.

foreach($ct->data as $key => $value){
    $ct->data[$key][2]='<a href="quantity.php?partno='.$ct->data[$key][0].'&description='.$ct->data[$key][1].'&quantity='.$ct->data[$key][2].'&order='.$o.'">'.$ct->data[$key][2].'</a>';
    $ct->data[$key][3]='<a href="quantity.php?partno='.$ct->data[$key][0].'&description='.$ct->data[$key][1].'&price='.$ct->data[$key][3].'&order='.$o.'">'.$ct->data[$key][3].'</a>';

    if($ct->data[$key][4] == "" || $ct->data[$key][4] == null)
        $ct->data[$key][4]='<a href="freight.php?partno='.$ct->data[$key][0].'&description='.$ct->data[$key][1].'&freight='.$ct->data[$key][4].'&order='.$o.'">Edit Charge.</a>';
    else
        $ct->data[$key][4]='<a href="freight.php?partno='.$ct->data[$key][0].'&description='.$ct->data[$key][1].'&freight='.$ct->data[$key][4].'&order='.$o.'">'.$ct->data[$key][4].'</a>';

    $Total =$Total+ $ct->data[$key][3];
    $freight =$freight+ $ct->data[$key][4];
}

And as you can see, in the foreach loop, I am trying to add up the contents of 2 columns.

The $Total column or, $ct->data[$key][3] lists the Prices for each row of products, and the $freight column does the same for each row of Freight charges.

And inside the foreach loop, I am trying to add together the total amount of prices, and Freight charges.

I'm not sure if I'm doing it the right way, because when I check the database, it just adds '0' (without the quotes). So it's not adding up!

For example, if there are a total of 3 rows in the table, and each product is 1 (dollar), it should add up to 3, right? And same goes for the $freight ones.

Can someone please tell me what I'm doing wrong here?

  • 写回答

2条回答 默认 最新

  • doujiene2845 2011-02-04 17:28
    关注

    You are setting data[$key][3] equal to some HTML hyper link. Its not something that can be "totalled"

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度