这里我有一个简单的代码,它是使用ajax的响应。在行动页面我有选择框(对于数量)(9个选项)(大小=" 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>
";
}
}
}
?>
我不知道如何为此编写正确的代码。我想打印&#34;选择&#34;如果我的$ pro_qty变量== 1。 请帮忙
答案 0 :(得分:5)
你不会直接在字符串中写入三元组,你需要将它插入字符串类似变量,如下所示:
echo "<option value='1' ".$qty == 1? 'selected' : ''." >1</option> ";
注意我是如何在使用"
的三元组之前打破字符串,添加一个句点来连接三元组,以三元组末尾的另一个句点结束,然后用另一个{{1}再次启动字符串}。
您可以在PHP手册中找到有关此内容的更多详细信息:String Operations
我通常使用花括号将字符串连接到其他字符串,如果你的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>";