drny60365 2015-02-18 02:58
浏览 41
已采纳

如何在foreach中显示数组-1

$query = "SELECT A.Date, B.Fee
FROM [Client] A JOIN [Price] B
ON A.SID = B.SID
WHERE A.Date BETWEEN '$from' AND '$to' AND A.ClientID = '$client'
ORDER BY A.Date";

I use the above query to display fee of transactions in a period (usually monthly), and where there's no transaction in a day it should display the fee from the last day transaction while there's no transaction data on that day. For example I wanna display this:

2015-01-05 | 234,567
2015-01-06 | 123,456
2015-01-07 | 123,456 // this is taken from 0106 cause no transaction on Sat
2015-01-08 | 123,456 // this is taken from 0106 cause no transaction on Sun
2015-01-09 | 345,678

While the data in database:

2015-01-05 | 234,567
2015-01-06 | 123,456
2015-01-09 | 345,678

I use the code below to display the data, and to do the trick currently, I re-query the data from date+1 and date+2 and display the friday's data. The problem is that won't do it when it's holiday (and I don't think that's efficient).

$stmt = $con->prepare($query);
$stmt->execute();
$rows = $stmt->fetchAll();
$num = count($rows);
foreach ($rows as $row) {
  echo date('d-m-Y', strtotime($row['Date']));
  echo number_format($row['Fee'], 0, ",", ".");
  }

I'm hoping for a solution.

  • 写回答

1条回答 默认 最新

  • droirqk4795 2015-02-18 06:01
    关注

    Okay I figured it out by the help on the comment above and here's the sqlfiddle:

    http://sqlfiddle.com/#!3/2d6b8/1

    Credit goes to @Turophile. Thank you.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line