0

ランキングに基づいてオブジェクトの価格を設定しようとしています。私の問題は、オブジェクトにランキングがない場合、次の層に移動することです。これが私のコードのサンプルです:

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

ランキングが空、null、または 0 の場合、ランキングを見つけるにはどうすればよいですか?

$amazonResult['SalesRank'] は空である可能性があり、その都度比較する必要がある値です。この変数はクエリから取得され、アイテムの価格が設定されるたびに実行されます

4

1 に答える 1

0

これを試して:

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

caseの変数と比較できるように、後の値は一定である必要がありますswitch

あなたの問題は、あなたの case 構造が true または false を返すこと$amazonResult['SalesRank']です。

于 2012-05-18T16:27:15.443 に答える