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

?>
2

1 Answer 1

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;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.