duanli8391 2015-07-10 18:53
浏览 35

php mysql如何在datewise中获取结果行

I need to get the result rows as date wise for example, i have

prs_id date                desc
1      2015-07-10          Test data1
2      2015-07-10          Test data2
3      2015-07-11          Test data3

med_id prs_id drug                dosage
1      1      Pracetamol          3
2      1      calpol              4
3      2      crocin              5
4      3      xxxxxx              8

i need to form the array and need to display like the below

2015-07-10
     1 Test data1

          1      1      Pracetamol          3
          2      1      calpol              4

     2 Test data2
          3      3      crocin              5

2015-07-11
     3 Test data3
          4      3      xxxxxx              8

I tried the following code

$sql = "SELECT * from prescriptions";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {

                $answer[$row['prs_created_date']][] = $row;


    }
}

below code simply prints the array, with prescription data as the key, thanks Marc B for this. But i need to print as the expected table, how can i do that.

echo '<pre>';
print_r($answer);
  • 写回答

2条回答 默认 最新

  • dongmei8071 2015-07-10 19:01
    关注

    Sort your results using order by date_col and then in php use below logic to display your results

    $sql = "SELECT * from prescriptions order by date_col";
    $result = $conn->query($sql);
    $date= false;
    if ($result->num_rows > 0) {
    
        while($row = $result->fetch_assoc()) {
            if ($date != $row['date'])
            {
            echo '<h1>'.$row['date'].'</h1>';
            $date = $row['date'];
            }
            echo '<p>' . $row['id'] . '</p>';
            echo '<p>' . $row['desc'] . '</p>';
            .
            .
            .
        }
    
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染