admin管理员组文章数量:1399796
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
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
Share Improve this question edited Aug 23, 2017 at 18:09 Rakesh K asked Aug 23, 2017 at 16:51 Rakesh KRakesh K 1,3301 gold badge18 silver badges44 bronze badges 23- You don't write a ternary directly into the string – GrumpyCrouton Commented Aug 23, 2017 at 16:53
- @GrumpyCrouton Sir,then how can i solve this? – Rakesh K Commented Aug 23, 2017 at 16:54
-
Why did your variable switch from $qty to $pro_qty? What does
$pro_qty
actually equal? Try to echo it before that div – GrumpyCrouton Commented Aug 23, 2017 at 17:53 - no. i am fetching qty from database and saving it in $pro_qty variable – Rakesh K Commented Aug 23, 2017 at 17:57
-
I still need to know what
$pro_qty
equals. – GrumpyCrouton Commented Aug 23, 2017 at 18:03
2 Answers
Reset to default 8You 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 mas:
$selected = $qty == 1? 'selected' : '';
echo "<option value='1'", $selected, ">1</option>";
Alternatively,
echo "<option value='1'", $qty == 1? 'selected' : '', ">1</option>";
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>";
本文标签: javascriptHow to use ternary operator in echo statement in phpStack Overflow
版权声明:本文标题:javascript - How to use ternary operator in echo statement in php - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744130785a2592170.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论