PHP Explode function and while loop problem

I’m currently taking an intro to PHP class online and my professor is overseas and I’m not getting the feedback I need in time, so here I am.

The code reads from a .txt file that contains a string containing lines like the following:

Rome:2:12
Tokyo:3:15

Being destination, number of travelers, and number of nights.

I cannot for the life of me figure out using the explode function in a while loop to extract and then test a variable that is non-numerical. Below is the code, which does not yield the correct answers. It’s supposed to read the and report the number of reservations in the file for Rome, and the total number of people traveling to Rome.

Rome Report
<?php
	$numParties = 0;
	$numTravelers = 0;

    $reservationFile = fopen("orders.txt", "r");
    $nextReservation = fgets($reservationFile);

    while(!feof($reservationFile))
    {
      list($destination, $numPeople, $numNights) = 
        explode(":", $reservationFile);
      
      if($destination == "Rome")
      { 
        $numParties = $numParties + 1;
        $numTravelers = $numTravelers + $numPeople;
       }
      
      $nextReservation = fgets($reservationFile);
      
      
    }


	print ("<h1>ROME TRAVEL REPORT</h1>");
	print ("<p>NUMBER OF PARTIES: $numParties</p>");
	print ("<p>NUMBER OF PEOPLE: $numTravelers</p>");
?>

So I’m assuming that the error in my code is the line stating:

      if($destination == "Rome")
      { 
        $numParties = $numParties + 1;
        $numTravelers = $numTravelers + $numPeople;
       }

I haven’t been able to find any solution just doing research on the internet. If anyone could help that would be wonderful.

2 posts were merged into an existing topic: PHP code help! Explode function in while loop (again)

Sponsor our Newsletter | Privacy Policy | Terms of Service