SQL查询使用不同的数组数据多次显示同一条记录


SQL query displaying same record multiple times with different array data

我不确定我做错了什么,但基本上我试图通过数组(done)列出一个月中的日期,但trips表中预订日期范围内的日期对它们来说有不同的外观,这就是目前的样子

http://oi60.tinypic.com/2zzrc03.jpg

我正试图这样输出。。

March 1[2 3 4 5]6[7 8 9 10]

代码在很大程度上是有效的,但对于有多个事件的月份,它会重复该月份,并在自己的月份行中显示日期

这是我的代码,是的,我知道它是mysql,但目前这是一个本地项目,所以我正在努力让它在进入这一步之前发挥作用。

<table width="100%" cellspacing="0">
  <?php
    $cmonth = date('F'); 
    $cyear = date('Y');
    $sql = "SELECT * FROM calendar WHERE year = '$cyear' ORDER BY m_order ASC";
    $res = mysql_query($sql);
    while ($rows = mysql_fetch_array($res)) {
      $month_end = $rows['days_in_month'];
      $month_name = $rows['month_name'];
      $m_order = $rows['m_order'];
      $sql2 = "SELECT * FROM trips WHERE start_date LIKE '____-0$m_order-__' ORDER BY start_date ASC";
      $res2 = mysql_query($sql2);
      while ($row = mysql_fetch_array($res2)) {
        $stdate = $row['start_date'];
        $s = date_parse_from_format("Y-m-d", $stdate);
        $endate = $row['end_date'];
        $e = date_parse_from_format("Y-m-d", $endate);
        $start = $s['day'];
        $end = $e['day'];
  ?>
    <tr>
    <td width="80px"><?php echo $month_name; ?></td>
      <?php
        foreach(range(1, $month_end) as $days) 
        {
          if(in_array($days, range($start, $end)))
          {
            echo "<td style='"background-color: #ccc;'" align='"center'">".  $days ."    </td>";`
          }
          else
            echo "<td align='"center'">". $days ."</td>"; 
        }
      ?>
    </tr>
    <?php } 
    } ?>
</table>

我只是想知道我的编码是否错误,或者我是否应该尝试用另一种方法来实现我的目标?欢迎提出任何建议。

通过四处玩耍和与人交谈,我使它发挥了作用,消除了对日历表的需求,并更改了数组。

<?php 
function date_range($first, $last, $step = '+1 day', $output_format = 'Y-m-d' ) {
$dates = array();
$current = strtotime($first);
$last = strtotime($last);
while( $current <= $last ) {
    $dates[] = date($output_format, $current);
    $current = strtotime($step, $current);
}
return $dates;
}
?>
<table width="100%" cellspacing="0" cellpadding="0">
<?php
$cmonth = date('F'); 
$cyear = date('Y');
$sql = "SELECT * FROM trips WHERE year(start_date) = '$cyear' ORDER BY   start_date ASC";
$res = mysql_query($sql);
$array_days = array();
while ($rows = mysql_fetch_array($res)) {
$all_dates[] = array('start'=>strtotime($rows['start_date']),'end'=>strtotime($rows['end_date']));
$selected_dates = date_range(date("Y-m-d",strtotime($rows['start_date'])), date("Y-m-d",strtotime($rows['end_date'])));
foreach($selected_dates as $selected_date){
    $array_days[$selected_date] = date("d",strtotime($selected_date));
}
}
 $current_month = date("m");
 $next_6_month = date("m", strtotime("+5 month", strtotime(date("F") .  "1")));
    for($i=$current_month;$i<=$next_6_month;$i++){  // 12 months in year
 ?>
  <tr>
  <td width="40px"><?php echo date('M', mktime(0, 0, 0, $i,10, $cyear)); ?></td>
<?php 
$days_in_month = cal_days_in_month(CAL_GREGORIAN,$i,$cyear);
foreach(range(1, $days_in_month) as $days) 
{
 if(array_key_exists(date('Y-m-d', mktime(0, 0, 0, $i,$days, $cyear)),$array_days)){
    echo "<td style='text-align:center;background-color:ccc' width='12px'>$days</td>";
}else{
    echo "<td style='text-align:center;' width='12px'>$days</td>";
}
 }
 ?>
 </tr>
<?php } ?>

 </table>

首先使用prepared语句和mysqli,因为使用当前方法可能会注入sql!

尝试使用SELECT DISTINCT

http://www.w3schools.com/sql/sql_distinct.asp