我有一个问题,涉及从日历程序中的表中收集事件,在该程序中,事件被分为"事件"或"重复事件"。我现在可以很好地处理所有的单个事件(感谢Chris在这个网站上),但如果它们是重复事件,我必须根据这个特定数据库中给出的值来计算。如果我更改数据库中的类型或数据,它可能会破坏日历,所以我必须使用我所拥有的。
到目前为止,我整理的变量是:
$quid2
=今天被分类为重复事件的事件的ID(以前需要)$quname
=重复的事件名称$qucls
=最后一次发送事件提醒的日期UNIX时间$qutype
=其中一个单词-每日、每周、每月或每年$qudesc
=事件描述
这些变量都有相同数量的项目,并且彼此之间的顺序正确。(我希望)
以下是我试图实现的逻辑。这肯定不是正确的语法,但我认为这是可以理解的;我需要弄清楚语法和形式是什么。我对这方面完全陌生。。。所以请温柔一点。。。
它需要放在一个数组中(我认为)
$arr1 = some type of array($quname, $qucls, $qutype, $qudesc)
更新数组。。。
- 如果
$qutype($row2)
="每日",则+1440为$qucls($row[1])
- 如果
$qutype($row2)
="周",则+10080为$qucls($row[1])
- 如果
$qutype($row2)
="每月,则+1个月为$qucls($row[1])
- 如果
$qutype($row2)
="每年",则+1年为$qucls($row[1])
然后最终产品。。。
while ($row = mysql_fetch_array($arr1, MYSQL_NUM)) {
$UxTime = $row[1];
date_default_timezone_set('America/Denver');
$Time = date("H:i", $UxTime);
$qufinal = sprintf("Event: %s 'nTime: %s 'nDesc: %s 'n'n", $row[0], $Time, $row[3);
}
...
这对我来说是一个很大的学习项目。我不知道足够的PHP和mysql来完成这项工作,但我知道的只是给我带来麻烦。谢谢
编辑:添加我生成这些变量的查询:
$today = date("Ymd");
mysql_select_db($dbname);
$query1 = "SELECT cal_id, cal_name, cal_date, cal_time, cal_type, cal_description FROM webcal_entry WHERE cal_type = "M" AND cal_date != " . $today;
$wequ1 = mysql_query($query1)
while ($row = mysql_fetch_array($wequ1, MYSQL_NUM)) {
$quid1 = $row[0], $quname = $row[1], $qutime = $row[2], $qudesc = $row[3];
}
$query2 = "SELECT cal_id, cal_type, cal_ByDay FROM webcal_entry_repeats WHERE cal_id = " . $quid1;
$wer1 = mysql_query($query2)
while ($row = mysql_fetch_array($wer1, MYSQL_NUM)) {
$quid2 = $row[0] $qutype = $row[1], $qubdy = $row[2];
}
$query3 = "SELECT cal_id, cal_last_sent FROM webcal_reminders WHERE cal_id = " . $quid2;
$wer2 = mysql_query($query3)
while ($row = mysql_fetch_array($wer2, MYSQL_NUM)) {
$qucls = $row[1];
}
数组的语法如下:
$arrayName = array($quname, $qucls, $qutype, $qudesc);
然后,您可以通过数组变量上的索引来访问这些值:
$arrayName[0] == $quname
$arrayName[1] == $qucls
...
您也可以将其定义为关联数组:
$arrayName = array(
"quname" => $quname,
"qucls" => $qucls,
"qutype" => $qutype,
"qudesc" => $qudesc
);
使用此语法,您可以通过元素名称访问元素:
$arrayName["quname"] == $quname
$arrayName["qucls"] == $qucls
...
更多阅读:阵列
然而,您并不真的需要一个数组来执行您计划在这里执行的操作。如果要存储结构上相等的数据,数组非常有用。例如,这适用于数据库中的行:它们总是具有相同数量的条目,并且列的类型相同。
如果在代码的那个点上只有一个数据集(在本例中是一个事件),那么就不需要数组。当然,您有几个事件,但当它们在循环中处理时(我假设),您一次只处理一个事件,然后转到下一个事件。
因此,您需要根据值$qutype
修改一个变量。为此,您可以使用switch
语句:
$dateObj = date_create("@$qucls");
switch($qutype) {
case "daily":
date_add($dateObj, date_interval_create_from_date_string("1 day"));
break;
case "weekly":
date_add($dateObj, date_interval_create_from_date_string("1 week"));
break;
case "monthly":
date_add($dateObj, date_interval_create_from_date_string("1 month"));
break;
case "yearly":
date_add($dateObj, date_interval_create_from_date_string("1 year"));
break;
}
$qucls = date_format($dateObj, "U");
我不加秒数,因为这会持续几天和几周,但不会持续几个月和几年,因为它们没有固定的秒数。
如果你对我上面使用的函数有疑问,你可以在php.net上查找它们的文档
在您显示的代码中,您不能使用mysql_fetch_array
该函数只适用于从调用mysql_query
得到的结果行,而不适用于普通数组。
你也不需要while循环。您所要做的就是将$qucls
格式化为可读格式,并生成最后一个字符串:
date_default_timezone_set('America/Denver');
$Time = date("H:i", $qucls);
$qufinal = sprintf("Event: %s 'nTime: %s 'nDesc: %s 'n'n", $quname, $Time, $qudesc);
编辑:
与评论中讨论的一样,这里是您在中编辑的修订和评论代码
$today = date("Ymd");
mysql_select_db($dbname);
// You need to use single quotes at the 'M'. Using double quotes will
// end the string and thus leading to incorrect syntax
$query1 = "SELECT cal_id, cal_name, cal_date, cal_time, cal_type, cal_description FROM webcal_entry WHERE cal_type = 'M' AND cal_date != " . $today;
$wequ1 = mysql_query($query1);
// This is a counter variable which is incremented in the loop
$i = 0;
// This is the outer while loop used to gather and store the events
while ($row = mysql_fetch_array($wequ1, MYSQL_NUM)) {
// Store the results in arrays
// Statements must be seperated by a ;
$quid1[$i] = $row[0];
$quname[$i] = $row[1];
$qutime[$i] = $row[2];
$qudesc[$i] = $row[3];
$query2 = "SELECT cal_id, cal_type, cal_ByDay FROM webcal_entry_repeats WHERE cal_id = " . $quid1[$i];
$wer1 = mysql_query($query2);
// Assuming that IDs are unique this query can only return one entry. Therefore no while is
// needed, but an if statement tests if the ID actually matched a result
if ($row = mysql_fetch_array($wer1, MYSQL_NUM)) {
//$quid2[$i] = $row[0]; <- the query above ensures that $quid1[$i] == $quid2[$i]. No need to store it again
$qutype[$i] = $row[1];
$qubdy[$i] = $row[2];
}
$query3 = "SELECT cal_id, cal_last_sent FROM webcal_reminders WHERE cal_id = " . $quid1[$i];
$wer2 = mysql_query($query3);
// Same as above; If the IDs are unique then you need no loop
if ($row = mysql_fetch_array($wer2, MYSQL_NUM)) {
// The $i++ used here is the short form. As this is the last time $i is
// used in the loop it needs to be increased before the next round. You can do
// this like this or in an extra statement. This way it's evaluated and then increased
$qucls[$i++] = $row[1];
}
// End outer while loop
}
// Now go through the results. $i holds the number of entries in the arrays + 1
// Secondary counter variable and for-loop
for ($j = 0; $j < $i; $j++) {
// Adding the dates to $qucls, formatting the string, ...
// Access them like above: $qucls[$j]
// Do not increase $j manually though - the for loop does that for you already
}
请注意,此代码未经测试。不过,它在语法上是正确的。
附带说明:您目前正在使用三个不同的数据库查询来收集数据
您可以使用SQL JOIN
s轻松地将它们合并到一个查询中。如果您希望有人向您展示如何做到这一点,您可以在单独的问题中展示它们,并要求将它们合并为一个查询。