douhuang3740 2018-08-01 18:01
浏览 80
已采纳

PHP + MySQL:显示具有相同值的所有记录

I need some help regarding displaying multiple MySQL records where the date values are the same.

Example:

========================== 
31 July
Record 1 

2 August
Record 2
Record 3 

5 August
Record 4
Record 5 

6 August
Record 6
========================== 

MySQL records example data:

nid | neventName | nstartDate
----+------------+-----------
| 1 | Record 1   | 2018-07-31 
| 2 | Record 2   | 2018-08-02 
| 3 | Record 3   | 2018-08-02 
| 4 | Record 4   | 2018-08-05 
| 5 | Record 5   | 2018-08-05 
| 6 | Record 6   | 2018-08-08

My code for this section is the following:

<?php
//DATE VALUES
date_default_timezone_set('Asia/Dubai');
$currentDate = date("Y-m-d");
$startDate = $rs_EventsVenuesListing_rows['nstartDate'];
$date = date_create($startDate);

//CREATE SQL STATEMENT
$sql_EventsVenuesListing = "SELECT * FROM tblvenueListingsEvents WHERE $currentDate <= nstartDate ORDER BY nstartDate ASC";
//$sql_EventsVenuesListing = "SELECT DISTINCT nstartDate FROM tblvenueListingsEvents WHERE $currentDate <= nstartDate ORDER BY nstartDate ASC";

//CONNECT TO MYSQL SERVER
require('inc-conn.php');

//EXECUTE SQL STATEMENT
$rs_EventsVenuesListing = mysqli_query($vconn, $sql_EventsVenuesListing);

//CREATE AN ASSOCIATIVE ARRAY
//$rs_EventsVenuesListing_rows = mysqli_fetch_assoc($rs_EventsVenuesListing);
?>

<?php

  echo "<table border = 1px>";
  while ($rs_EventsVenuesListing_rows = mysqli_fetch_assoc($rs_EventsVenuesListing)) {
  // printing table row
  // ##### DISPLAY ALL DATES USING START DATE FROM EVENTS #####
  echo'<tr>';
  echo '<td>'.$rs_EventsVenuesListing_rows['nstartDate'].'</td>';
  echo'</tr>'; // closing table row

  // ##### DISPLAY ALL EVENTS THAT HAS THE SAME START DATE #####
  if ($startDate = $startDate) {
  echo'<tr>';
  echo '<td>'.$rs_EventsVenuesListing_rows['neventName'].'</td>';
  echo'</tr>'; // closing table row
  }

  echo'<tr>';
  echo '<td><br></td>';
  echo'</tr>'; // closing table row
  }

echo '</table>';
?>
<!-- ######################## DISPLAY DATES ######################## -->


Your assistance will gladly be appreciated.
Regards,
Dane

  • 写回答

1条回答 默认 最新

  • droxy80248 2018-08-01 18:15
    关注

    Here is a simplyfied snippet of how such a structure

    Header1
      data1
      data2
    Header2
      data3
    ...
    

    works:

    $startdate = null; // initialize
    
    while($row = mysqli_fetch_assoc($rs_EventsVenuesListing)) {
    
        if($startdate != $row['nstartDate']) {
            // echo HEADER ROW (the date) HERE!
    
            // set startdate to the new one:
            $startdate = $row['nstartDate'];
        }
    
        // allways echo DATA ROW (Record 1) HERE
    
    }
    

    This code is not meant for copy & paste. It shall only show the logic behind.

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

报告相同问题?

悬赏问题

  • ¥15 删除虚拟显示器驱动 删除所有 Xorg 配置文件 删除显示器缓存文件 重启系统 可是依旧无法退出虚拟显示器
  • ¥15 vscode程序一直报同样的错,如何解决?
  • ¥15 关于使用unity中遇到的问题
  • ¥15 开放世界如何写线性关卡的用例(类似原神)
  • ¥15 关于并联谐振电磁感应加热
  • ¥60 请查询全国几个煤炭大省近十年的煤炭铁路及公路的货物周转量
  • ¥15 请帮我看看我这道c语言题到底漏了哪种情况吧!
  • ¥66 如何制作支付宝扫码跳转到发红包界面
  • ¥15 pnpm 下载element-plus
  • ¥15 解决编写PyDracula时遇到的问题