Question

In: Computer Science

In this assignment you will write a PHP program that reads from a data file, creates...

In this assignment you will write a PHP program that reads from a data file, creates an associative array, sorts the data by key and displays the information in an HTML table.

Create a PHP file called hw4.php that will do the following:

- Display your name.
- Read text data from a file. The data file is hw3.txt.

The file hw3.txt will hold the following text:

PQRParrot, Quagga, Raccoon
DEFDo statements, Else statements, For statements
GHIGeese, Hippos, If statements
YZ Yak, Zebra
JKLJelly Fish, Kudu, Lynx
MNOManatee, Nautilus, Octopus
ABCApples, Boas, Cats
VWXVulture, While statements, Xmen
STUSea Horse, Tapir, Unicorn

- Take the first three characters of each line read as the key and the rest of the line as the value or data.
- Create an associative array with the key and data.- Once all the data is in the array, sort the array by the key.
- Display the data as an HTML table. Below is what your finished table should look like.

Key Data
ABC Apples, Boas, Cats
DEF Do statements, Else statements, For statements
GHI Geese, Hippos, If statements
JKL Jelly Fish, Kudu, Lynx
MNO Manatee, Nautilus, Octopus
PQR Parrot, Quagga, Raccoon
STU Sea Horse, Tapir, Unicorn
VWX Vulture, While statements, Xmen
YZ Yak, Zebra

- Use repetition to complete this assignment.

Solutions

Expert Solution

Answer:

PHP file called hw4.php

<html>
<head>
<body>
<!-- Display name -->
Name :XZY
<br>
<?php
$fn = fopen("hw3.txt","r");

while(! feof($fn)) {
   // getting complete line of the file, line-by-line
   $line = fgets($fn);
   // getting first three characters of the line using function 'substr()'
   $field = substr($line, 0, 3);
   // getting remaining characters of line after first 3 characters
   $data = substr($line, 3);
   // Creating an associative array with these
   $arr[$field] = $data;  
}
  
fclose($fn);
?>
<?php
       // sort the associative array by key, using function 'ksort()'
       ksort($arr);
?>
       <!-- Creating a table and printing array key , value pair in each row -->
       <table align="left" border="1" cellpadding="10" cellspacing="0">
       <tr><td>Key</td><td>Data</td></tr>
<?php
      
           foreach ($arr as $key => $value) {
               echo "<tr>";
               echo "<td>".$key."</td>"."<td>".$value."</td>";
               echo "</tr>";
        }
      
?>
       </table>
</body>
</html>

Screenshot of Output:

-----------------------------------------------------------------------------------------------------------------------------------------


Related Solutions

In this programming assignment, you will write a program that reads in the CSV file (passenger-data-short.csv),...
In this programming assignment, you will write a program that reads in the CSV file (passenger-data-short.csv), which contains passenger counts for February 2019 on 200 international flights. The data set (attached below) is a modified CSV file on all International flight departing from US Airports between January and June 2019 reported by the US Department of Transportation. You write a program that give some summary statistics on this data set. Create a header file named flights.h. In this file, you...
Write a program that reads a file (provided as attachment to this assignment) and write the...
Write a program that reads a file (provided as attachment to this assignment) and write the file to a different file with line numbers inserted at the beginning of each line. Such as Example File Input: This is a test Example File Output 1. This is a test. (Please comment and document your code and take your time no rush).
In C++, write a program that reads data from a text file. Include in this program...
In C++, write a program that reads data from a text file. Include in this program functions that calculate the mean and the standard deviation. Make sure that the only global variables are the actual data points, the mean, the standard deviation, and the number of data entered. All other variables must be local to the function. At the top of the program make sure you use functional prototypes instead of writing each function before the main function... ALL LINES...
Specification This script reads in class data from a file and creates a dictionary. It then...
Specification This script reads in class data from a file and creates a dictionary. It then prints the data from the dictionary. The script has the following functions class_tuple_create print_classes class_tuple_create This function has the following header def class_tuple_create(filename): The function reads in a file with four fields of data about a class. The fields are Course ID Room number Instructor Start time The function must print an error message if the file cannot be opened for any reason. The...
Using the provided m1_mbox.txt, write a program that reads the data from the file, locates the...
Using the provided m1_mbox.txt, write a program that reads the data from the file, locates the lines containing sender information (those lines begin with 'From:'), and extracts just the email address of the sender of each message. It should then duplicate the set of email addresses for senders, and write the duplicated list to a file called senders.txt, so that each sender appears in the output file only once. In addition, your program should print to the screen the following...
Write a java program: Write a program that creates a text file. Write to the file...
Write a java program: Write a program that creates a text file. Write to the file three lines each line having a person's name. In the same program Append to the file one line of  'Kean University'.  In the same program then Read the file and print the four lines without lines between.
Overview: You will write a program that reads up to 100 numbers from a file. As...
Overview: You will write a program that reads up to 100 numbers from a file. As you read the numbers, insert them into an array in ascending order. Specifics: 1A. Write a function called insertIntoSortedArray . i. It should take three arguments - a. myArray[ ] : sorted array that should be able to hold at most 100 integers. b. numEntries : the number of elements inserted so far. c. newValue : the incoming value to be inserted into the...
Write a program that opens the file: "Lab6A_Data", reads all the values from the file, and...
Write a program that opens the file: "Lab6A_Data", reads all the values from the file, and calculates the following: A) The number of values in the file B) The sum of all the values in the file (a running total) C) The average of all the values in the file. D) The minimum value. E) The maximum value. F) The range of the data set of values. G) The number of times the value: '357' occurrs in the file. Display...
Create a program that creates a sorted list from a data file. The program will prompt...
Create a program that creates a sorted list from a data file. The program will prompt the user for the name of the data file. Create a class object called group that contains a First Name, Last Name, and Age. Your main() function should declare an array of up to 20 group objects, and load each line from the input file into an object in the array. The group class should have the following private data elements: first name ,last...
c++ Create a program that creates a sorted list from a data file. The program will...
c++ Create a program that creates a sorted list from a data file. The program will prompt the user for the name of the data file. Create a class object called group that contains a First Name, Last Name, and Age. Your main() function should declare an array of up to 20 group objects, and load each line from the input file into an object in the array. The group class should have the following private data elements: first name...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT