3

I am struggling again with my PHP structures and you guys gave me a lots of help. Thanks in advance. Now I got a new challenge to myself. I have the following table im my database:

  id |     htl_name    |      city_zone     |   price    |   given_date
------------------------------------------------------------------------
  1  | Hotel Beach Inn |   Cityzone1        |  10.00     |   2012-09-01
  2  | Hotel Beach Inn |   Cityzone1        |  10.00     |   2012-09-02
  3  | Hotel Beach Inn |   Cityzone1        |  10.00     |   2012-09-03
  4  | Hotel Beach Inn |   Cityzone1        |  11.00     |   2012-09-04
  5  | Hotel Beach Inn |   Cityzone1        |  11.00     |   2012-09-05
  6  | Hotel City Inn  |   Cityzone1        |  15.00     |   2012-09-01
  7  | Hotel City Inn  |   Cityzone1        |  15.00     |   2012-09-02 
  8  | Hotel City Inn  |   Cityzone1        |  16.00     |   2012-09-03
  9  | Hotel City Inn  |   Cityzone1        |  16.00     |   2012-09-04
 10  | Hotel City Inn  |   Cityzone1        |  16.00     |   2012-09-05
-------------------------------------------------------------------------

When the user enter an Check-In date, a Check-Out date and select a City-Zone, the script will use PHP/MySQL structure to filter the City-Zone required and the dates within the period entered. So far so good and working but let's pretend that the user enters:

Check-In: 2012-09-01

Check-Out: 2012-09-05

City-Zone: Cityzone1

The output should be:

RESULT 1

Hotel: Beach Inn

Nights: 4

Check-In: 2012-09-01

Check-Out: 2012-09-05

Total Rate: 42.00 --> this exclude the first night rate because the numbers of night just starts counting from the second night

RESULT 2

Hotel: City Inn

Nights: 4

Check-In: 2012-09-01

Check-Out: 2012-09-05

Total Rate: 67.00 --> this exclude the first night rate because the numbers of night just starts counting from the second night

I have done so far the following script:

//MySQL Select structure that apply filter on user's entered information


$filter1=mysql_query("SELECT * FROM.........  ORDER BY htl_name, city_zone, given_date LIMIT 1");
$filtern=mysql_num_rows($filter1);
...
if($filtern>=1){
  While($row=mysql_fetch_array($filter1){
     $first_hotel_found=$row['htl_name'];
  }

  // New filter but listing everything within the date period

  $new_filter=mysql_query("SELECT * FROM ........ORDER BY htl_name, city_zone, given_date");
  $final_price=0; 
  $output="";
  while($row=mysql_fetch_array($new_filter){
     $htl_name=$row['htl_name'];
     $price=$row['price'];

     $final_price=$final_price+$price;

     if($htl_name!==$first_hotel_found){
        output .='Hotel:'.$htl_name.'<br/>';
        ... NO WORRIES WITH NUMBER OF NIGHTS AND CHECK-IN / CHECK-OUT DATES
        output .='Total Rate:'.$final_price.'<br/>';
        //after output the result go back clean up the $final_price and start adding again for the new Hotel
        $final_price=0;
        //$first_hotel_found assumes the actual $row
        $first_hotel_found=$row['htl_name'];
      }

    } // End of the while

}else{
     echo "There are no rates available for the information entered!";
     }

}

.... HTML portion ....

<body>
  <?php echo $output; ?>
</body>
</html>

The problem is that the output doesn't list both hotels instead list the name of the last hotel and the total rate from the first hotel:

Hotel: City Inn

Nights: 4

Check-In: 2012-09-01

Check-Out: 2012-09-05

Total Rate: 42.00

I would appreciate any help that will lead me to finish my project.

Thank you very much.

2 Answers 2

1

Here:

if($filtern>=1){
  While($row=mysql_fetch_array($filter1){
   $first_hotel_found=$row['htl_name'];
}

You are setting the first hotel under $first_hotel_found and when you later going to that loop:

 while($row=mysql_fetch_array($new_filter){

you have inside if($htl_name!==$first_hotel_found)

Since above you have set up $first_hotel_found this comparision with the first result is false and the first hotel does not showing in the results.

Sign up to request clarification or add additional context in comments.

2 Comments

Hey Zefiry! It is working now! Thanks my friend. Best Regards.
@AlexAraujo Please mark the tick next to his answer so that others can also refer to it.
1

This condition

if($htl_name!==$first_hotel_found){

is true exactly when $htl_name is Hotel City Inn for the first time (if the first hotel was Hotel Beach Inn). You print $htl_name and not $first_hotel_found, as you should.

You will never print information about the second hotel because the same condition will never be true again. If you add a third hotel, it will be true exactly when $htl_name is that hotel for the first time. To solve this you could copy the code that creates output to outside the loop as well, to execute it that last time:

output .='Hotel:'.$htl_name.'<br/>';
... NO WORRIES WITH NUMBER OF NIGHTS AND CHECK-IN / CHECK-OUT DATES
output .='Total Rate:'.$final_price.'<br/>';
//after output the result go back clean up the $final_price and start adding again for the new Hotel
$final_price=0;
//$first_hotel_found assumes the actual $row
$first_hotel_found=$row['htl_name'];

1 Comment

That was a good shot Calle. Now I have the two output but the name of the hotels are duplicated.

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.