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'])) {
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 L76k模块的GPS的使用
  • ¥15 请帮我看一看数电项目如何设计
  • ¥23 (标签-bug|关键词-密码错误加密)
  • ¥66 比特币地址如何生成taproot地址
  • ¥20 数学建模数学建模需要
  • ¥15 关于#lua#的问题,请各位专家解答!
  • ¥15 什么设备可以研究OFDM的60GHz毫米波信道模型
  • ¥15 不知道是该怎么引用多个函数片段
  • ¥30 关于用python写支付宝扫码付异步通知收不到的问题
  • ¥15 隐藏系统界面pdf的打印、下载按钮