在线评级中的校正功能计算

时间:2014-09-13 10:19:04

标签: php

我需要你对这个php函数的帮助。

该函数从db获取数据,执行得分帐户并将其导出。在实践中,必须为买方和卖方($type)计算得分,但是当我去出口时,我只有一个买方。有问题的代码如下。在此先感谢您的帮助。

function shop_get_ratings($user_id, $type = 'seller'){

        $type = strtolower($type);
        $valid = array('seller','buyer');
        if( !in_array($type, $valid)){
            return false;
        }

        $conn = getConnection();
        $sql = 'SELECT AVG(i_%s_score) as %s_rating FROM %st_shop_transactions WHERE fk_i_user_id = %d AND i_%s_score IS NOT NULL';
        $rs = $conn->osc_dbFetchResults(sprintf($sql,$type,$type, DB_TABLE_PREFIX, $user_id, $type));


        $seller_r = 0;
        if( false !== $rs && isset($rs[0]['seller_rating']) && !is_null($rs[0]['seller_rating']) ){
            $seller_r = (int)$rs[0]['seller_rating'];
    }

        $sql = 'SELECT COUNT(*) as rating_count FROM %st_shop_transactions WHERE fk_i_user_id = %d AND i_%s_score IS NOT NULL';
        $rs = $conn->osc_dbFetchResults(sprintf($sql, DB_TABLE_PREFIX, $user_id, $type));

        $seller_r_c = 0;
        if( false !== $rs && isset($rs[0]['rating_count']) && !is_null($rs[0]['rating_count']) ){
            $seller_r_c = (int)$rs[0]['rating_count'];
        }


        $percentage = 0;
        if( $seller_r > 0 ){
            $percentage =($seller_r/5)*100;
        }


    $stats = array(
                'average_rating' => (int)$seller_r, 
                'rating_percentege' => (float)$percentage,
                'rating_count' => (int)$seller_r_c,

        );

    View::newInstance()->_exportVariableToView($type.'_ratings', $stats);
        return $stats;
    }

1 个答案:

答案 0 :(得分:0)

通过阅读代码,看起来你应该得到卖家的评分确定,但最终评分为0的买家。

这是因为在$sql = 'SELECT AVG(i_%s_score) as %s_rating行中您将$type插入到查询中,以使字段名为seller_typebuyer_type,具体取决于您评分的类型#39;重新尝试使用该功能。

但是,在查询结果集时,您明确要查找名为seller_rating的字段。 $typebuyer时,此字段不会设置,因此$seller_r将始终为0.

这里最简单的修复可能会将字段命名为sql中的avg_rating,而不依赖$type - var的名称注入。所以,像:

$sql = 'SELECT AVG(i_%s_score) as avg_rating
    FROM %st_shop_transactions
    WHERE fk_i_user_id = %d
        AND i_%s_score IS NOT NULL';
$rs = $conn->osc_dbFetchResults(
    sprintf($sql, $type, DB_TABLE_PREFIX, $user_id, $type)
);

$seller_r = 0;
if (false !== $rs
    && isset($rs[0]['avg_rating'])
    && !is_null($rs[0]['avg_rating'])
){
    $seller_r = (int)$rs[0]['avg_rating'];
}