在PHP文件中添加IF语句

在PHP文件中添加IF语句,php,html,sql,Php,Html,Sql,我有下面的PHP文件,它运行良好,所有输出都显示出来,但我对IF语句有问题。我不知道为什么它会显示IF语句和两行星星(字体很棒的图标)的所有文本。为什么会发生这种情况,有人能这么好心帮助我,我必须把IF放在哪里才能使IF工作,谢谢 <?php include('database_connection.php'); if(isset($_POST["action"])) { $query = " SELECT * FROM items WHERE pro

我有下面的PHP文件,它运行良好,所有输出都显示出来,但我对IF语句有问题。我不知道为什么它会显示IF语句和两行星星(字体很棒的图标)的所有文本。为什么会发生这种情况,有人能这么好心帮助我,我必须把IF放在哪里才能使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;
}

?>

if语句位于字符串内部,因此它不会作为if条件执行。您需要断开字符串并附加
if
条件值,并回显最终的
$output
include('database_connection.php')

if(isset($\u POST[“action”])){
//数据库查询
$query=“从产品状态为“1”的项目中选择*;
如果(isset($_POST[“brands”])){
$brand_filter=内爆(“,”,$_POST[“brands”]);
$query.=“和介于'400'和'600'之间的产品”;
}
$statement=$connect->prepare($query);
$statement->execute();
$result=$statement->fetchAll();
$total_row=$statement->rowCount();
$output='';
如果($total_row>0){
foreach($结果为$行){
$rating_element='';
如果($row[“rating”]==1){
$rating_元素=
';
}
如果($row[“rating”]==2){
$rating_元素=
';
}
$output.='

“.$row[“产品价格”]” “.$rating_元素” '; } }否则{ $output='未找到数据'; } echo$输出; }
这和你一小时前问的问题相同吗()。你有没有发现重复标记,如果有,你还有什么问题。这是否回答了你的问题?
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;
}