首页 > 解决方案 > 函数不会在 - 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));
        }

        $x = 0;

        while ($x < $count_results) {

            foreach ($temp[$x] as $value) {

                array_push($temp_2, $value[2]);
            }

            $x++;
        }

        $result = $temp_2;

        return $result;
    }

标签: phplaravel

解决方案


希望你一切都好。对于您的 Http 响应,Laravel 尝试将所有响应转换为字符串或对象,并且似乎如果您的查找没有返回值,Laravel 将设置 $result = false(布尔值),这会给您一个错误。

我已经移动了一些代码以使事情变得更清楚。首先,我在你的 updatePricelist() 函数中添加了一个 else if 语句来进行最后的检查。如果查找不返回任何结果,这是将 $result 变量设置为字符串。

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

    if (!empty($result)) {

        $result = $result." found globally";
    }
    else {
        return "can save to db"; -> //this is to prevent the boolean response
    }

在这之后。我将您的 array_dup() 函数移到了barcodeLookUp() 函数之外。

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);


    $result = $this->array_dup($merge_array);

    return current($result);

}

public function array_dup($ar)
{
    return array_unique(array_diff_assoc($ar, array_unique($ar)));
}

推荐阅读