gpt4 book ai didi

javascript - PHP 与 jQuery : how to do it in this case

转载 作者:行者123 更新时间:2023-12-01 05:42:21 25 4
gpt4 key购买 nike

我有一个 jQuery 日历,一天有 3 个事件。如果任何客户预订了某个事件,我的日历会在我将其提交到 SQL 数据库后显示该事件已预订。

因此,如果我只在 SQL 中提交任何日期,我的日历就会显示它。这意味着通过 PHP

if($date > 0) { echo 'here is result';}

为了显示任何事件,我想为页面中的特定事件添加以下脚本: { "EventID": 1, "StartDateTime": new Date(2015,3, 1), "Title": "上午 10 点至下午 2 点", "URL": "用户名", "描述": "预订者 ", "CssClass": "Event_1"},

现在在我的 SQL 中,我为所有事件添加了 3 个事件 * 31 天 = 93 行。如果在sql中通过id在任何行的日期字段中找到任何数据,我的页面将显示上述脚本。

为了更清楚起见,我给出了这个错误的脚本,我想通过 jQuery 和 PHP 来正确执行该脚本。

<?php if($id = '1'){if($date > 0){ echo' ?>
{ "EventID": 1, "StartDateTime": new Date(2015,3,<? echo '$date';?>), "Title": "10am to 2pm", "URL": "#", "Description": "Booked by ", "CssClass": "Event_1" },
<?php ';}} ?>

现在我不明白如何通过 jQuery 和 php 来做到这一点?在这里我读了很多文章,但我失败了。

这是我的代码示例:

<?php
include_once('db.php');
global $db;
$result = mysqli_query($db,"SELECT * FROM room ORDER BY id");
if(!$result) {
die("Database query failed: " . mysqli_error());
}
while($row = mysqli_fetch_assoc($result)) {
$id=$row['id'];
$date=$row['date'];
}
?>

<script type="text/javascript">
$().ready(function() {
// others script here

var events = [
{ "EventID": 1, "StartDateTime": new Date(2015,3, 1), "Title": "10am to 2pm", "URL": "username1", "Description": "Booked by ", "CssClass": "Event_1" },}
{ "EventID": 2, "StartDateTime": new Date(2015,3, 1), "Title": "2pm to 6pm", "URL": "username2", "Description": "Booked by ", "CssClass": "Event_2" },
{ "EventID": 3, "StartDateTime": new Date(2015,3, 1), "Title": "6pm to 10pm", "URL": "username3", "Description": "Booked by ", "CssClass": "Event_3" }
// All events
];
});
</script>

最佳答案

根据您的代码,我做了一些调整。我还没有测试过,但应该可以工作,或者至少指向正确的方向:

<?php
include_once('db.php');
global $db;
$result = mysqli_query($db,"SELECT * FROM room ORDER BY id");
if(!$result) {
die("Database query failed: " . mysqli_error());
}

$events = array();
while($row = mysqli_fetch_assoc($result)) {
if( !isset( $events[$row['date']]) ){ //create array of events for each day with results
$events[$row['date']] = array();
}

$num_events = count($events[$row['date']]);
$date = new DateTime($row['date']);

$events[$row['date']][] = array(
"EventID" => 1,
"StartDateTime" => $date->format('Y-m-d'), //assuming real mysql datetime
"Title" => "10am to 2pm",
"URL" => "username1",
"Description" => "Booked by ",
"CssClass" => "Event_" . $num_events + 1
);
}
?>

<script type="text/javascript">
$(function(){
var events = '<?php echo json_encode($events);?>'
for (day in events){
var day_events = events[day];
for(var i= 0; i< day_events.length; ++i){
day_events[i].StartDateTime = new Date(day_events[i].StartDateTime); //convert into proper date
console.log(day_events[i]);
}
}
});
</script>

关于javascript - PHP 与 jQuery : how to do it in this case,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29961190/

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