duandui2803 2012-05-18 16:21
浏览 4

尝试使用php根据排名设置对象的购买价格

I am trying to price an object based on ranking. My problem is that if the object has no ranking it is going to the next tier. Here is a sample of my code:

switch ($amazonResult['SalesRank']) {
case ($amazonResult['SalesRank'] < 1 || trim($amazonResult['SalesRank'])===''|| !isset($amazonResult['SalesRank']) || $amazonResult['SalesRank']=== null):
    $Price=((float) $lowestAmazonPrice) *<some percent to pay>;
    $payPrice = round($Price, 0);  //to round the price up or down to the nearest $
    break; 
case ($amazonResult['SalesRank'] > 0 && $amazonResult['SalesRank'] <= 15000):
    $Price=((float) $lowestAmazonPrice) * <some percent to pay>;
    $payPrice = round($Price, 0);  //to round the price up or down to the nearest $
    break;
default:
    $Price=((float) $lowestAmazonPrice) * <some percent to pay>;
    $payPrice = round($Price, 0);  //to round the price up or down to the nearest $
    break;
}

How do I get it to find the ranking if the ranking is either empty, null, or 0?

$amazonResult['SalesRank'] could be empty, and is the value that needs to be compared in each case. This variable is pulled from a query and is run every time an item is to be priced

  • 写回答

1条回答 默认 最新

  • dounei9043 2012-05-18 16:27
    关注

    Try this:

    if(!isset($amazonResult['SalesRank']) || empty(trim($amazonResult['SalesRank'])) {
        // case if the variable is empty.
    } else if($amazonResult['SalesRank'] < 1) {
        // some "valid" value
        $Price=((float) $lowestAmazonPrice) *<some percent to pay>;
        $payPrice = round($Price, 0);  //to round the price up or down to the nearest $
        break;
    } else if($amazonResult['SalesRank'] > 0 && $amazonResult['SalesRank'] <= 15000) {
        $Price=((float) $lowestAmazonPrice) * <some percent to pay>;
        $payPrice = round($Price, 0);  //to round the price up or down to the nearest $
        break;
    } else {
        $Price=((float) $lowestAmazonPrice) * <some percent to pay>;
        $payPrice = round($Price, 0);  //to round the price up or down to the nearest $
        break;
    }
    

    The value after case should be constant so that you can compair that with the variable in switch.

    Your problem is that your case construct will return true or false which is may be not equal to $amazonResult['SalesRank'].

    评论

报告相同问题?

悬赏问题

  • ¥15 Python输入字符串转化为列表排序具体见图,严格按照输入
  • ¥20 XP系统在重新启动后进不去桌面,一直黑屏。
  • ¥15 opencv图像处理,需要四个处理结果图
  • ¥15 无线移动边缘计算系统中的系统模型
  • ¥15 深度学习中的画图问题
  • ¥15 java报错:使用mybatis plus查询一个只返回一条数据的sql,却报错返回了1000多条
  • ¥15 Python报错怎么解决
  • ¥15 simulink如何调用DLL文件
  • ¥15 关于用pyqt6的项目开发该怎么把前段后端和业务层分离
  • ¥30 线性代数的问题,我真的忘了线代的知识了