dongyuxin5353
2015-01-16 01:33 阅读 24
已采纳

如何使用php和sql应用array_diff

Hi im unable to apply array_diff() for when user edits the stockroom detail to replace with the new result.

Here is the edit function where i call out all the variables via sql.

 function einv_editStockrm($srid,$code,$name,$desc,$remark,$cat)
    {
        //connect to database
        base_connectDatabase();   

 $User = base_getUserDetail($_SESSION['uID']);
    $Stockroom = einv_getStockrmDetail($srid);
    base_executeSQL("UPDATE einv_stockroom
    SET einv_stockrm_code='" . $code . "',
    einv_stockrm_name='" . $name . "',
    einv_stockrm_desc='" . $desc . "',
    einv_stockrm_remark='" . $remark . "',
    einv_stockrm_cat = '" . $cat . "'
    WHERE einv_stockrm_id=" . $srid . "");

    base_addTransactionLog('Manage Stock Room', 'Edit',
    "
    Stock Room Code = " . $code . " ||
    Stock Room Name = " . $name . " ||
    Stock Room Description = " . $desc . " ||
    Stock Room Remark = " . $remark . " ||
    Stock Room Category = " . $cat . "
    ");

Here is the array_diff() function. There may be unnecessary calling and/or lack of coding..not too sure. This set of codings result in no output. The server basically crashes. Any help on this?

    //oldsr is existing stockroom data
    //newsr is new stockroom data through edit function
    //resultsr is the end result 
    <?php 
    $StockroomGetAllDetails = array();
    $arr2 = einv_getStockrmDetailFromCode($stockroomCode);
    $codearr = explode(",", $arr2['einv_stockrm_code']);

    $oldsr = array($codearr);
    $newsr = array($codearr);
    $resultsr = array_diff($newsr, $oldsr);

    if(!count(array_diff($oldsr, $newsr)) && !count(array_diff($newsr, $oldsr))) {
    // Arrays are equal
    } else {
    // Array values are different
        echo $resultsr;
    }
    ?> 

This code is for redirect.

        //go to stock room page
        echo '<script type="text/javascript">' . "
";
        echo 'window.location="../einventory/view_stockrm.php?id='. $srid .'";';
        echo '</script>';

    //close the database
    base_closeDatabase();
}
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

1条回答 默认 最新

  • 已采纳
    douke1954 douke1954 2015-01-16 06:47

    This set of codes able to display the new datas added and removed accordingly :)

    $oldCat = $Stockroom["einv_stockrm_cat"];
    
    $oldCatArr = explode(",",$oldCat);
    $newCatArr = explode(",",$cat);
    
    //Debugging
    //print_r($oldCatArr);
    //print_r($newCatArr);
    $resultCatAdd = array_diff($newCatArr, $oldCatArr);
    $resultCatRemove = array_diff($oldCatArr, $newCatArr);
    
    //Debugging
    print_r($resultCatAdd);
    print_r($resultCatRemove);
    
    点赞 评论 复制链接分享

相关推荐