gpt4 book ai didi

php - SQL查询使用不同的数组数据多次显示相同的记录

转载 作者:行者123 更新时间:2023-11-29 12:19:23 26 4
gpt4 key购买 nike

我不确定我做错了什么,但基本上我试图通过数组(完成)输出该月的日期,但该日期在我的行程<的预订日期范围内 表的外观与它们不同,这就是它当前的样子

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>

关于php - SQL查询使用不同的数组数据多次显示相同的记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29268654/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com