Lab 2: (worth 10 points)

Please do not submit your work until you have completed all of the problems listed below.

How to Submit:

Create a Word document, paste the source code, capture the image of the output and paste it to the Word document, and submit the Word document as an attachment in Etudes-NG.   

Word attachment:  Submit the codes and the output only. Label your work as follows:

Problem 1:

(the codes)

(the output)

*************************************************************

Problem 2:

(the codes)

(the output)

**************************************************************

Note: Please include the rows of asterisks to separate each problem. Failure to follow the above instructions will result in a deduction of 4 points from your lab work.

************************************************************************

 1.  Create a string of names consisting of "John, Jerry, Ann, Sanji, Wen, Paul, Louise, Peter".

  1. Create a single dimensional array out of the string.
  2. Get the total number of values in the array
  3. Search for a specific value in the array.  You can determine what that value is.
  4. Sort the array by keys in descending order
  5. After sorting the array in (d), continue to sort the array by value in ascending order
  6. After sorting the array in (e), continue to sort the array in reverse order.
  7. Remove the first element from the array.
  8. Add "Willie" and "Daniel" to the end of the array.
  9. Replace "Paul" with "Andre".
  10. Add "Alisha" to the beginning of the array.
  11. Output the minimum and maximum value of the array
  12. Generate an output that returns the current element key and value and moves the internal pointer forward.
  13. Randomly retrieve a value from the array
  14. Create another array of names.  Choose names of your choice.
  15. Merge both of your arrays together and display them in sorted order.

          Generate an output for each of the items above.

          Reference:  http://www.w3schools.com/PHP/php_ref_array.asp

2.  Create a multidimensional array of movies organized by genre.  This should take the form of an associative array with genres as keys, such as Musical, Action, Adventure, and so forth.  Each of the array’s elements should be an array containing movie names, such as Passion of Christ, Star Wars, Chariots of Fire, and so on.  When your arrays are created, loop through them, printing the name of each genre and its associated movie(s).  Sort your array.

Here are some references on multidimensional arrays and associative arrays:

http://www.informit.com/articles/article.aspx?p=31840&seqNum=3

http://www.tizag.com/phpT/arrays.php

http://www.w3schools.com/php/php_arrays.asp

http://www.webcheatsheet.com/PHP/multidimensional_arrays.php

http://onlamp.com/pub/a/php/2001/06/21/php_foundations.html

http://www.informit.com/articles/article.aspx?p=31840&seqNum=4

http://www.php-learn-it.com/php_arrays.html

http://www.php.net/manual/en/language.types.array.php

There are many more sites on the web that you can google regarding multidimensional arrays and associative arrays.

3.  Create a feedback form that accepts a user’s full name in one textbox (indicate a message that the user must enter his/her first name first, then last name), state abbreviation where he/she lives (such as CA, NY, etc.), an email address, and the month, day and year of his/her birthday.  Use case conversion functions to capitalize the first letter of each name the user submits.  Check that the user’s email address contains the @ symbol and print a warning otherwise.  If the user did not input a state abbreviation, assign a default value of “CA.”  Generate an error message if user did not input full name, email address or birthday.  

List all the values the user inputted splitting the full name into first name and last name in your output. 

Example of output:  First name:  John

                                Last name:  Smith

The order how you display the first name and last name in the output depends on the alphabetizing of the first name and last name.  For example, if the full name is Susan Abbitte.  Alphabetically, Abbitte comes before Susan.  So, the output for full name will be:

Last name:  Abbitte

First Name:  Susan

However, if the user enters Betty Magadan, then the output for full name will be:

First name:  Betty

Last name:  Magadan

Other items in the output:

a.     A message that tells the user how many days, hours, minutes and seconds until the big day (birthday) from today’s date.  

Resources:  http://www.developertutorials.com/tutorials/php/calculating-difference-between-dates-php-051018/page1.html

http://www.tutorialhelpdesk.com/tutorials/web-development/php/date-and-time/calculating-the-difference-between-two-dates.html

http://www.hawkee.com/snippet/446/

http://www.php.net/manual/en/function.date.php

http://www.w3schools.com/php/php_ref_date.asp

b.    The length of user’s first name and the length of user’s last name.

c.    Shuffle all the characters in the last name and output the result.

d.    Output the last two characters in the lastname.

e.    If the user’s first name has a letter e or E, replace that with the letter o or O, respectively.  Output the result.

Reference:  http://www.w3schools.com/PHP/php_ref_string.asp

4. Create a Song Organizer script that stores songs in a text file.  Print a summary that includes the number of lines in the file and the file's size.  Append another song in the text file.  Include functionality that allows users to view the song lists and prevents the same song name from being entered twice.  Also, include code that sorts the songs by name and randomizes the song list with the shuffle( ) function.  

5.  Create a PHP script which takes two parameters: (1) the number of years and (2) the amount of money to be put on a savings account.  Include a function that   calculates the amount that will have been reached after a given number of years. Print the amount with no more than two decimals.  Come up with your own interest rate.

Short Problem Solving Exercises on Arrays:

Reference:  http://www.w3schools.com/PHP/php_ref_array.asp

6. Remove the last two chunks of values from the following array: 

         $array = array("Zero"=>"PHP", "One"=>"Perl", "Two"=>"Java");

 

Output the removed chunks, which are [One] => Perl and [Two] => Java,

and the remaining chunk, which is     [Zero] => PHP

 

7.  Given the following array:

$date = array('01', '01', '2006');

$keys = array('php', 'string', 'function');

 

Generate an output such as:

A formated date: 01/01/2006

A keyword list: php, string, function

 

8.  Create an array with a sequence of integers or characters.  Do not use a loop.

Please check:  http://www.w3schools.com/PHP/func_array_range.asp

9.  Given the following array:

$array = array("Zero"=>"PHP", "One"=>"Perl", "Two"=>"Java");

Generate an output such as:

This script will print:

Padded:

---,---,PHP,Perl,Java,>>,>>,>> 

 

10.  Pass an array of numbers into a function and output the average number in the array of numbers.

11.  Demonstrate how to return an array of random numbers from a function.

12.  Demonstrate how to define an array argument as a reference type.

13.  Demonstrate how an array is passed through an argument by value

 

Short Problem Solving Exercises on Files:

14. Create a text file and read the entire file into a single string.

15. Create a text file and read the file one character at a time

16. Create a text file with multiple lines and read those lines into an array.

17.  Create a text file with multiple lines, and read those lines one line at a time.

 *****************************************************************************

 Additional Notes for File Handling in PHP:

When working with data in flat files, the first thing you must do is open the file. This same function will also create the file if it does not already exist. Once open you can do several things with the contents.

Fread () is used to read the contents of the file for as many bytes as you specify, and return the data to the PHP file.

The file () command retrieves data from a flat file, and puts it into an array that you can the use in your PHP script.

Similar to file () is file_get_contents () which also reads the file, but instead of creating an array, returns all the data in a string.

Another way to read a file is line by line. This is how it is read if you use the fgets () function. It then leaves the pointer at the end of the last line read.

If you want to add data to the file you can do so using the fwrite () command. This will write directly from the current position of the pointer.

If you are writing to a file and do not wish to overwrite what is already there, you need to find the end of the file using the feof () function. This will place the pointer at the end of the last data in the file.

Here are some resources that may help:

http://www.php-learn-it.com/php_files/php_file_create.html

http://www.php-learn-it.com/php_files/php_file_read.html

http://www.php-learn-it.com/php_files/php_file_write.html

http://www.tizag.com/phpT/files.php

http://www.tutcity.com/tutorial/create-files-with-php.9693.html

http://www.ibm.com/developerworks/opensource/library/os-php-readfiles/

http://php.about.com/od/advancedphp/ss/php_read_file.htm

http://php.about.com/od/advancedphp/ss/file_write_php.htm

http://www.homeandlearn.co.uk/php/php10p2.html