javascriptphpsqlajaxternary

How to use ternary operator in echo statement in php


here i have a simple code which is response using ajax . in action page i have select box(for qty)(9 options)(size="1") . my code checks in database product qty so i want to display the qty(from database) "selected" . how can i use ternary operator
Here is my code

if (isset($_POST['getcart'])){

    $cart_item_query = "SELECT * FROM cart";
    $cart_item_run = mysqli_query($conn,$cart_item_query);
    if (mysqli_num_rows($cart_item_run)>0){
        $grand_total=0;
        while($cart_row=mysqli_fetch_array($cart_item_run)){
            $pro_id = $cart_row['product_id'];
            $pro_qty = $cart_row['qty'];
            $product_query = "SELECT * FROM products WHERE product_id = '$pro_id'";
            $product_run = mysqli_query($conn, $product_query);
            $product_row = mysqli_fetch_array($product_run);
            $pro_name = $product_row['product_name'];
            $pro_image = $product_row['product_image'];
            $pro_sell_price = $product_row['sell_price'];
            $total = $pro_sell_price*$pro_qty;
            $grand_total =$grand_total+ $total;





            echo "<div class='row'>

                                    <div class='col-md-3 col-xs-3'><img src='img/$pro_image' alt='' width='50px'></div>
                                    <div class='col-md-3 col-xs-3'><p>$pro_name</p><a href=''><p><i class='fa fa-trash' aria-hidden='true'></i> Remove</p></a></div>
                                    <div class='col-md-2 col-xs-2'><select class='change_qty' size='1' style='width:50px;'>
                                     <option class='qty' data-pro='$pro_id' value='1' ".$pro_qty==1?'selected':''.">1</option>
                                    <option class='qty' data-pro='$pro_id' value='2' ".$pro_qty==2?'selected':''.">2</option>
                                    <option class='qty'  data-pro='$pro_id' value='3' ".$pro_qty==3?'selected':''.">3</option>
                                    <option class='qty'  data-pro='$pro_id' value='4' ".$pro_qty==4?'selected':''.">4</option>
                                    <option class='qty' data-pro='$pro_id' value='5' ".$pro_qty==5?'selected':''.">5</option>
                                    <option class='qty' data-pro='$pro_id' value='6' ".$pro_qty==6?'selected':''.">6</option>
                                    <option class='qty' data-pro='$pro_id' value='7' ".$pro_qty==7?'selected':''.">7</option>
                                    <option class='qty' data-pro='$pro_id' value='8' ".$pro_qty==8?'selected':''.">8</option>
                                    <option class='qty' data-pro='$pro_id' value='9' ".$pro_qty==9?'selected':''.">9</option>

                                        </select></div>

                                    <div class='col-md-2 col-xs-2'><input data-pro='$pro_id' id = 'price- $pro_id' type='text' value='$pro_sell_price' disabled class='form-control dis-input price' ></div>

                                    <div class='col-md-2 col-xs-2'><input id = 'total-$pro_id' data-pro='$pro_id' type='text' value='$total' disabled class='form-control dis-input total' ></div>

                                </div>

                                <hr>
                                ";
        }
    }
}

?>

I dont know how to write correct code for this. i want to print "selected" there if my $pro_qty variable == 1. please help


Solution

  • You don't write ternaries directly into the string, you need to insert it into the string sort of like a variable, like this:

    echo "<option value='1' ".$qty == 1? 'selected' : ''." >1</option> ";
    

    Notice how I broke the string with before the ternary with ", added a period to concatenate the ternary, ending with another period at the end of the ternary, and started the string again with another ".

    You can find more detail about this at the PHP manual: String Operations


    Alternatives

    Seperate ternary from string

    I usually use curly braces to concatenate strings into other strings, if your echo statement is surrounded by double quotes (not single quotes), you can use this method

    I would have written this with the ternary seperate from the actual string, like this:

    $selected = $qty == 1? 'selected' : '';
    echo "<option value='1' {$selected}>1</option>";
    

    OR you could drop the curly brackets, but I find this less readable, and harder to tell where variables are. The string also must be wrapped in double quotes.

    $selected = $qty == 1? 'selected' : '';
    echo "<option value='1' $selected>1</option>";
    

    Using Commas

    You can also do string separation by commas:

    $selected = $qty == 1? 'selected' : '';
    echo "<option value='1'", $selected, ">1</option>";
    

    Alternatively,

    echo "<option value='1'", $qty == 1? 'selected' : '', ">1</option>";