Page 1 of 1

reading contents of a file and splitting into 3's

Posted: Tue Aug 19, 2003 12:20 pm
by like_duh44
Hi, i have a php file that i want to get the contents of a file. The file is arranged like this:

Username
Post
Time
Username2
Post2
Time2
etc
etc
etc

What kind of script would I need to get the file info in like 3's? LIke somehow making a 'Foreach set of 3' do this blablabla

Thanks

Posted: Tue Aug 19, 2003 1:25 pm
by evilmonkey
Well, you'd need to cycle it using a for (or foreach) loop. I don't know exactly how php handles these files, but another programming language I know handles them like this:

user post time
user post time
user post time
...

and then you cycle like this:

Code: Select all

//open your file with the var $file
for (i=0; i++) {
echo $user." ".$post." ".$time."<br>";
if (feof($file)){
break;}
}
Or something like that. Once again, I don't know if tht structure will work with PHP, but that's the overall idea.

Posted: Tue Aug 19, 2003 1:40 pm
by xisle
you may want to think about storing within xml type tags for greater expansion. You may want to use the text file elsewhere or add more fields later.

here's a sample:

Code: Select all

<record>1030503605
<date>1032926400</date>
<event>event description2</event>
<address1>address2</address1>
<city>city2</city>
<state>state2</state>
<time>time2</time>
</record>
<record>1030503690
<date>1036126800</date>
<event>event 3</event>
<address1>address3</address1>
<city>city3</city>
<state>state3</state>
<time>time3</time>
</record>
parse it for what you need with a switch and ditch the tags on output.

Code: Select all

function replace($field, $line)&#123;
	$val=str_replace("<$field>", "", $line);
	$val=str_replace("</$field>", "", $val);
 	return $val;
&#125;

	switch($line)&#123;
	   case strstr($line, "<event>"):
	   $event=replace('event', $line);
	   $event=trim($event);
	   if($event !="")
	   $out&#1111;$date].="<br>$event";
	   break;
	   
	   case strstr($line, "<address1>"):
	   $address1=replace('address1', $line);
	   $address1=trim($address1);
	   if($address1 !="")
	   $out&#1111;$date].="<br>$address1";
	   break;

	   case strstr($line, "<city>"):
	   $city=replace('city', $line);
	   $city=trim($city);
	   if($city !="")
	   $out&#1111;$date].="<br>$city,";
	   break;	   
	   
	   case strstr($line, "</record>"):
	   $out&#1111;$date].="<p>";
	&#125;
have fun..

Posted: Wed Aug 20, 2003 9:29 am
by like_duh44
Thank you Xisle. I'm not quite ready for xml LOL. Maybe in my next version (I'm coding a chat [a mysql and non mysql], check it out at http://www.vgapws1.myhost24.com/aatachat.php). I might use xml a little later (sounds like a great idea, never tought of it), but for now, i figured it out:

Code: Select all

<?php
$posts = file("posts.txt");
$count = (count($posts));
$i = $count;

while ($i != "0")
{
     $posttext = $posts[$i-3];
     $postuser = $posts[$i-2];
     $posttime = $posts[$i-1];

     echo "$posttime GMT  $postuser Says: $posttext";

     $i = $i - 3;
}
?>