user9277271
user9277271

Reputation:

How to write these `if..else` conditional statements with Ternary Operators

I want to write these if..else statements with ternary operators:

if((($request->city)=="98")&&($cartPrice >= 300000)&&($request->province)=="8")){
    "ort_free_delivery" => 'City sending free delivery'
}elseif((($request->city)!="98")&&($cartPrice >=300000)&&($request->province)!="8")){
    "ort_free_delivery" => 'Country sending free delivery'
}else{
    "ort_free_delivery" => '',
}

And here is my try:

( ( ($request->city)=="98" ) ? ($cartPrice>=300000) ? ( ( ($request->province)=="8" ) ? "ort_free_delivery" == "City sending free delivery" ) ); 

( ( ($request->city)!="98" ) ? ($cartPrice>=300000) ? ( ( ($request->province)!="8" ) ? "ort_free_delivery" == "Country sending free delivery" : "ort_free_delivery" == "" ) ); 

But this is wrong because there are two lines of ternary operators, and I need this condition in one line (because I'm inserting some data with DB class in Laravel):

    DB::table('order_detail')->insert([
        "ort_ord_id" => $orderId,
        "ort_amount" => $value->price,
        "ort_total" => $value->discounted * $value->quantity,
        "ort_discount" => ($value->price * $value->quantity) - $value->discounted * $value->quantity,
        "ort_type" => "product",
        "ort_number" => $value->quantity,
        "ort_reference_id" => $value->id,
        ( ( ($request->city)=="98" ) ? ($cartPrice>=300000) ? ( ( ($request->province)=="8" ) ? "ort_free_delivery" == "City sending free delivery" ) )
        ( ( ($request->city)!="98" ) ? ($cartPrice>=300000) ? ( ( ($request->province)!="8" ) ? "ort_free_delivery" == "Country sending free delivery" : "ort_free_delivery" == "" ) )
        "created_at" => now(),
        "updated_at" => now()
    ]);

So my question is, how can I add this conditional statement with ternary operators in one line?

Upvotes: 2

Views: 308

Answers (4)

Jorel
Jorel

Reputation: 11

"ort_free_delivery" => ($cartPrice >= 300000 ? ( $request->city=="98" && $request->province=="8" ? 'City sending free delivery' : 'Country sending free delivery' ) : '')

Human read code:

If cartPrice is lower 300000 is always ''

else if both city is 98 and province is 8 at same time (AND)...

is "city sending.."

else

is "Country sending.."

Because:

the main distinguish rule seems that both city=98 and province=8 must be True to be 'city sending' ...

... so all other cases is 'country sending'...

... when cartprice is >= 300000

Upvotes: 0

Pedro Rodrigues
Pedro Rodrigues

Reputation: 707

Using var_dump for testing, instead of what you have, this is what I came up with:

(((($request->city=="98")&&($cartPrice>=300000)&&($request->province=="8"))?var_dump('1'):((($request->city!="98")&&($cartPrice>=300000)&&($request->province=="8"))?var_dump('2'):var_dump('3'))))

In a more readable way:

(
    (
        (
            ($request->city == "98") && 
            ($cartPrice >= 300000) && 
            ($request->province == "8")
        )
        ? var_dump('1') 
        : (
            (
                ($request->city != "98") && 
                ($cartPrice >= 300000) && 
                ($request->province == "8") 
            )
            ? var_dump('2')
            : var_dump('3')
          )
    )
)

Upvotes: 1

John Lobo
John Lobo

Reputation: 15319

One option is build array and then insert

$data=[
        "ort_ord_id" => $orderId,
        "ort_amount" => $value->price,
        "ort_total" => $value->discounted * $value->quantity,
        "ort_discount" => ($value->price * $value->quantity) - $value->discounted * $value->quantity,
        "ort_type" => "product",
        "ort_number" => $value->quantity,
        "ort_reference_id" => $value->id,
        "created_at" => now(),
        "updated_at" => now()
    ];  

For conditional data

if($request->city==98&&$cartPrice >= 300000&&$request->province==8){
    $data["ort_free_delivery"] = 'City sending free delivery';
}elseif($request->city!=98&&$cartPrice >=300000&&$request->province!=8){
     $data["ort_free_delivery"] = 'Country sending free delivery';
}else{
    $data["ort_free_delivery"] = '';
}

Then query

  DB::table('order_detail')->insert($data);

or

DB::table('order_detail')->insert([
    "ort_ord_id" => $orderId,
    "ort_amount" => $value->price,
    "ort_total" => $value->discounted * $value->quantity,
    "ort_discount" => ($value->price * $value->quantity) - $value->discounted * $value->quantity,
    "ort_type" => "product",
    "ort_number" => $value->quantity,
    "ort_reference_id" => $value->id,
    "ort_free_delivery" =>($request->city==98&&$cartPrice >= 300000&&$request->province==8)?'City sending free delivery':
        (($request->city!=98&&$cartPrice >=300000&&$request->province!=8)?'Country sending free delivery':null),
    "created_at" => now(),
    "updated_at" => now()
]);

Upvotes: 1

auspicious99
auspicious99

Reputation: 4321

You can try this. The idea is to replace the statement after the first : with a second ternary. The second ternary gets evaluated only if the first condition is false.

    DB::table('order_detail')->insert([
        "ort_ord_id" => $orderId,
        "ort_amount" => $value->price,
        "ort_total" => $value->discounted * $value->quantity,
        "ort_discount" => ($value->price * $value->quantity) - $value->discounted * $value->quantity,
        "ort_type" => "product",
        "ort_number" => $value->quantity,
        "ort_reference_id" => $value->id,
        "ort_free_delivery" => ( ( ($request->city)=="98" ) &&  ($cartPrice>=300000) && ( ($request->province)=="8" ) ) ? "City sending free delivery" : ( ( ($request->city)!="98" ) && ($cartPrice>=300000) && ( ($request->province)!="8" ) ) ? "Country sending free delivery" : "" ) ),
        "created_at" => now(),
        "updated_at" => now()
    ]);

Upvotes: 1

Related Questions