我有下面的PHP文件,它工作得很好,所有的输出都显示出来了,但是我有IF语句的问题。我不知道为什么它显示了IF语句的所有文本和两行星号(字体很棒的图标).Why它发生了,有人能这么好地帮助我吗,我必须把IF放在哪里才能工作,?谢谢
<?php
include('database_connection.php');
if(isset($_POST["action"]))
{
$query = "
SELECT * FROM items WHERE product_status = '1'
";
if(isset($_POST["brands"]))
{
$brand_filter = implode("','", $_POST["brands"]);
$query .= " AND product BETWEEN '400' AND '600' ";
}
$statement = $connect->prepare($query);
$statement->execute();
$result = $statement->fetchAll();
$total_row = $statement->rowCount();
$output = '';
if($total_row > 0)
{
foreach($result as $row)
{
$output .= '
<div class="col-sm-4 col-lg-3 col-md-3">
<div style="border:1px solid #ccc; border-radius:5px; padding:16px; margin-bottom:16px; height:450px;">
<p align="center"><strong><a href="#">'. $row['hotel_name'] .'</a></strong></p>
<h4 style="text-align:center;" class="text-danger" >'. $row['product_price'] .'</h4>
if($row["rating"]==1)
{
<span class="fa fa-star checked"></span>
<span class="fa fa-star"></span>
<span class="fa fa-star"></span>
<span class="fa fa-star"></span>
<span class="fa fa-star"></span>
}
if($row["rating"]==2)
{
<span class="fa fa-star checked"></span>
<span class="fa fa-star checked"></span>
<span class="fa fa-star"></span>
<span class="fa fa-star"></span>
<span class="fa fa-star"></span>
}
</div>
</div>
';
}
}
else
{
$output = '<h3>No Data Found</h3>';
}
echo $output;
}
?>发布于 2021-04-18 00:32:05
if语句在字符串中,因此它不会作为if条件执行。您需要断开字符串并追加if条件值,然后回显最终的$outputinclude('database_connection.php');
if (isset($_POST["action"])) {
// db query
$query = "SELECT * FROM items WHERE product_status = '1' ";
if (isset($_POST["brands"])) {
$brand_filter = implode("','", $_POST["brands"]);
$query .= " AND product BETWEEN '400' AND '600' ";
}
$statement = $connect->prepare($query);
$statement->execute();
$result = $statement->fetchAll();
$total_row = $statement->rowCount();
$output = '';
if ($total_row > 0) {
foreach ($result as $row) {
$rating_element = '';
if ($row["rating"] == 1) {
$rating_element = '<span class="fa fa-star checked"></span>
<span class="fa fa-star"></span>
<span class="fa fa-star"></span>
<span class="fa fa-star"></span>
<span class="fa fa-star"></span> ';
}
if ($row["rating"] == 2) {
$rating_element = '<span class="fa fa-star checked" ></span >
<span class="fa fa-star checked" ></span >
<span class="fa fa-star" ></span >
<span class="fa fa-star" ></span >
<span class="fa fa-star" ></span >';
}
$output .= '<div class="col-sm-4 col-lg-3 col-md-3">
<div style="border:1px solid #ccc; border-radius:5px; padding:16px; margin-bottom:16px; height:450px;">
<p align="center"><strong><a href="#">' . $row['hotel_name'] . '</a></strong></p>
<h4 style="text-align:center;" class="text-danger" >' . $row['product_price'] . '</h4>
' . $rating_element . '
</div>
</div>
';
}
} else {
$output = '<h3>No Data Found</h3>';
}
echo $output;
}https://stackoverflow.com/questions/67140335
复制相似问题