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).
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...
Write a C++ program that reads a string from a text file and determines if the...
Write a C++ program that reads a string from a text file and determines if the string is a palindrome or not using stacks and queue
C++ Write a program that prompts for a file name and then reads the file to...
C++ Write a program that prompts for a file name and then reads the file to check for balanced curly braces, {; parentheses, (); and square brackets, []. Use a stack to store the most recent unmatched left symbol. The program should ignore any character that is not a parenthesis, curly brace, or square bracket. Note that proper nesting is required. For instance, [a(b]c) is invalid. Display the line number the error occurred on. These are a few of the...
Write a program in Java that reads a file containing data about the changing popularity of...
Write a program in Java that reads a file containing data about the changing popularity of various baby names over time and displays the data about a particular name. Each line of the file stores a name followed by integers representing the name’s popularity in each decade: 1900, 1910, 1920, and so on. The rankings range from 1 (most popular) to 1000 (least popular), or 0 for a name that was less popular than the 1000th name. A sample file...
Write a php program that writes numbers to a file. The file type should be .txt...
Write a php program that writes numbers to a file. The file type should be .txt and the file name should be numbers.tx. The numbers.txt should contain 10 random integers between 1 to 100 after the file is written.
Write a program named FinalExamProgram2 that reads numbers from a file (which you will create using...
Write a program named FinalExamProgram2 that reads numbers from a file (which you will create using Notepad) into a one-dimensional array and then analyzes the numbers as described below. Your program must use loops to read the numbers into the array and to analyze the contents of the array. The program’s main function should do the following:  Read eight floating-point numbers from the file named numbers.txt into a onedimensional array, displaying each number on the screen.  Pass the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT