我正在编程约会刨床,但仍然在努力解决javascript没有生成事件并以纯文本方式打印代码的问题,所以起初我有一个php函数从sql获取所有约会数据库并生成javascript代码
<script>
function getEventData() {
<?php
function showAppointments($von, $bis){
$link = OpenDB();
try {
$query = 'SELECT termin_id, client, dateFrom, dateTill, title, descri FROM termin WHERE dateFrom BETWEEN :von AND :bis';
$statement = $link->prepare($query);
$statement->bindValue(':von', $von, PDO::PARAM_STR);
$statement->bindValue(':bis', $bis, PDO::PARAM_STR);
$statement->execute();
$row = $statement->fetchAll();
}
catch (PDOException $e) {
p($e);
}
CloseDB($link);
return $row;
}
if(empty($von) && empty($bis)) {
$von = date("Y.m.j",strtotime("monday this week"));
$bis = date("Y.m.j",strtotime("sunday this week"));
}
if(isset($_POST['last_week'])){
$von = date("Y.m.j",strtotime("previous monday"));
$bis = date("Y.m.j",strtotime("previous sunday"));
}
if(isset($_POST['next_week'])){
$von = date("Y.m.j",strtotime("next monday"));
$bis = date("Y.m.j",strtotime("next sunday"));
}
$rows = showAppointments($von, $bis);
echo "const appointments = [";
for($i = 0; $i < sizeof($rows); $i++) {
$columns = array_values($rows[$i]);
echo "[";
for($j=0; $j< sizeof($columns); $j++){
echo "'".$columns[$j]."'";
if( $j < sizeof($columns)-1) echo ",";
}
echo "]";
if( $i < sizeof($rows)-1) echo ",";
}
echo "];";
?>
then all events are generated by jquery and returned
return {
events:[
$.each(appointments, function(index, row){
eval("{'id':"+ row[0] + ",'client':"+ row[1] +",'start':" + new Date(row[2])+",'end': "+ new Date(row[3]) +",'title':'"+ row[4] +"','description':'"+ row[5] +"'}");
});
]}}
</script>
foreach中的代码以纯文本形式打印,如何解决问题?它实际上应该打印像
这样的东西{'id':1, 'start': new Date(year, month, day, 12), 'end': new Date(year, month, day, 13, 30),'title':'Lunch with Mike'}
答案 0 :(得分:1)
你应该在事件外部尝试Jquery.each,然后使用eval(Array.join);在JavaScript中,这应该工作