$value ) { print "$key => $value
"; foreach ( $value as $subkey => $subvalue ) { print "   $subkey => $subvalue
"; } ?> "> $value ) { print "$key => $value
"; foreach ( $value as $subkey => $subvalue ) { print "   $subkey => $subvalue
"; } ?> ">

Presentation is loading. Please wait.

Presentation is loading. Please wait.

Practice! Write a PHP script that does the following:

Similar presentations


Presentation on theme: "Practice! Write a PHP script that does the following:"— Presentation transcript:

1 Practice! Write a PHP script that does the following:
require “employees.inc” $employees = get_employees() Create a unique integer ID for each employee and add this ID to each employee array. Remove any duplicate entries. Print a list of distinct job titles for this company, with the number of employees for each title. Print the list as a table sorted by employee ID. Extra challenge: Sort by date of hiring!

2 Practice Initial Script – print the data structure <html>
<body> My Example: <br /> <?php require "/tmp/employees.inc"; $employees = get_employees(); foreach ( $employees as $key => $value ) { print "$key => $value<br />"; foreach ( $value as $subkey => $subvalue ) { print "   $subkey => $subvalue <br />"; } ?> </body> </html>

3 Practice “Create a unique integer ID for each employee and add this ID to each employee array.” How do we get Unique integers in MySQL? What variable type would hold an integer? How do you add an extra element to an array? $array = array(“One”, “Two”); $array[2] = “Three”; What is $array now?

4 Practice “Remove any duplicate entries.”
Another way to say it: Find the distinct entries In programming, “distinct” is often a clue that you want to use an associative array You define the keys and the values. $array[“test”] = true; $array[“test”] = false; $array[$key1] = true; $array[$key2] = false; What if $key1 and $key2 are equal? What makes up a unique entry?

5 Practice Our unique entries are currently stored as arrays. How can we turn an array into a string (so we can use it as a key)? Once we have the key, what should the value be?

6 Practice “Print a list of distinct job titles for this company, with the number of employees for each title.” Again, “distinct”. Maybe we should use an associative array. What would be the keys? The values?

7 Practice “Print the list as a table sorted by employee ID.”
PHP sorts array elements. If the elements are all numbers, or all strings, then we are okay. However, if the elements are more complex, we have to write our own array element comparison function.

8 Practice What are the array elements we’ll be sorting?
array('McGregor', 'E.N.', 'Brakeman', '8/18/1918‘, “SomeID”) array('Andrus', 'O.R.', 'Engineer', '9/4/1917‘, “SomeID”) How do you compare these? We want to compare the 5th elements (index 4): function byEmployeeID($a, $b) { if ($a[4] < $b[4]) { return -1; } // already sorted if ($a[4] == $b[4]) { return 0; } // already sorted if ($a[4] > $b[4]) { return 1; } // not sorted, swap! }


Download ppt "Practice! Write a PHP script that does the following:"

Similar presentations


Ads by Google