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???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
  • You cannot have an if in a concatenation statement. Also DRY (Don't repeat yourself) – mplungjan Apr 17 '21 at 14:37
  • Also you may want to look at https://stackoverflow.com/questions/10250825/converting-numbers-to-visual-rating-stars rather than having multiple ifs. – Nigel Ren Apr 17 '21 at 14:38
  • Where can i put IF statement? – andrewpuni Apr 17 '21 at 14:39
  • might want to use a function, also utilise half-stars etc https://3v4l.org/ktSko - also `$brand_filter` suspiciously looks like its open to sql injection, if it were used – Lawrence Cherone Apr 17 '21 at 15:08
  • can you help me and show how my code should looks like? i dont know much about PHP,sorry. – andrewpuni Apr 17 '21 at 15:14
  • learn about [strings](https://www.php.net/manual/en/language.types.string.php), you cant just place PHP code in a string and it executes, has been mentioned in the first comment, also if you're going to build and do a bunch of concatenation, just to echo at the end you might as well break out of php, in the loop https://3v4l.org/45DFF also avoid inline css and `align="center"`, use css style `text-align:center` instead, or as its bootstrap `class="text-center"` – Lawrence Cherone Apr 17 '21 at 15:25
  • i dont understand anything what you mean...i just trying to resolve problem with IF...but i think its too complicated – andrewpuni Apr 17 '21 at 15:31

0 Answers0