简体   繁体   English

根据变量之一创建数组并根据变量之一更新其内容

[英]Making an array out of variables and update its contents based on one of the variables

I have a question that pertains to gathering events from tables in a calendar program where they are separated into "events" or "repeated events". 我有一个问题与从日历程序中的表中收集事件有关,在这些程序中它们被分为“事件”或“重复事件”。 I can get all individual events perfectly well now (thanks to Chris on this site), but if they are repeating events I have to calculate it from what is given in this particular db. 我现在可以很好地获得所有单个事件(感谢克里斯在此站点上),但是如果它们是重复事件,则必须根据此特定数据库中给出的值进行计算。 If I change the types or the data in the db, it will probably trash the calendar so I have to use what I have. 如果更改数据库中的类型或数据,则可能会浪费日历,因此我必须使用已有的内容。

The variables I have sorted out so far are: 到目前为止,我整理出的变量是:

  • $quid2 = The IDs for today's events that are classified as repeating events (needed earlier) $quid2 =今天的事件的ID,这些事件被归类为重复事件(之前需要)
  • $quname = The repeated event names $quname =重复的事件名称
  • $qucls = The date UNIX time for the last sent reminder of events dated today $qucls =上次发送事件提醒的日期为UNIX时间,该日期为今天
  • $qutype = One of these words - daily, weekly, monthly or yearly $qutype =这些单词之一-每天,每周,每月或每年
  • $qudesc = A description of the event $qudesc =事件的描述

These variables all have the same number of items and are ordered correctly between each other. 这些变量的项目数均相同,并且彼此之间正确排序。 (I Hope) (我希望)

Below is the logic I am trying to accomplish. 以下是我要完成的逻辑。 It are most assuredly not proper syntax but I think it is understandable; 无疑,这不是正确的语法,但我认为这是可以理解的; I need to figure out what the syntax and form is. 我需要弄清楚什么是语法和形式。 I am utterly and completely new at this... so please be gentle... 我对此完全陌生...所以请保持温柔...

It needs to be put in an array (I think) 它需要放在一个数组中(我认为)

$arr1 = some type of array($quname, $qucls, $qutype, $qudesc)

update the array... 更新数组...

  • IF $qutype($row2) = "daily", then + 1440 to it's $qucls($row[1]) 如果$qutype($row2) =“ daily”,则+ 1440为$qucls($row[1])
  • IF $qutype($row2) = "weekly", then + 10080 to it's $qucls($row[1]) 如果$qutype($row2) =“ weekly”,则$qucls($row[1])
  • IF $qutype($row2) = "monthly, then + 1 month to it's $qucls($row[1]) 如果$qutype($row2) =“每月一次,那么它的$qucls($row[1]) + 1个月
  • IF $qutype($row2) = "yearly", then + 1 year to it's $qucls($row[1]) 如果$qutype($row2) =“ yearly”,则它的$qucls($row[1]) + 1年

Then final product... 然后是最终产品

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);
}

...

This is a big learning project for me. 对我来说,这是一个很大的学习项目。 I don't know enough PHP and mysql to make this work but I know just enough to get me in trouble. 我不了解足够的PHP和mysql来完成这项工作,但是我知道的足以使我遇到麻烦。 Thanks! 谢谢!

EDIT: adding the queries from which I made these variables: 编辑:添加查询,我从中进行这些变量:

$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];
}

The syntax for arrays is as follows: 数组的语法如下:

$arrayName = array($quname, $qucls, $qutype, $qudesc);

Then you can access the values by their index on the array variable: 然后,您可以通过它们在数组变量上的索引来访问值:

$arrayName[0] == $quname
$arrayName[1] == $qucls
...

You can also define it as associative array: 您也可以将其定义为关联数组:

$arrayName = array(
  "quname" => $quname, 
  "qucls" => $qucls, 
  "qutype" => $qutype, 
  "qudesc" => $qudesc
);

Using this syntax you can access the elements by their name: 使用此语法,您可以按元素名称访问元素:

$arrayName["quname"] == $quname
$arrayName["qucls"] == $qucls
...

More reading on this: Arrays 有关此内容的更多阅读: 数组

However, you don't really need an array for what you plan to do here. 但是,您实际上不需要计划在这里进行的操作。 Arrays are very useful if you want to store data that is structurally equal. 如果要存储结构上相等的数据,则数组非常有用。 This applies eg to rows in a database: They always have the same number of entries, and the columns are of the same type. 例如,这适用于数据库中的行:它们始终具有相同数量的条目,并且列具有相同的类型。

If you have just one dataset at that point of code (one event in this case), then you need no array. 如果在该代码点只有一个数据集(在这种情况下为一个事件),则不需要数组。 Of course you have several events, but as they are processed in a loop (I assume) you handle only one event at a time, and then head to the next. 当然,您有几个事件,但是当它们在循环中处理时(我假设),您一次只处理一个事件,然后转到下一个事件。

So, you want to modify a variable depending on the value $qutype . 因此,您想根据值$qutype修改变量。 To do that, you can use a switch statement : 为此,您可以使用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");

I don't add the number of seconds, because that would work for days and weeks- but not for months and years, as they don't have a fixed number of seconds. 我没有添加秒数,因为它可以工作数天和数周,但不能工作数月和数年,因为它们没有固定的秒数。

If you have questions about the functions I used above you can look up their documentation on php.net . 如果您对我上面使用的功能有疑问,可以在php.net上查找其文档。

In the code you show you must not use mysql_fetch_array . 在显示的代码中,您不得使用mysql_fetch_array
That function is only meant for result rows you got from a call to mysql_query , but not for normal arrays. 该函数仅用于通过调用mysql_query获得的结果行,不适用于普通数组。

You don't need the while loop either. 您也不需要while循环。 All you have to do is formatting $qucls to a readable format and produce the final string: 您要做的就是将$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);

Edit: 编辑:

Like discussed in the comments here is the revised and commented code you edited in: 就像评论中讨论的一样,这里是您在其中修改和评论的代码:

$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

}

Please note that this code is untested. 请注意,此代码未经测试。 It's syntactically correct though. 从语法上讲,这是正确的。

On a side note: You are currently using three different database queries to gather the data. 附带说明:您当前正在使用三个不同的数据库查询来收集数据。
You can easily merge them into a single query using SQL JOIN s. 您可以使用SQL JOIN将它们轻松合并到单个查询中。 If you want somebody to show you how to do that, you can show them in a seperate question and ask for them to be joined into one. 如果您希望有人向您展示如何做到这一点,则可以向他们展示一个单独的问题,然后要求他们加入一个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM