比较CakePHP中的两个多维数组 - 努力使逻辑正确

时间:2011-12-22 10:15:40

标签: php arrays cakephp

方案

我正在开发一个内部 CakePHP 公司应用程序,它有一个发货屏幕,有三个列表:

  1. 发货的可用商品(左下方显示 - "可供包装的商品")
  2. 已经从数据库中提取的已打包物品(右下方显示 - "已包装物品")
  3. 对打包物品清单的更改(更改后的表格提交)
  4. 用户可以将项目从左侧列表拖动到右侧列表,反之亦然。单击“保存”按钮后,表单将与右侧列表中的项目一起提交。

    Original state of lists

    期望结果

    作为保存的一部分,我需要将更新后的列表(使用 PHP )与数据库中保存的列表进行比较。

    如果有任何添加,我需要使用发货ID更新商品记录。

    Additions - from left to right

    对于任何删除,我需要更新商品记录以将发货ID卖给NULL。

    Removals - from right to left

    这将允许用户在两​​个方向上拖放项目,然后单击“保存”并期望页面加载他们离开页面的状态。

    当前状态

    我可以保存要添加到列表中的项目,并单独保存要删除的项目。但是,我不能让他们一起工作。当我尝试这个时,添加到列表中的一个项目将完全替换" Packed Items"上的所有其他项目。名单。

    到目前为止的代码......

    function edit() {       
     $this->Despatch->recursive = -1; 
     if (!empty($this->data)) { 
      if($this->Despatch->save($this->data)) {
       /* NEED TO RETRIEVE CURRENT REPAIR IDs ASSOCIATED 
       WITH THE DESPATCH ID AND COMPARE WITH FORM SUBMISSION LIST TO 
       REMOVE THE DIFFERENCES */
       # Retrieve current repair records where despatch_id = $this->id.
       $this->Despatch->Repair->recursive = -1;
       $current_associated_repairs = $this->Despatch->Repair->find('all', array(
        'fields' => array('id', 'despatch_date', 'despatch_id'),
        'conditions' => array('Repair.despatch_id =' => $this->data['Despatch']['id'])));
       # Create array with repair IDs
       $repairs = explode(",", $this->data['Repair']['ids']);
       $i = 0;
       foreach($repairs as $repair) {
        $repairupdates[$i]['Repair']['id'] = $repair;
        $i++;
       }
       # Delete array index to prevent it interfering with form saving later
       unset($this->data['Repair']['ids']);
    
       # 2. Find unmatched IDs between current records and form submission list of repairs
       $length1 = sizeof($current_associated_repairs);
       $length2 = sizeof($this->data['Repair']);
    
       for ($i = 0; $i < $length1; $i++) { 
        for ($j = 0; $j < $length2; $j++) {
     ### LOGIC NOT CORRECT HERE !!! ###
     ### THIS WORKS FOR REMOVING PACKED INSTRUMENTS - FROM RIGHT TO LEFT ###
     ### BUT ACTUALLY BEING TRIGGERED WHEN ADDING FROM LEFT TO RIGHT TOO ###
         if ($current_associated_repairs[$i]['Repair']['id'] == $this->data['Repair'][$j]['id'] 
         && !in_array($current_associated_repairs[$i]['Repair']['id'], $this->data['Repair'][$j])) {
         # if it's in current repairs and not in form submission, set to null...
         # otherwise, it must be an addition if there's no matches
         # 3. Set the despatch_date and despatch_id fields of those records to NULL
         $this->data['Repair'][$j+1]['id'] = $current_associated_repairs[$i]['Repair']['id'];
         $this->data['Repair'][$j+1]['despatch_date'] = null;
         $this->data['Repair'][$j+1]['despatch_id'] = null;
     }
        }
       }
       # 4. Save the new list of repairs to the current despatch
       if($this->Despatch->save($this->data) && $this->Despatch->Repair->saveAll($this->data['Repair'])) {
        $this->Session->setFlash(__('The despatch has been saved.', true), 'default', array('class' => 'success-scheme message'));
       } else {
        $this->Session->setFlash(__('The repair could not be updated with the despatch number. Please try again.', true), 'default', array('class' => 'error-scheme message'));
       }
      } else {
       $this->Session->setFlash(__('The despatch could not be saved. Please try again.', true), 'default', array('class' => 'error-scheme message'));       
      }
     $this->redirect($this->referer(array('action' => 'view', $this->data['Despatch']['id'])));
     }
    }
    

    我认为我已经包含了所有人应该需要的所有细节,但只是询问您是否需要了解更多信息。我遇到的主要问题是理解实现此目的的代码逻辑。

    非常感谢提前。

    PS - JavaScript代码

    // click handler for saving despatch - required to save sortable lists properly
    $('#save_despatch').click(function(e) {
        e.preventDefault();
        // gives comma-separated array of instrument IDs, e.g. 12345, 56789...
        var result = $('#sortable2').sortable('toArray');
        $('#RepairIds').val(result);
        $('form').submit();
    });
    

1 个答案:

答案 0 :(得分:2)

我无法在你的逻辑中弄清楚你现在在做什么,但我会建议一种毫无疑问应该有用的方法。

为简单起见,您有两个列表:左侧和右侧。这段代码是一种伪代码,因为我不完全理解你需要什么,但是这样,列表应该保持更新,并且不能出错。

显示列表之前的逻辑(在PHP中):

//Retrieve records from mysql into two arrays->aLeft and aRight
//Create an MD5 hash to easily compare current en future lists.
$sCheckSum = md5(implode(",",$aLeft));
$_SESSION['checkSum'] = $sCheckSum;

//Generate your lists now 
//Display output

JS:

$('#save_despatch').click(function(e) {
    e.preventDefault();
    // gives comma-separated array of instrument IDs, e.g. 12345, 56789...
    var leftResult = $('#sortable2').sortable('toArray');
    var rightResult = $('#sortableX').sortable('toArray'); //Make sure this matches your lists, as I have not seen your HTML
    $('#Left').val(leftResult);
    $('#Right').val(rightResult);
    $('form').submit();
});

然后在接收端:

//Get the values of the #Left and #Right inputs in to $aLeft and $aRight
$sNewCheckSum = md5($aLeft);
if($sNewCheckSum  != $_SESSION['checkSum'] ) //Left list is not the same, so the right is neither
{
   foreach(explode(",",$aLeft) as $iRepairId){
      //Not sure what should happen here, but thats up to you.
      //Mysql update table SET  ID = NULL WHERE repairdId = $iRepairId
   }
   foreach(explode(",",$aRight) as $iRepairId){
      //Not sure what should happen here, but thats up to you.
      //Mysql update table SET  ID = xxx WHERE repairdId = $iRepairId
   }

}
相关问题