简体   繁体   English

你能检查我的 PHP 代码吗? (Opencart 扩展)

[英]Can you check my PHP code? (Opencart Extension)

I am using a plugin to get paid in opencart.我正在使用插件在 opencart 中获得报酬。 But I think there are some errors in PHP code.但我认为 PHP 代码中存在一些错误。

Example: PHP Warning: Invalid argument supplied for foreach()示例: PHP 警告:为 foreach() 提供的参数无效

<?php
***************************
***************************
***************************
    private $category_installment = array();
    private $category_full;
    public function __construct()
    {
        $this->hash = new Hash();
    }
***************************
***************************
***************************
    protected function categorySearch($category_id = 0)
    {
        if (!empty($this->category_full[$category_id]) and array_key_exists($this->category_full[$category_id], $this->category_installment)) {
            $return = $this->category_installment[$this->category_full[$category_id]];
        } else {
            foreach ($this->category_full as $id => $parent) {
                if ($category_id == $id) {
                    if ($parent == 0) {
                        $return = 0;
                    } elseif (array_key_exists($parent, $this->category_installment)) {
                        $return = $this->category_installment[$parent];
                    } else {
                        $return = $this->categorySearch($parent);
                    }
                } else {
                    $return = 0;
                }
            }
        }
        return $return;
    }
}

Can you help me find the errors in the code?你能帮我找出代码中的错误吗? (Opencart Extension) (Opencart 扩展)

Before loop check data type using: gettype();在循环检查数据类型之前使用:gettype();

<?php
***************************
***************************
***************************
    private $category_installment = array();
    private $category_full;
    public function __construct()
    {
        $this->hash = new Hash();
    }
***************************
***************************
***************************
    protected function categorySearch($category_id = 0)
    {
        if (!empty($this->category_full[$category_id]) and array_key_exists($this->category_full[$category_id], $this->category_installment)) {
            $return = $this->category_installment[$this->category_full[$category_id]];
        } else {
            if(!empty($this->category_full){  /* added this*/
                foreach ($this->category_full as $id => $parent) {
                    if ($category_id == $id) {
                        if ($parent == 0) {
                            $return = 0;
                        } elseif (array_key_exists($parent, $this->category_installment)) {
                            $return = $this->category_installment[$parent];
                        } else {
                            $return = $this->categorySearch($parent);
                        }
                    } else {
                        $return = 0;
                    }
                }
            } else {  /* added this*/
                $return = 0;/* added this*/
            } /* added this*/
        }
        
        return $return;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM