如果遇到非重复数值,如何调试[重复]

问题描述 投票:1回答:1

这个问题在这里已有答案:

我正进入(状态

警告:第37行的/home/zeefashcp/public_html/catalog/model/bossthemes/boss_tagcloud.php中遇到非数字值。

行:37 "$spread = $max_qty - $min_qty;"

<?php
class ModelBossthemesBossTagCloud extends Model {

    public function getRandomTags($limit = 5, $min_font_size = 9, $max_font_size = 25, $font_weight) {
        $product_id = array();
        $names          = array();
        $totals         = array();
        $tags           = array();
        $tagcloud       = false;

        $tagNameQuery = $this->db->query("SELECT DISTINCT tag FROM " . DB_PREFIX . "product_description WHERE language_id=" . (int)$this->config->get('config_language_id'));

        if (count($tagNameQuery->rows) > 0) {
            foreach ($tagNameQuery->rows as $row) {
                if($row['tag']){
                    $names = explode(',', $row['tag']);
                    $totals = array_merge($totals, $names);
                }
            }
            $tags = array_slice($totals, 0, $limit); 
            if($tags){
                $tagcloud = $this->generateTagCloud($tags, true, $min_font_size, $max_font_size, $font_weight);
            }
        }

        return $tagcloud;
    }

    private function generateTagCloud($tags, $resize = true, $min_font_size = 9, $max_font_size = 25, $font_weight) {

        if ($resize == true) {
            arsort($tags);

            $max_qty = max(array_values($tags));
            $min_qty = min(array_values($tags));

            $spread = $max_qty - $min_qty;

            if ($spread == 0) { $spread = 1; }

            $step = ((int)$max_font_size - (int)$min_font_size) / ($spread);

            $cloud = array();

            foreach ($tags as $key => $value) {
                $size=rand((int)$min_font_size,(int)$max_font_size);

                $cloud[] = '<a href="' . $this->url->link('product/search', 'tag=' . $value) . '" style="text-decoration:none;font-size:' . $size . 'px;font-weight:' . $font_weight . ';" title="">' . $value . '</a> ';
            }

        } else {

            foreach ($tags as $key => $value) {
                $cloud[] = '<a href="' . $this->url->link('product/search', 'tag=' . $value) . '" style="text-decoration:none;" title="">' . $value . '</a> ';
            }
        }

        $tagcloud = '';

        shuffle($cloud);

        for ($x = 0; $x < count($cloud); $x++) {
            $tagcloud .= $cloud[$x];
        }

        return $tagcloud;
    }
}
?>
php variables undefined opencart2.x
1个回答
0
投票

你的$ tags数组只包含字符串,可能至少有一个非数字字符串,所以函数min()和max()不返回整数而是返回一个字符串。

这就是为什么$spread = $max_qty - $min_qty;不起作用的原因。

以下是PHP文档的摘录(函数min()):

将以字母数字的形式比较多个非数字字符串值。返回的实际值将是原始类型,不应用任何转换。

PHP documentation

由于我不知道你想做什么,也不知道你的标签是什么样的,我无法帮助你。

© www.soinside.com 2019 - 2024. All rights reserved.