Forum Moderators: coopster

Message Too Old, No Replies

Reading from a text file

         

ShiggyWiggy

10:30 am on Jan 9, 2004 (gmt 0)

10+ Year Member



I'm having trouble getting information to be read from a text file and displayed through a web browser.

I have a text file with information such as

Number of Posts ¦ 23
Users Logged in ¦ 12

I know this kind of thing is usually in a database but was hoping to get the information from a text file to begin with.
What I want is a way of getting the information from the text file (especially the data after the ¦ ) and displaying it in a table, but its causing me some problems.

Is this possible?
Could anyone give me some help/advise as to how to get it to work.

Would it be easier to set up a small database and use that? If I was to do that what would be the best way to go about it?

Thanks

P.S I've just started learning how PHP works sorry if this seems like a stupid problem.

justageek

2:00 pm on Jan 9, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



$line_data = file(your_file_name);
for($i=0;$i<=count($line_data);$i++){
$line_elements = explode("¦",$line_data);
$posts = trim($line_elements[0]);
$logged_in_users = trim($line_elements[1]);
}

coopster

2:55 pm on Jan 9, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld, ShiggyWiggy!

There are a number of ways to accomplish your task. Along with the offering here by justageek, you may also want to read Basics of extracting data from CSV files [webmasterworld.com] in the PHP Forum Library [webmasterworld.com].

ShiggyWiggy

3:10 pm on Jan 9, 2004 (gmt 0)

10+ Year Member



Thanks for the replies. I had similar code to justageek but it wasn't quite as 'clean' bit messy with trying to display what was in the array.

This is what I have at the moment

<?php
$fp = fopen("test.txt","r");
$line_data = file("test.txt");
for($i=0;$i<=count($line_data);$i++){
$line_elements = explode("¦",$line_data);
$posts = trim($line_elements[0]);
$logged_in_users = trim($line_elements[1]);
}
fclose($fp);
print("$posts");
print("$logged_in_users");

?>

But when I try to view it it just displays the word Array on the screen.
But I can't see why it does this.

coopster

3:21 pm on Jan 9, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



That's because they are arrays. You see, the explode function returns an array of strings, each of which is a substring of the string (in your case, the $line_data variable) formed by splitting it on boundaries formed by the the string separator (in your case, the pipe symbol). You can view the data in the array with something like this:

print '<pre>';
print_r($posts);
print_r($logged_in_users);
print '</pre>';

ShiggyWiggy

3:43 pm on Jan 9, 2004 (gmt 0)

10+ Year Member



Still outputs the word array.

totally baffled by it now.

I appreciate your help though. Thanks

coopster

4:32 pm on Jan 9, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Well, I responded a bit quickly too. I notice now that you are assigning those variables after the explode array assignment.

First, you are going to want to print your statements inside the loop, so you could trim a bit of code by just printing the trimmed value rather than storing it in a variable.
Secondly, the loop should only be occurring when the index is less than the array count as arrays begin with 0, but the total count will always be one more.
And lastly, we need to use an index in the array assignment within the explode function:


$fp = fopen("test.txt", "r");
$line_data = file("test.txt");
for($i=0;$i<count($line_data);$i++){
$line_elements = explode("¦", $line_data[$i]);
print trim($line_elements[0]) . '<br />';
print trim($line_elements[1]) . '<br />';
}
fclose($fp);

ShiggyWiggy

4:43 pm on Jan 9, 2004 (gmt 0)

10+ Year Member



Excellent. Works now, Thank you for your help.

ShiggyWiggy

4:45 pm on Jan 9, 2004 (gmt 0)

10+ Year Member



Oh one other thing before I forget. Is it possible to put the information like this into an HTML table within the page or is that not possible with php?

Once again, thanks for helping out.

justageek

5:42 pm on Jan 9, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



$line_data[$i]

Ooops. Forgot that :-)

JAG

coopster

6:11 pm on Jan 9, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



>>Is it possible to put the information like this into an HTML table within the page...

Absolutely, that's the beauty of it all! See where I inserted HTML <br /> tags? Replace those with your <td> and you are good to go. Instead of printing the information during the loop, you can build a variable and print it later. Something like this:


<?php
$table_data = '';
$fp = fopen("test.txt", "r");
$line_data = file("test.txt");
for($i=0;$i<count($line_data);$i++){
$line_elements = explode("¦", $line_data[$i]);
$table_data .= '<tr><td>' . trim($line_elements[0]) . '</td>';
$table_data .= '<td>' . trim($line_elements[1]) . '</td></tr>';
}
fclose($fp);
?>
<html><head><title>Title</title></head><body>
<table><?php print $table_data; ?></table>
</body></html>

justageek

7:50 pm on Jan 9, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You should also remove the

$fp = fopen("test.txt", "r");

and

fclose($fp);

lines to save some overhead.

file() will open and close the file so having the fopen() will be one extra call to open a file you won't use anyway.

JAG

ShiggyWiggy

7:51 pm on Jan 9, 2004 (gmt 0)

10+ Year Member



Wow, didn't realised it worked like that.
Thanks a lot. Really helped me realise what is possible. Now time to get back to looking into it all in more detail now that I know the kind of things possible with it.

Thanks