douzhan5262 2015-11-04 19:01
浏览 21
已采纳

PHP:某些时间/日期值未标记为冲突(但大多数其他值都是)

Summary:

I have an app with a slew of checkboxes on the page. The user selects whatever checkboxes they want, and hits submit.

I need to do some conflict checking.

My current state of the code is working great.. however I discovered a flaw in the logic it seems, that allows for certain 'selections' to pass through without being noted as a conflict. (but I'm not clear as to WHY they are slipping past, nor how to fix it)

Technical Overview:

I take an array of the submitted controls/elements, and loop through each one doing the following:

1.) checkStartFirst() = See what one starts first and arrange the order of the two 'times' as arguments sent to another function (isConflict())

2.) isConflict() = takes the arguments passed to it..and checks to see if the argumentX start time is greater than argumentY's end time. I then push these values into another array to be used later one.

3.) After the above loop is complete.. I have another function that clears out any dups in the array and passes it back where I need it to be (to be used in front end for highlighting said conflicts)

debugging output for 1 of the questionable selections that slips past and doesnt get marked as a conflict, (where other same date/time selection DO get flagged as a conflict)

Sending: hours_9_7_reg_session_101_945_1005 / hours_9_7_reg_session_102_945_1005

Received for checking: hours_9_7_reg_session_101_945_1005 / hours_9_7_reg_session_102_945_1005

B: CHECKING: hours_9_7_reg_session_102_945_1005 [end: 09/07/2015 10:05] > hours_9_7_reg_session_101_945_1005 [start: 09/07/2015 9:45]

Since it is in a loop, it gets checked again (other way)

Sending: hours_9_7_reg_session_102_925_945 / hours_9_7_reg_session_101_945_1005

Received for checking: hours_9_7_reg_session_102_925_945 / hours_9_7_reg_session_101_945_1005

A: CHECKING: hours_9_7_reg_session_102_925_945 [end: 09/07/2015 9:45] > hours_9_7_reg_session_101_945_1005 [start: 09/07/2015 9:45]

(again, slips past)

My PHP functions.. starts with calling setConflicts()

//new (proposed) session conflicts checker
function checkStartFirst($cf_presX, $cf_presY) {                         
    //$cf_presX['fullStart'] < $cf_presY['fullStart'] ? $this->isConflict($cf_presX, $cf_presY) : $this->isConflict($cf_presY, $cf_presX);

    echo 'Received for checking: '. $cf_presX['id'] . '  /  ' . $cf_presY['id'] .'<br>';
    if($cf_presX['fullStart'] < $cf_presY['fullStart']){
        echo 'A: ';
        $this->isConflict($cf_presX, $cf_presY);        
    }else{
        echo 'B: ';
        $this->isConflict($cf_presY, $cf_presX);        
    }   
}

function isConflict ($cc_presX, $cc_presY) {      
    echo 'CHECKING:  ' . $cc_presX['id'] .' [end: ' . $cc_presX['fullEnd'] . ']     >     ' . $cc_presY['id'] .'  [start: ' . $cc_presY['fullStart'] . ']';
    if ($cc_presX['fullEnd'] > $cc_presY['fullStart']) {    
        echo '  --   has conflict <br>';            
        array_push($this->conflict_output, $cc_presX['id']);
        array_push($this->conflict_output, $cc_presY['id']);
        //echo 'Found Conflict: ' . $cc_presX['id'] . '  /  ' . $cc_presY['id'] . '<br>';
    }else{
        //only here for debugging readability
        echo '<br>';
    }
}

function setConflicts() {
    $presentations = $this->conflict_list;  
    for ($i = 0; $i < count($presentations); $i++) {
        for ($j = 0; $j < count($presentations); $j++) {
            // if it is not the same event
            if ($presentations[$i]['id'] != $presentations[$j]['id']) {
                echo '<br><br>Sending: '.($presentations[$i]['id'] .'  /  '. $presentations[$j]['id']) .'<br>';
                $this->checkStartFirst($presentations[$i], $presentations[$j]);
            }else{
                echo '<br><br><br>same session, do not check: ' . ($presentations[$i]['id'] .'  /  '. $presentations[$j]['id']) . '<br><br><br>';
            }
        }
    } 
    $this->getConflicts();  
}

function getConflicts(){
    $presentations = $this->conflict_output;
    //remove duplicates in array & re-key (sequentially)
    $uniquePresentations = array_unique($presentations);
    //re-key array using sequential index #'s
    $uniquePresentations = array_values($uniquePresentations);
    if(count($uniquePresentations) > 0){
        //save conflict (names) to array
        $this->conflict_return = $uniquePresentations;
        $this->errmsg = 'Please review the form for errors, there are conflicts in the highlighted sessions below. (Possible duplicate or overlapping session times have been selected.) <br>You can not proceed until all conflicts are resolved.';    
    }       
}

How can the code/time selections in the blockquote above be slipping past? Where as others -do- get flagged?

I'm not sure if I need to tighten up some conditionals or something?

  • 写回答

1条回答 默认 最新

  • du6029076877 2015-11-05 20:19
    关注

    Well the solution (error) was easier than I expected.

    Basically anywhere I am checking the date/time stamps.. I need to make sure I am casting/converting them to a strtotme() value ... as pulling the value out of the object/array it is treated as a string.

    Why every other time conflict checking worked without issue.. I dont know. LOL

    But doing this fixed my problems:

    if(strtotime($cf_presX['fullStart']) < strtotime($cf_presY['fullStart'])){
    
    
    if (strtotime($cc_presX['fullEnd']) > strtotime($cc_presY['fullStart'])) {
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路
  • ¥15 经gamit解算的cors站数据再经globk网平差得到的坐标做形变分析
  • ¥15 GD32 SPI通信时我从机原样返回收到的数据怎么弄?
  • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错
  • ¥20 @microsoft/fetch-event-source 流式响应问题
  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题