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 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记