如何在php中的echo语句中使用三元运算符

问题描述 投票:0回答:2

这里我有一个简单的代码,它是使用 ajax 进行响应的。在操作页面中,我有选择框(数量)(9个选项)(size =“1”)。我的代码检查数据库产品数量,因此我想显示“选定”的数量(来自数据库)。我如何使用三元运算符
这是我的代码

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

?>

我不知道如何为此编写正确的代码。如果我的 $pro_qty 变量 == 1,我想在那里打印“selected”。 请帮忙

javascript php sql ajax ternary
2个回答
8
投票

您不能将三元数直接写入字符串中,您需要将其像变量一样插入到字符串中,如下所示:

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

请注意,我如何用

"
在三元数之前断开字符串,添加一个句点来连接三元数,在三元数的末尾以另一个句点结束,并再次以另一个
"
开始该字符串。

您可以在 PHP 手册中找到更多详细信息:字符串操作


替代品

将三进制与字符串分开

我通常使用大括号将字符串连接成其他字符串,如果你的echo语句被双引号包围(不是单引号),你可以使用这个方法

我会用与实际字符串分开的三元来写这个,如下所示:

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

或者你可以删除大括号,但我发现这可读性较差,并且更难判断变量在哪里。该字符串还必须用双引号引起来。

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

使用逗号

您也可以用逗号进行字符串分隔:

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

或者,

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

0
投票
       if($row['priority'] == '1'){
            $selected = 'Selected';
        } else {
            $selected = '';
        }


        echo "<td><select id='priority' name='priority'>";                   
            echo "<option value='0'>Low</option>";
            echo "<option value='1' ".$selected.">High</option>";
        echo "</select></td>";
© www.soinside.com 2019 - 2024. All rights reserved.