0

i have following PHP file,it work well,all output is showing up,but i have problem with IF statement.i dont know why its showing up with all text of both IF statement and both row of stars (font awesome icons).Why is it happening,can someone be so kind to help me,where do i have to put IF to make if work,???thanks

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

?>
andrewpuni
  • 33
  • 5
  • Is this the same question as you asked an hour ago(https://stackoverflow.com/questions/67139305/add-if-statement-to-php). Did you get anywhere with the duplicate flagged and if so what problems do you still have. – Nigel Ren Apr 17 '21 at 16:26
  • 2
    Does this answer your question? [add IF statement to PHP](https://stackoverflow.com/questions/67139305/add-if-statement-to-php) – Maëlan Apr 17 '21 at 19:35

1 Answers1

1

The if statement is inside the string so it will not execute as a if condition. you need to break the string and append the if condition value and echo the final $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;
}