This week you use what you have learned about arrays, data serialization, cookie
ID: 3708750 • Letter: T
Question
This week you use what you have learned about arrays, data serialization, cookies, sessions, and working with the file system to build a simple phone book application. Your task is to read the serialized data from the phonebook.dat file and populate a simple HTML table that is provided to you. You will not need to be able to add data to this file, only read from it.
In the zip file attached are three files:
phonebook.dat - This files contains some phonebook data I have created for you. You will use this data for your application and will need to generate your own.
phonebook.php - This file in the template application file. Much of the code you will need in already in here (HTML, CSS, and some essential PHP).
There where 5 multi-dimensional array entries in this data file in a serialized format. Each entry looked something like this before serialization:
Your task was to read each line of the file, un-serialize the data, loop over the array, and fill out the HTML template provided. The result is a simple phone book!
The template files above contain most of the code you need to get you started. All you need to do is:
Open the data file
Read each line from the file
un-serialize the data
Print (echo) out the data into the HTML provided in the template
Loop to the next line and repeat the process.
The expectations and grade rubric
For a perfect grade:
The source data file is to remain unchanged.
All 5 entries are printed to the screen in the HTML format provided.
The application throws no errors (warnings are okay)
No entries are hard coded in HTML (that's cheating)
PHP File:
Phonebook:
# I'm going to silence warnings for you... you will gerate a ton of them.
# If you are having problem with your code, comment this line out so you can see the warnings.
error_reporting(E_ALL ^ E_WARNING);
# 1.) Open the file for reading
?>
My Phone Book
Phone book
# 2.) Loop over each line of the file
# 3.) unserialize the data from that line
# 4.) print it out below
?>
# 5.) Now you can end your loop and close the file
?>
Phonebook.dat file:
a:4:{s:4:"Name";s:8:"John Doe";s:7:"Address";a:4:{s:6:"Street";s:11:"123 Main St";s:4:"City";s:5:"Dover";s:5:"State";s:2:"DE";s:3:"Zip";s:5:"19904";}s:5:"Phone";s:12:"555-555-1234";s:5:"Email";s:16:"john@example.com";}
a:4:{s:4:"Name";s:8:"Jane Doe";s:7:"Address";a:4:{s:6:"Street";s:11:"123 Main St";s:4:"City";s:5:"Dover";s:5:"State";s:2:"DE";s:3:"Zip";s:5:"19904";}s:5:"Phone";s:12:"555-555-5678";s:5:"Email";s:16:"jane@example.com";}
a:4:{s:4:"Name";s:15:"Jonny Appleseed";s:7:"Address";a:4:{s:6:"Street";s:15:"1 Infinite Loop";s:4:"City";s:10:"Coopertino";s:5:"State";s:2:"CA";s:3:"Zip";s:5:"90201";}s:5:"Phone";s:12:"555-555-9101";s:5:"Email";s:15:"jonny@apple.com";}
a:4:{s:4:"Name";s:12:"Jack Sparrow";s:7:"Address";a:4:{s:6:"Street";s:15:"The Black Pearl";s:4:"City";s:17:"Cut Throat Island";s:5:"State";s:7:"Tortuga";s:3:"Zip";s:5:"00001";}s:5:"Phone";s:12:"555-555-1213";s:5:"Email";s:14:"jack@savvy.com";}
a:4:{s:4:"Name";s:14:"Luke Skywalker";s:7:"Address";a:4:{s:6:"Street";s:15:"17 Jedi Council";s:4:"City";s:8:"Corasant";s:5:"State";s:2:"XX";s:3:"Zip";s:5:"99999";}s:5:"Phone";s:12:"555-555-1415";s:5:"Email";s:17:"luke@theforce.com";}
Explanation / Answer
# 1.) Open the file for reading
$myfile = fopen(__DIR__ . '/phonebook.dat', 'r');
?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>My Phone Book</title>
</head>
<body>
<h1>Phone book</h1>
<?php
$count = 0;
# 2.) Loop over each line of the file
foreach(file('phonebook.dat') as $line) {
$count++;
$arrayX[$count] = $line. " ";
echo $line;
}
echo ($arrayX[1])['Street'];
print_r (unserialize($arrayX[2])); //example testing output
# 3.) unserialize the data from that line
# 4.) print it out below
$id = '19904';
foreach(file('phonebook.dat') as $link) {
if ($link['Address']==$id) {
$result = $link;
echo $result;
break;
}
}
foreach (file('phonebook.dat') as $palabra) {
foreach ($palabra as $key=>$parto) {
if (is_numeric($key)) {
echo $parto['Street'] . "<br>";
}
}
}
echo $newList = array_combine(array_column($arrayX[1],'Street'),$arrayX[1]);
$search='Street';
$cluster=false;
foreach (file('phonebook.dat') as $n=>$c)
if (in_array($search, $c)) {
$cluster=$n;
break;
}
echo $cluster;
for($counter = 1; $counter < 6; $counter++)
{
?>
<table border="0" width="">
<tr>
<td>Name:</td>
<td><?php echo (unserialize($arrayX[$counter]))['Name']; ?></td>
</tr>
<tr>
<td>Street:</td>
<td><?php echo (unserialize($arrayX[$counter]))['Address']['Street']; ?></td>
</tr>
<tr>
<td>City:</td>
<td><?php echo (unserialize($arrayX[$counter]))['Address']['City']; ?></td>
</tr>
<tr>
<td>State:</td>
<td><?php echo (unserialize($arrayX[$counter]))['Address']['State']; ?></td>
</tr>
<tr>
<td>Zip:</td>
<td><?php echo (unserialize($arrayX[$counter]))['Address']['Zip']; ?></td>
</tr>
<tr>
<td>Phone:</td>
<td><?php echo (unserialize($arrayX[$counter]))['Phone']; ?></td>
</tr>
<tr>
<td>Email:</td>
<td><?php echo (unserialize($arrayX[$counter]))['Email']; ?></td>
</tr>
</table>
<hr>
<?php
} //end of the while loop
# 5.) Now you can end your loop and close the file
fclose($myfile);
?>
</body>
</html>
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.