PHP函数应该返回数组,但只返回mysql表中的一个结果
我希望我的日历显示mysql表中的所有事件,但这段代码只显示最后一个事件。这是我的密码:PHP函数应该返回数组,但只返回mysql表中的一个结果,php,arrays,function,calendar,return-value,Php,Arrays,Function,Calendar,Return Value,我希望我的日历显示mysql表中的所有事件,但这段代码只显示最后一个事件。这是我的密码: $result5 = mysqli_query($con,"SELECT * FROM event WHERE username IN ('$matches')"); $num_rows = mysqli_num_rows($result5); while($row5 = mysqli_fetch_array($result5)) { $tim
$result5 = mysqli_query($con,"SELECT * FROM event WHERE username IN ('$matches')");
$num_rows = mysqli_num_rows($result5);
while($row5 = mysqli_fetch_array($result5))
{
$time=strtotime($row5['start_date']);
$day = date("d",$time);
$month= date("m",$time);
$month2= date("F",$time);
$year=date("Y",$time);
echo "$day $month $year ".$row5['name_event'] ; // this show me correct
echo '<br/>';
$calendar = new donatj\SimpleCalendar();
$calendar->addDailyHtml( "Event", "$day-$month-$year");// there is some problem
}
$calendar->show(true);
$result5=mysqli_查询($con,“从事件中选择*用户名(“$matches”)”);
$num_rows=mysqli_num_rows($result5);
而($row5=mysqli\u fetch\u数组($result5))
{
$time=strottime($row5['start_date']);
$day=日期(“d”,时间为$time);
$month=日期(“m”,时间为$time);
$month2=日期(“F”,时间为$time);
$year=日期(“Y”,时间为$time);
echo“$day$month$year”。$row5['name_event']];//这显示正确吗
回声“
”;
$calendar=new donatj\SimpleCalendar();
$calendar->addDailyHtml(“事件”,“$day-$month-$year”);//出现了一些问题
}
$calendar->show(true);
下面是我使用的函数的代码:
public function addDailyHtml( $html, $start_date_string, $end_date_string = null ) {
static $htmlCount = 0;
$start_date = strtotime($start_date_string);
if( $end_date_string ) {
$end_date = strtotime($end_date_string);
} else {
$end_date = $start_date;
}
$working_date = $start_date;
do {
$tDate = getdate($working_date);
$working_date += 86400;
$this->daily_html[$tDate['year']][$tDate['mon']][$tDate['mday']][$htmlCount] = $html;
} while( $working_date < $end_date + 1 );
$htmlCount++;
}
public函数addDailyHtml($html、$start\u date\u string、$end\u date\u string=null){
静态$htmlCount=0;
$start\u date=strottime($start\u date\u string);
如果($end\u date\u字符串){
$end\u date=strottime($end\u date\u字符串);
}否则{
$end_date=$start_date;
}
$working\u date=$start\u date;
做{
$tDate=getdate($working\u date);
$working_date+=86400;
$this->daily_html[$tDate['year'][$tDate['mon'][$tDate['mday']][$htmlCount]=$html;
}while($working_date<$end_date+1);
$htmlCount++;
}
有人能帮我解释一下为什么日历只显示一个事件吗?您每次都在循环中创建一个新的$calendar对象。因此,当您在foreach之后调用$calendar->show()时,您将在最后一个对象上调用它。不要只在查询中嵌入变量而不进行清理。第一个传入的参数是字符字符串“Event”。该字符串用于设置$htmlCount。然后,$htmlCount将递增1。这可能不是您计划要做的。WizKid-这是正确的,但当我放置$calendar->show()时,它们会显示3-4个日历或表中的任何事件。扎拉图斯特拉-你的评论没有谈到这个问题,我净化了每个变量。user3629249-很好,但我不知道如何解决它。有人能把一些代码,将工作?