FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


PHP - Reading Data





n0obie4life
So, I have a file called "abcdefgh.txt".

The file is parsed this way

Code:

1 1234321 123.456.789.0
1 145784523 435.765.123.56
2 123468032 192.168.0.1


The first row is the "id", second is "time", third is "ip". So, in my table HTML file, i have 3 collums (yeah i got the spelling wrong). I need each item to be put in the respective collum.
Sappho
Dunno if i understood it but to me it seems that all u need is to read the file line by line and split each line with preg_split with '/ /' as pattern so it will slice the string for u to array where [0] will be id, [1] will be time and [2] will be ip.
JustaMin
Sappho wrote:
Dunno if i understood it but to me it seems that all u need is to read the file line by line and split each line with preg_split with '/ /' as pattern so it will slice the string for u to array where [0] will be id, [1] will be time and [2] will be ip.


That's how i'd do it Wink
Nyizsa
Ok, I think you are after something like this:
Code:

...
<table>
<thead>
<tr>
<td>id</td>
<td>time</td>
<td>ip</td>
</tr>
</thead>
<?php
$abclist=file('abcdefgh.txt');
foreach ($abclist as $abcitem)
{
$current=explode(' ',$abcitem);         //The first parameter is the divider, eg. space
echo ('<tr>\n');
echo ('<td>'.$current[0].'</td>\n');
echo ('<td>'.$current[1].'</td>\n');
echo ('<td>'.$current[2].'</td>\n');
echo ('</tr>\n');
}
?>
</table>

I didn't test it actually, but it should work. Give it a try, and good luck!
mjerom
yep
In a given line of your file, items are separeted by a space :" "
The function explode(' ', $aLine) will put all the items in an array
if tour file is
1 1234321 123.456.789.0
1 145784523 435.765.123.56
the previous code will give you :
array (1, 1234321, 123.456.789.0, 1 , 145784523, 435.765.123.56)

I'd go that way too !

Take a look at the implode function too...
n0obie4life
Hmm, thanks for all your suggestions. I'll try them out Wink.
Nyizsa
mjerom wrote:
The function explode(' ', $aLine) will put all the items in an array

No.
Code:
$abclist=file('abcdefgh.txt');

will put the whole file into an array, 1 line = 1 item. For example:
Code:
$abclist[1]=='1 145784523 435.765.123.56'        //True

In the loop I get every item, and explode them using a space as a divider. So
Code:
$abcitem[1]=='145784523'       //True

I hope it helped.
BearClaw
i would reccommend using the explode() function as well. when mysql databases aren't available thats how i encode my text files to serve as them. it works quite well.
Related topics
PEAR path
[PhP] News Posting Tutorial (code, actually ^^')
Can somebody do me a help?
A very good PHP MySQL Tutorial
[GameUnleashed]Play games online
Any gaming or clan sites
styling text string read from plain text file
PHP reading a line?
cannot add data to mysql from PHP form
fopen in server frihost?
php reading
PHP e XML
SQL doesn't add data [SOLVED]
Fractals: Using them in a web page. I just finished.
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.