COP 2842, Web Scripting (PHP), Fall 2013, Due date: October 24, 2013 midnight. A
ID: 3544548 • Letter: C
Question
COP 2842, Web Scripting (PHP), Fall 2013,
Due date: October 24, 2013 midnight.
Assignment 5 (16 points)
1. According to the U.S. Census Bureau, the 12 largest American cities (by population) in 2005 were as follows: (the numbers are not actual numbers)
? New Jersey, NJ (9,008,278 people)
? Los Angeles, CA (3,694,410)
? Chicago, IL (2,985,016)
? Houston, TX (1,845,631)
? Philadelphia, PA (1,517,550)
? Phoenix, AZ (1,321,045)
? San Diego, CA (1,283,400)
? Dallas, TX (1,188,580)
? San Antonio, TX (1,198,646)
? Detroit, MI (951,270)
? Georgia, GA (378, 457)
? New York, NY (678, 4567)
Write a script that define an array (or arrays) that holds this information about locations and population. Print a table of locations and population information that includes the total population in all 12 cities.
? Modify your solution so that the rows in result table are ordered by population. (6 points)
? Then modify your solution so that the rows are ordered by city name. (6 points)
Explanation / Answer
Here is your php script to accomplish all three requirements. Let me know via comment if you need further help:
<html>
<head><title>PHP Arrays</title></head>
<body>
<?php
function print_table($cities)
{
echo '<table border="2" cellpadding="5"><tr><th>City Name</th><th>State</th><th>Population</th></tr>';
foreach($cities as $obj)
{
echo '<tr>';
echo '<td>' . $obj['name'] . '</td><td>' . $obj['state'] . '</td><td>' . $obj['population'] . '</td>';
echo '</tr>';
}
echo '</table>';
}
$cities = array();
$cities[] = array('name' => 'New Jersey', 'state' => 'NJ', 'population' => 9008278);
$cities[] = array('name' => 'Los Angeles', 'state' => 'CA', 'population' => 3694410);
$cities[] = array('name' => 'Chicago', 'state' => 'IL', 'population' => 2985016);
$cities[] = array('name' => 'Houston', 'state' => 'TX', 'population' => 1845631);
$cities[] = array('name' => 'Philadelphia', 'state' => 'PA', 'population' => 1517550);
$cities[] = array('name' => 'Phoenix', 'state' => 'AZ', 'population' => 1321045);
$cities[] = array('name' => 'San Diego', 'state' => 'CA', 'population' => 1283400);
$cities[] = array('name' => 'Dallas', 'state' => 'TX', 'population' => 1188580);
$cities[] = array('name' => 'San Antonio', 'state' => 'TX', 'population' => 1198646);
$cities[] = array('name' => 'Detroit', 'state' => 'MI', 'population' => 951270);
$cities[] = array('name' => 'Georgia', 'state' => 'GA', 'population' => 378457);
$cities[] = array('name' => 'New York', 'state' => 'NY', 'population' => 6784567);
echo '<h3>Original Data: </h3>';
print_table($cities);
# Calculate Total:
$total = 0;
foreach($cities as $obj)
{
$total += $obj['population'];
}
echo 'The total population is: ' . $total . '.';
echo '<h3>Ordered by population: </h3>';
foreach ($cities as $key => $row)
{
$population[$key] = $row['population'];
}
array_multisort($population, SORT_DESC, $cities);
print_table($cities);
echo '<h3>Ordered by city name: </h3>';
foreach ($cities as $key => $row)
{
$city_name[$key] = $row['name'];
}
array_multisort($city_name, SORT_ASC, $cities);
print_table($cities);
?>
</body>
</html>