[英]Making an array out of variables and update its contents based on one of the variables
我有一個問題與從日歷程序中的表中收集事件有關,在這些程序中它們被分為“事件”或“重復事件”。 我現在可以很好地獲得所有單個事件(感謝克里斯在此站點上),但是如果它們是重復事件,則必須根據此特定數據庫中給出的值進行計算。 如果更改數據庫中的類型或數據,則可能會浪費日歷,因此我必須使用已有的內容。
到目前為止,我整理出的變量是:
$quid2
=今天的事件的ID,這些事件被歸類為重復事件(之前需要) $quname
=重復的事件名稱 $qucls
=上次發送事件提醒的日期為UNIX時間,該日期為今天 $qutype
=這些單詞之一-每天,每周,每月或每年 $qudesc
=事件的描述 這些變量的項目數均相同,並且彼此之間正確排序。 (我希望)
以下是我要完成的邏輯。 無疑,這不是正確的語法,但我認為這是可以理解的; 我需要弄清楚什么是語法和形式。 我對此完全陌生...所以請保持溫柔...
它需要放在一個數組中(我認為)
$arr1 = some type of array($quname, $qucls, $qutype, $qudesc)
更新數組...
$qutype($row2)
=“ daily”,則+ 1440為$qucls($row[1])
$qutype($row2)
=“ weekly”,則$qucls($row[1])
$qutype($row2)
=“每月一次,那么它的$qucls($row[1])
+ 1個月 $qutype($row2)
=“ yearly”,則它的$qucls($row[1])
+ 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
格式化$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
將它們輕松合並到單個查詢中。 如果您希望有人向您展示如何做到這一點,則可以向他們展示一個單獨的問題,然后要求他們加入一個問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.