Ask PHP Expert

You have a new file to work with:  bobsinventory.php.  The great news is that this file contains a single string variable with every piece of data from the inventory table you first encountered in Program #3, but also two more pieces of data.  So, now you won't be burdened with the need to create dozens of variables and initialize them to table values.  The whole table and more is in the file!

As suggested above, the days rented value for each vehicle has been updated with the correct number.  The names of the vehicles have been abbreviated somewhat, and now there are no spaces in any of the individual data.  Further, there are two more "fields" of data:  the actual revenue generated so far by each vehicle and the mileage for each one.  Someone told Bob that if he doesn't track the mileage, then using the depreciation factor alone might raise eyebrows in certain federal units!

So, just to summarize, the file now contains details on each vehicle in this order:  vehicle name; model year; serial number; #seats; rental charge per day; days rented so far this year; total revenue for this vehicle so far; original price of vehicle; mileage on the vehicle; depreciation factor; and scheduled maintenance interval.  (With all this data available, it's almost as if we got these numbers out of a database! J)

Of course, now the challenge becomes that of getting the data into appropriate variables to use. Actually, I should say into an appropriate array, because as you surely must anticipate, that's what this program is going to be about.  One by one you will need to read each vehicle's data into an associative array.  I will cover some specifics in just a moment.

NOTE.  There are many different ways to go about achieving the goals of any programming project.  There are almost always multiple ways of accomplishing the same thing with different coding structures.  But what this program will require are two mandatory concepts:  #1, use an associative array; and #2, successfully use a foreach loop.  Your processing must consist of a similar while loop to what you used in Program #5:  a while loop predicated on there still being data to take out of the string with strtok.  Only this time, instead of grabbing the values and placing them into separate variables, you must use an associative array to hold all eleven pieces of data for one vehicle.  You are to use only one array to hold vehicle data:  you are not allowed to use a separate array for each vehicle!  Therefore, you will need to grab the data for each vehicle one set of data at a time.

Let's say you decide to use an array called $vehicle.  Make up string names for each of the eleven keys you will need for the array.  For example, you might use 'name' for the vehicle's name.  Loading the array elements is no different from loading the data into a "normal" variable:  you just need to use the correct array element on the left side of the assignment (with the correct key).  For example, to get the first set of values, I would probably start pulling them out this way:

$vehicle['name'] = rtrim(strtok($rental_data, " "));

$vehicle['year'] = rtrim(strtok(" "));

$vehicle['serial']  = rtrim(strtok(" "));

And so on.  The first 10 values for each vehicle are terminated by spaces and the last value by a carriage return.

(Remember that after the very first call to strtok, you can't use the variable name again, or else the strtok function will start all over again at the beginning of the entries.)  So, now after you have grabbed the first set of vehicle data, the element $vehicle['name'] will have the value 'DodgeAvenger' in it.  Continuing, $vehicle['year'] will have the value 2006 in it, etc.

After getting all of the first vehicle's eleven values into the array, now start up your while loop.  You can just use the array's first element as the condition, since if the value $vehicle['name'] is defined, then there is more work to do.

Inside the while loop is the time you need to process the array.  That gets us to my next mandatory requirement.  You must use a foreach loop to loop through all the elements of the array $vehicle.  Your first requirement inside the loop will be to display some data for each vehicle in columns.  For that purpose you are not allowed to simply use the array values directly, for example, printing $vehicle['name'] and $vehicle['year'], etc.  That would be much too easy, and you wouldn't be learning anything about the foreach loop.

So, inside your while loop, start a foreach loop.  As I illustrated in my Addendum to this chapter, the foreach syntax "breaks down" the loop elements into two simple variables, often called $key and $value.  If you follow the naming system I use in the Addendum, then the first $key will be 'name' and the first $value will be 'DodgeAvenger'.  The next time through the foreach loop, $key will be 'year' and $value will be 2006.  So, each time through your foreach loop the variables $key and $value become the next key/value pair for the current vehicle.

(At this point you might be thinking:  this is surely a clunky way to deal with the car data.  Why not just use the array data directly?  The answer is that I want you to learn as much about the foreach loop as possible.  So, I want to force you to work with the data broken down inside the foreach loop to really master that tool.)

Here's what I want you to perform inside the foreach loop.  Display a one-line listing of some of the data for the vehicle.  Show data for each vehicle in the following order.  (These are example headers for each column.)

VEHICLE YEAR SERIAL# RENTALCOST DAYSOUT REVENUE ORIGPRICE MILEAGE NOTES   

The VEHICLE through MILEAGE values come right from your data values.  But note that you are not to display the values for Seats, Depreciation Factor, and Maintenance Interval.  As you encounter these values in the foreach loop, you need to skip printing them.  (Hint:  you'll need to test to see if the $key is equal to any of these.)

So, just to make this as clear as I can.  Inside the foreach loop, you will have displayed the VEHICLE, YEAR, etc all the way to the MILEAGE values for one vehicle.  The total amount of code required to do this should not be more than two to three lines, not including the start of the loop, the foreach header.  I am not looking for a beautifully composed set of incredibly spaced and formatted columns.  I am looking for whether or not you understand how to use the keys and values as they are parsed from the array.

Now, after the foreach loop has displayed almost one complete line, there are a few more details to attend to.  The NOTES column is not yet composed.  Here's the kind of notes that you need to display.  For any vehicle that has over 100,000 miles, display the letter 'I' in that column, a flag that this vehicle needs to be Inspected more often.  Also, for any vehicle that has fewer than 10 days actually rented, place an 'L' in the NOTES column, a flag that shows that this vehicle is an underperformer (Loser).  Of course, there may be vehicles with both of those letters in the NOTES column.  To take care of the NOTES, though, you have full use of the array and its elements.  So, just test the appropriate elements and print either or both of those characters for the vehicles that fall into those categories.

Now, just a couple more things to do.  Bob wants two totals for the end of the report:  the total number of days that all vehicles have been rented; and the total revenue for all rental operations.  Both of these totals, of course, must also be accumulated within your while loop.  (Don't forget to display them at the end of the report.)  At the bottom of the loop, you need to "read" another set of data into the $vehicle array.

PHP , Programming

  • Category:- PHP
  • Reference No.:- M9524174

Have any Question?


Related Questions in PHP

Question continue to build on the skills of providing web

Question: Continue to build on the skills of providing Web page content and structure with HTML and Web page style and layout using CSS. Demonstrate the skill of creating a dynamic Web page that includes JavaScript clien ...

Question develop a 5-6 page word document that describes

Question: Develop a 5-6 page Word document that describes the design for a small Web site that meets the following specifications. 1. The Web site includes four or more Web pages designed according to current usability g ...

Question using the course software and week one template as

Question: Using the course software and Week One Template as a starting point, create a new HTML webpage and include the following: • Write the code to display your name in the largest-size heading element. • Write the m ...

In php write a simple addtion calculator that reads in two

In PHP write a simple addtion calculator that reads in two values and then add them together and display the output. Be sure to validate both inputs and ensure that it can add 0+0 = Sum: 0. The user should be able to inp ...

Final project assignment -requirements specifications -1

FINAL PROJECT ASSIGNMENT - REQUIREMENTS / SPECIFICATIONS - 1. For the final project, you will have to make good use of your CMS using PHP and MySQL for the content delivery. You will have to be able to deliver the conten ...

  • 4,153,160 Questions Asked
  • 13,132 Experts
  • 2,558,936 Questions Answered

Ask Experts for help!!

Looking for Assignment Help?

Start excelling in your Courses, Get help with Assignment

Write us your full requirement for evaluation and you will receive response within 20 minutes turnaround time.

Ask Now Help with Problems, Get a Best Answer

Why might a bank avoid the use of interest rate swaps even

Why might a bank avoid the use of interest rate swaps, even when the institution is exposed to significant interest rate

Describe the difference between zero coupon bonds and

Describe the difference between zero coupon bonds and coupon bonds. Under what conditions will a coupon bond sell at a p

Compute the present value of an annuity of 880 per year

Compute the present value of an annuity of $ 880 per year for 16 years, given a discount rate of 6 percent per annum. As

Compute the present value of an 1150 payment made in ten

Compute the present value of an $1,150 payment made in ten years when the discount rate is 12 percent. (Do not round int

Compute the present value of an annuity of 699 per year

Compute the present value of an annuity of $ 699 per year for 19 years, given a discount rate of 6 percent per annum. As