Jump to content

Arrays with .txt files and tables


crose1211

Recommended Posts

I have been working with a telephone directory that I created for a php class and it consists of three files: One file to write, one file to read and the html directory. Well, four if you include the contacts file. We have to take the file this week and create a two column table with an array from the contact file.  I cannot for the life of me figure out how to get an array into two separate columns of a table with the php code.

 

I will post the code here that I do have (the one I attempted to do the table on is scary..because as I said it's not working like I had hoped, but I did try!) Any sort of direction here would be much appreciated.  I have looked through my book and online but all I can find online is examples with SQL and we aren't that far into the class yet.  My book doesn't offer any sort of help with tables, just the arrays, so I am stumped and this assignment is due tonight!  They have tutoring rooms but I had to take the baby to the doctor this morning and didn't make it in time and they are closed now. :-( Thanks in advance for any type of help of advice you can offer!

 

This is my form:

<html xmlns="http://www.w3.org/1999/xhtml">
<head></head>
<body>
<form action='phoneWrite.php' method='POST'>
<!--Username: <input type='text' size='15' maxlength='25'>-->

<h1>Telephone Directory</h1>
<br>
<table align='Left'>
  <tr>
    <td>Last Name:</td>
    <td><input name='LastName' /></td>
  </tr>
  <tr>
    <td>First Name:</td>
    <td><input name='FirstName' /></td>
  </tr>
  <tr>
    <td>Street Address:</td>
    <td><input name='StreetAddress' /></td>
  </tr>
  <tr>
    <td>City:</td>
    <td><input name='City' /></td>
  </tr>
  <tr>
    <td>State:</td>
    <td><input name='State' /></td>
  </tr>
  <tr>
    <td>Zip:</td>
    <td><input name='Zip' /></td>
  </tr>
  <tr>
    <td>Area Code:</td>
    <td><input name='AreaCode' /></td>
  </tr>
  <tr>
    <td>Phone Number:</td>
    <td><input name='PhoneNumber' /></td>
  </tr>
  <tr>
    <td></td>
    <td><input type="submit" value="Register" /></td>
    <td></td>
</tr>
<tr>
<td></td>
<td><input type="reset" value="Clear" /></td>
<td></td>
</tr>
</table>
</form>
</body>
</html>

This is the php used to write data:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
</head>
<body>
<?php
if (empty($_POST['LastName']) || empty($_POST['FirstName']) || empty($_POST['StreetAddress']) || empty($_POST['City']) ||
empty($_POST['State']) || empty($_POST['Zip']) || empty($_POST['AreaCode']) || empty($_POST['PhoneNumber']))
	echo "<p>You must enter your information.</p>\n";
else {
$lName = addslashes($_POST['LastName']);
$fName = addslashes($_POST['FirstName']);
$address = addslashes($_POST['StreetAddress']);
$city = addslashes($_POST['City']);
$state = addslashes($_POST['State']);
$zip = addslashes($_POST['Zip']);
$area = addslashes($_POST['AreaCode']);
$number = addslashes($_POST['PhoneNumber']);
$NewEntry = "$lName, $fName, $address, $city, $state, $zip, $area, $number\n";
$ContactFile = fopen("contacts.txt", "ab"); 
if (is_writeable("contacts.txt")) {
if (fwrite($ContactFile, $NewEntry))
	echo "<p> Registration Success!</p>\n";
else 
	echo "<p> Registration Failed, please try again.</p>\n";
}		
else
	echo "<p> Cannot add registration to the database.</p>\n";
fclose($ContactFile);
}
?>

<form action = "phoneRead.php" method='POST'>
<h1>Telephone Directory</h1>
<p>Last Name: <input type="text" name ="LastName"
size = "20" /></p>
<p>First Name: <input type="text" name="FirstName"
size = "20" /></p>
<p>Street Address: <input type="text" name="StreetAddress"
size = "20" /></p>
<p>City: <input type="text" name="City"
size = "20" /></p>
<p>State: <input type="text" name="State"
size = "20" /></p>
<p>Zip: <input type="text" name="Zip"
size = "20" /></p>
<p>Area Code: <input type="text" name="AreaCode"
size = "20" /></p>
<p>Phone Number: <input type="text" name="PhoneNumber:
size = "20" /></p>
<p><input type="submit" value="Register"/></p>
<p><input type="reset" value="Clear" /></p>
<a href="http://localhost/MillerAssignment4/contacts.txt">Read file</a>
</form>
</body>
</html>

And this is what I have so far on the table/read file:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
</head>
<body>
<?php
if ((!file_exists("contacts.txt"))
|| (filesize("contacts.txt")
== 0))
echo "<p> There are no entries in your telephone directory.</p>\n";
else {
$ContactArray = 
	file("contacts.txt");
echo "<table
	style = \"background-color:lightgray\"
	border = \"1\" width = \"100%\">\n";
$count = count($ContactArray);
	for ($i = 0; $i < $count; ++$i) {
		$CurrMsg = explode("~",
			$ContactArray[$i]);
echo "<tr>\n";
echo "<td width=\"5%\"
style=\"font-weight:bold\">Name:
</span> " .
htmlentities($ConNum[0]) .
"<br />\n";
echo "<span
	font-weight:bold\">Phone Number:
	</span><br />\n"
	htmlentites($ConNum[2]) .
		"</td>\n";
echo "</tr>\n";
}
echo "</table>\n";
}
?>
</body>
</html

 

The contact.txt file is attached if you want to look at it

 

[attachment deleted by admin]

Link to comment
Share on other sites

Well for starters you never really said what error you were getting with it but from looking at your code, I am not 100% sure what is actually going on but some of the things I did find were all in the table read/write file.

 

1.) you are using $ConNum[0] and $ConNum[2] when displaying the values but you never defined that variable anywhere, in fact you seem to have called it $CurrMsg when you called the explode of the file array.

 

2.) you are exploding on a ~, but when you put the file together everything is seperated by a comma.

 

the file function returns the file as an array, with each element being a different line in the file (including the \n at the end if that matters to you or not im not sure).

 

If you need to figure out what is in the array after reading it in, you can use print_r($ContactArray); to see the exact content the variable has, this might help you to see if you are reading or parsing it correctly.

Link to comment
Share on other sites

Well the project wants us to take the data from the contact.txt file and break it into a two header column with just the name and number.  The name will be last name first, with the first name seperated by a comma and the phone number will be cocantenated (or however you spell it lol) like this ex: 123-456-7890

 

I did type CurrMsg that was an accident, I was looking at some examples in my book when I typed that.  Okay, I think I can figure the array out I just need some information on how to separate the data into two columns.  Our professor gave an example like this in class

 

$Rawphonenumber="800-555-5555"'
$phoneChunks=explode("-", $Rawphonenumber);
echo "Rawphonenumber=$Rawphonenumber<br/>";
echo "Firstchunk=$$phoneChunks[0]<br/>"; 

 

While this is a good example of the explode function, I still have to think about the fact that I am using a text file, not just typing the data in manually.  I could type it in manually I suppose from the text file but I really want something that will read it and sort it.  The sort function is easy to use so I think I have that covered as well.  I can figure out the array this morning but as far as getting it into a table that's where I still have no idea where to start.  Thanks for the reply! :confused:

Link to comment
Share on other sites

I am not really sure what it is you are looking for as the code you say you need you already have in the file you posted and that I corrected.

 

Since your file seems to be saved in this format:

 

"$lName, $fName, $address, $city, $state, $zip, $area, $number\n";

 

when you read it in with file() you will have an array of each line being a different entry in the array. Like you have before you want to iterate through the entire array so you use a loop, in your case you used a for loop.

 

$ContactArray = file("contact.txt");
$count = count($ContactArray);

echo "table start information";

for($i = 0; $i < $count; $i++)
{
   $row = explode(","  ,  $ContactArray[$i]);

   $lastname = $row[0];
   $firstname = $row[1];
   $phone = $row[7];

  echo "The number for {$lastname}, {$firstname} is: {$phone}<br />";
}
echo "end table";

 

This snippet will put the last and first name of every contact in the file that is saved in the above format that you seem to be writing them in to, and their phone number, each on a separate line.

Link to comment
Share on other sites

thanks for this post and reply to my question so much.  I created this multi dimensional array just to manually input the information but like I said I have to have it where it reads the information so this really helps put things into perspective.  This way it has to be done manually.

 

$Contacts = array(
array('Miller,Rose', '903-799-5939'),
array('Williams, Jimmy', '903-100-9999'),
array('Jordan, Jayden', '385-190-3456'),
array('Patterson, Amanda', '305-174-3905'),
array('Miller, Ian', '234-094-6793'),
array('Purgason,Zoey', '234-936-3754'),
array('Barr,Kenneth', '123-456-8900'),
array('Beckes, Mandii', '900-888-3457'),
array('Stevens, Donny', '127-097-4873'),
array('Krunch, Kapn', '405-504-3890')
);
sort($Contacts)
?>
<table border="1">
<tr>
<th>Name</th>
<th>Number</th>
</tr>
<?php
foreach($Contacts as $Contacts)
{
echo'<tr>';
foreach($Contacts as $item)
{
echo"<td>$item</td";
}
echo'</tr>';
}
?>
<a href="http://localhost/millerUnit5/telephoneDirectory.html">Back to Form</a>
</table>
</body>
</html>
</body>
</html>

 

Now to work on this again with the tips you have provided!  I am so grateful! Thanks so very much!!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.