Hello everyone,
The question I have today is for a course I’m taking in PHP, so I’m looking for guidance here overall:
I’m trying to read data from a file, split the data into two variables, place those two variables into an associative array, alphabetically sort that array based on the contents of the variable representing the “key” data and then, finally, displaying the data inside a table.
Here is what my file looks like:
[CODE]
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
I am separating the first three characters from each line of text and using that for my “key” data in my associative array.
Here is my code:
[code=php]
<html>
<?php
@ $fp = fopen(“filename.txt”, ‘r’); // open file
if (!$fp) // error control
{
echo “<p>I could not open your file right now.</p>”;
}
echo “<table border=”1″>n”;
echo “<tr><th style=background-color:”#CCCCFF”>Key</th>
<th style=background-color:”#CCCCFF”>Data</th>
</tr>”;
while (!feof($fp))
{
$rline = fgets($fp, 60); // read text data
$key_data = substr($rline, 0, 3); // retrieving key data
$text_data = substr($rline, 3); // retrieving text data minus first three characters
$lines_of_text = array($key_data=>$text_data); // creating associative array
}
ksort($lines_of_text); // sort associative array by key values
while (list($key, $value) = each($lines_of_text)) // loop through each line of text and display data
{
echo “<tr><td align=”left”>”.$key.”</td><td>”.$value.”</td></tr>”;
}
fclose($fp); // closing file
?>
</html>
What I am getting is my table descriptions of ‘Key’ and ‘Data’ but nothing else. It’s like the code isn’t storing the file contents into my array.
However, I am able to get my table displayed if I rearrange the code by placing my second while statement within the first, like this:
[code=php]
<html>
<?php
@ $fp = fopen(“filename.txt”, ‘r’); // open file
if (!$fp) // error control
{
echo “<p>I could not open your file right now.</p>”;
}
echo “<table border=”1″>n”;
echo “<tr><th style=background-color:”#CCCCFF”>Key</th>
<th style=background-color:”#CCCCFF”>Data</th>
</tr>”;
while (!feof($fp))
{
$rline = fgets($fp, 60); // read text data
$key_data = substr($rline, 0, 3); // retrieving key data
$text_data = substr($rline, 3); // retrieving text data minus first three characters
$lines_of_text = array($key_data=>$text_data); // creating associative array
ksort($lines_of_text); // sort associative array by key values
while (list($key, $value) = each($lines_of_text)) // loop through each line of text and display data
{
echo “<tr><td align=”left”>”.$key.”</td><td>”.$value.”</td></tr>”;
}
}
fclose($fp); // closing file
?>
</html>
Except that I cannot sort the array based on my ‘key’ data which are obviously the letters of the alphabet. Where have I gone wrong here?
Thanks for any assistance!
Trellot