简体   繁体   English

使用PHP从MySQL数据库中检索数据-时间管理

[英]Retrieving data from the MySQL database using PHP - time management

I need help on how to get/display some informations from DB (MySQl) 我需要有关如何从数据库(MySQl)获取/显示一些信息的帮助

My current table looks like this: 我当前的表如下所示:

id | title | level | start_afterID | days | 

1  |   A   |   0   |       0       |   7  |
2  |   B   |   1   |       1       |   5  |
3  |   C   |   1   |       2       |   3  |
4  |   D   |   1   |       3       |   2  |
5  |   E   |   1   |       4       |   2  |
6  |   F   |   1   |       3       |   6  |

Level '0' means that this job will start first and start day and time is in other table ( $config_start ) 级别“ 0”表示此作业将首先开始,并且开始的日期和时间在其他表中( $config_start

All others entry with level = 1 don't have fixed start, just id of job ( start_afterID ) after which they start. 所有其他级别= 1的条目都没有固定的开始,只是开始后的工作idstart_afterID )。

Also every job will last in number of days from DAYS field. 同样,每项工作将在“天”字段中持续天数。

In this table job with ID 6 (title F) must start after jobID 3 - And that start time is something like: 在此表中,ID为6(标题F)的任务必须在jobID 3之后开始-并且开始时间类似于:

$config_start + jobID 3 + jobID 2;

$end of this job is: 这项工作的$ end是:

$config_start + jobID 3 + jobID 2 + 6;

At the end, I will need to display all jobs from DB in incoming order. 最后,我将需要按传入顺序显示数据库中的所有作业。

I tried my best with this, but i can't figure how to check all parent levels and calculate it. 我尽力了,但是我不知道如何检查所有父级并进行计算。 Also, I can create new administration for this and maybe there is a better way of inserting new jobs ? 另外,我可以为此创建新的管理,也许有插入新作业的更好方法?

THANKS for any help on this, 感谢对此的任何帮助,

First of all, I advise you to rethink the level column - IIUC this is only a boolea, telling you wether this is a job root or not - this information is also prsent in start_afterID being 0. 首先,我建议您重新考虑level列-IIUC只是一个布尔值,告诉您是否是作业根目录-start_afterID为0时也显示此信息。

Now calculating the start and end times is a non-trivial job, that requires lots of DB avvess, so I advise you to create a second table with id|startday|endday and move the calculation logic in a way, that this (essentially cache) table is updated on job change, leaving you with a fast query path for normal operations. 现在,计算开始时间和结束时间是一项不平凡的工作,需要大量的数据库专家,因此我建议您创建一个具有id|startday|endday的第二张表,并以某种方式移动计算逻辑,该操作(基本上是缓存) )表会根据工作变动进行更新,从而为您提供了用于正常操作的快速查询路径。

To calculate the contents of this timing table, you would do something like 要计算此时间表的内容,您可以执行以下操作

function docalc($after,$startday) {

    $sql="SELECT * FROM tablename WHERE start_afterID=$after";

    //Run the query, this depends on your framework used

    //Pseudocode from here - again depends on your framework
    foreach ($row) {
        $id=$row['id'];
        $endday=$startday+$row['days']; //This will not be a simple addition, depending on your data types

        //Adapt the following to your data types
        $sql="REPLACE INTO timingcache SET id=$id, startday=$startday, endday=$endday";
        //Run the query - depends on your framework

        docalc($id,$endday);

    } //for each row

}


//this assumes $config_start is set correctly
docalc(0,$config_start);

If I understood you want to sort your data by level , start_afterID and days . 如果我了解您要按levelstart_afterIDdays对数据进行排序。

In that case you have to do a select: select * from table order by level asc, start_afterID asc, days desc; 在这种情况下,您必须进行选择: select * from table order by level asc, start_afterID asc, days desc;

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

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