PHP code help! Explode function in while loop (again)


#1

Code below is not working. It reads from a .txt file with strings of data including lines like the following:

Windows:2

Where first variable is the operating system and the second is the amount of copies ordered.

It’s supposed to count the number of orders that request more than one copy, the number of copies of Linux software ordered, the number of copies of Macintosh software ordered, and the number of copies of Windows software ordered. The numbers aren’t coming out correctly and I cannot figure out why.

Software2
<?php

	$multipleOrders = 0;
	$linuxCopies = 0;
	$macintoshCopies = 0;
	$windowsCopies = 0;

	$orderFile = fopen("orders.txt", "r");
	$nextOrder = fgets ($orderFile);

    while(!feof($orderFile))
    {
      list($operatingSystem, $numCopies) = 
        explode(":", $nextOrder);
      
      if($numCopies > 1)
      { 
        $multipleOrders = $multipleOrders + 1;
      }
      
      if($operatingSystem == "Linux")
      { 
        $linuxCopies = $linuxCopies + 1;
      }
      
      if($operatingSystem == "Macintosh")
       { 
         $macintoshCopies = $macintoshCopies + 1;
       }
      
       if($operatingSystem == "Windows")
       {  
         $windowsCopies = $windowsCopies + 1;
       }
      
      $nextOrder = fgets($orderFile);

     }

	fclose ($orderFile);

	print ("<h1>SOFTWARE ORDERS: REPORT</h1>");
	print ("<p>ORDERS FOR MULTIPLE COPIES: $multipleOrders</p>");
	print ("<p>LINUX COPIES ORDERED: $linuxCopies</p>");
	print ("<p>MACINTOSH COPIES ORDERED: $macintoshCopies</p>");
	print ("<p>WINDOWS COPIES ORDERED: $windowsCopies</p>");
?>

PHP Explode function and while loop problem
#2

Well, my guess is that it is due to the spaces in the input. Also, it might be due to spelling errors.
To fix any issues with spaces, change your if functions for the name of the operating systems to trim them first.
Such as if(trim($operatingSystem)==“Windows”)) { … } Trimming the name might solve your issue.

Also, you use capitals in your names. Windows vs windows… in your text file, are all of the operating systems spelled the same?

Good luck…

Oh, and if you wish to do this better, you can use arrays instead of counters. If interested, we can show you how you would do it that way.


#3

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.


#4

Well, not to solve your problem, but, to give you some details to help out.

To process a text file and read each line out, this is a better way in my humble opinion:

$Reservations = file("orders.txt");

foreach($Reservations as $Reservation) {
    Handle the data here
}

Makes the looping easier to read and to handle.
Now, inside the data handling area, you can use the list and explode the way you have it now, but you will need to trim the city name. Exploding of data from a string of chars sometimes leaves spaces in place. These are compared like all other chars and therefore you need to trim off both ends of them with trim();

Try it again and let us know if you have issues. Oh, one other thing. You should start learning on how to debug your programs. In this simple one, just before you use the list/explode code, you could add another line to display the incoming data. A simple echo $Reservation. “
”; would do. This way, just for testing you can see what the program is doing as far as what it is reading. That way you can see what it is starting out with for each line of text data read from the file. This, of course, would only be for you and testing and be removed before you turn in the project. Hope this all makes sense…