猿问

函数不会在内部运行两次 - if else - 语句

我想做一个条形码查找。


第一次尝试时,会在用户条形码上本地查找条形码。


如果 是重复的,则该函数返回第一个实例,并带有文本“在本地找到”


如果在本地找不到条形码,则应进行全局搜索。


当在本地找不到条形码时,我遇到的问题是 if else 语句。


如果在本地找不到它,它应该检查是否在全局范围内找到它。


public function updatePricelist(Request $request)

    {


        $user_id = Auth::user()->id;


        $counter = count($_POST["product_name"]);


        //product barcode

        for ($x = 0; $x < $counter; $x++) {

            $product_barcode[] = $_POST["product_barcode"][$x];

        }


        //check the barcode locally

        $result = $this->barcodeLookUp($product_barcode, 'local');


        if (!empty($result)) {


           $result =  "found locally";

        }


        else {


            $result = $this->barcodeLookUp($product_barcode, 'global');


            if (!empty($result)) {


                $result =  "found globally";

            }

        }


        return $result;

    }


    public function barcodeLookUp($product_barcode, $type)

    {


        $user_id = Auth::user()->id;


if ($type === 'local') {


            $db_barcodes = $this->getBarcodes($user_id, 'local');

        }


        if ($type === 'global') {


            $db_barcodes = $this->getBarcodes($user_id, 'global');

        }


        //merge arrays

        $merge_array = array_merge($db_barcodes, $product_barcode);


        function array_dup($ar)

        {

            return array_unique(array_diff_assoc($ar, array_unique($ar)));

        }


        $result = array_dup($merge_array);


        return current($result);


    }


    public function getBarcodes($user_id, $type)

    {



        if ($type === 'local') {


            $result = DB::select('SELECT products FROM vendor_pricelist WHERE user_id = ?', [$user_id]);

        }


        if ($type === 'global') {


            $result = DB::select('SELECT products FROM vendor_pricelist WHERE user_id != ?', [$user_id]);

        }


        $count_results = count($result);


        $temp = [];

        $temp_2 = [];


        for ($x = 0; $x < $count_results; $x++) {


            array_push($temp, json_decode($result[$x]->products));

        }



海绵宝宝撒
浏览 166回答 2
2回答

绝地无双

这可能有帮助:&nbsp; &nbsp; //check the barcode locally&nbsp; &nbsp; $result = $this->barcodeLookUp($product_barcode, 'local');&nbsp; &nbsp; if (empty($result)) {&nbsp; &nbsp; &nbsp; &nbsp; $result = $this->barcodeLookUp($product_barcode, 'global');&nbsp; &nbsp; &nbsp; &nbsp; if (!empty($result)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "message" => "found globally",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "result"&nbsp; => $result,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ];&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "message" => "not found",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "result"&nbsp; => null,&nbsp; &nbsp; &nbsp; &nbsp; ];&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; return [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "message" => "found locally",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "result"&nbsp; => $result,&nbsp; &nbsp; &nbsp; &nbsp; ];&nbsp; &nbsp; }
随时随地看视频慕课网APP
我要回答