Php program, PHP Web Programming

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.

Posted Date: 2/25/2013 12:57:13 AM | Location : United States







Related Discussions:- Php program, Assignment Help, Ask Question on Php program, Get Answer, Expert's Help, Php program Discussions

Write discussion on Php program
Your posts are moderated
Related Questions
Web Master needed for Word Press as well as aMember Mainly as a webmaster familiar with aMember as well as WordPress As well for web design and development. Marketing savv

I want a Eventbrite programmer Need someone to write a code that will implement custom tickets for eventbrite. Desired Skills are eventbrite

Website Development for Nutrition Company We are in search of a developer with a strong skill set. We need to be sure the candidate can handle ecommerce, responsive site deve

Project Description: Want an API expert to aid in application development in regards to API extraction, and injection into other sites, as the main site is updated. Do not bid i

We need a web developer We have a very modest three page site on go daddy and just want someone to design it through the go daddy site. We as well would like a logo designed.

I require a Shopify that Must have Ecommerce and Design Quality Knowledge We are looking for a Professional Web Designer who has experience in setting up Shopify E-commerce stor

Update PHP / HTML form I need the HTML modernized to HTML5. The customer likewise has to fill in a phone number and email address, else the form should not validate. As of

Seeking urgent assistance with loading an ASP.net website We are urgently seeking assistance with loading an ASP.net website installation onto a new server, job will start immed

Chrome extension I am seeking a chrome extension for amazon.com. The extension will make it relaxed for me to get in touch with customers that have left me a feedback. Have you

I want help in vBulletin clean and update I have an installation of vBulletin that has been compromised, I have taken some steps to secure my installation but I need someone to