Flatfile-database

 
Post new topic   Reply to topic    Aprelium Forum Index -> General Questions
View previous topic :: View next topic  
Author Message
cmxflash
-


Joined: 11 Dec 2004
Posts: 872

PostPosted: Sat Oct 22, 2005 12:20 pm    Post subject: Flatfile-database Reply with quote

I've been working on a small website, that uses one flatfile-database.
I use this script to make PHP read the db.txt-file:


Code:
$open = fopen("db.txt", "r");
$file=fgets($open,1024);
fclose($open);
if ($file == ""){
   echo "<b>Error.</b><hr>";
} else {
echo $file;
}


This script will only output the first line of the database. How do I make it output everything in the database?
Back to top View user's profile Send private message
MonkeyNation
-


Joined: 05 Feb 2005
Posts: 921
Location: Cardiff

PostPosted: Sat Oct 22, 2005 1:47 pm    Post subject: Reply with quote

Code:
/* Probably don't need the explode. Can't be arsed to put it into theory. */
foreach(explode("\n", implode("", file("db.txt"))) as $this_key => $this_value) {
  echo $this_value;
}

Or something, probably. Don't use file wrappers all that much, MySQL 4 lyfe.
_________________
Back to top View user's profile Send private message AIM Address Yahoo Messenger MSN Messenger ICQ Number
TRUSTAbyss
-


Joined: 29 Oct 2003
Posts: 3752
Location: USA, GA

PostPosted: Sat Oct 22, 2005 10:38 pm    Post subject: Reply with quote

This will loop through the entire db.txt file and output the results. If the
file is empty , I could guess that you want an Error to be printed. LateR!

Note: This will also go to Error , if db.txt does not exist.

Code:

<?php
if (@filesize("db.txt") != 0) {
 $open = fopen("db.txt", "r");

 while (!feof($open)) {
  $file = fgets($open, 1024);

  echo $file;
 }
 fclose($open);
}else{
 echo "<b>Error.</b><hr>";
}
?>


Note: If you have a huge file , the file() function would open that entire
file all at once and can slow down performance , this code uses a buffer.

Sincerely , TRUSTpunk


Last edited by TRUSTAbyss on Sat Oct 22, 2005 11:54 pm; edited 16 times in total
Back to top View user's profile Send private message Visit poster's website
roganty
-


Joined: 08 Jun 2004
Posts: 357
Location: Bristol, UK

PostPosted: Sat Oct 22, 2005 10:44 pm    Post subject: Re: Flatfile-database Reply with quote

the file() function reads the file into an array


Code:
$open = fopen("db.txt", "r");

$fileArray=file($open);

fclose($open);

if(! is_array($fileArray) ){
 echo "<b>Error.</b><hr>";
}else{
 foreach( $fileArray as $val )
  print "<br />$val";
}

_________________
Anthony R

Roganty
| Links-Links.co.uk
Back to top View user's profile Send private message Visit poster's website
TRUSTAbyss
-


Joined: 29 Oct 2003
Posts: 3752
Location: USA, GA

PostPosted: Sat Oct 22, 2005 11:16 pm    Post subject: Reply with quote

I edited my last post to show you why the file() function can be a bad
option to use. Reading a file into chunks can give better performance.

Sincerely , TRUSTpunk


Last edited by TRUSTAbyss on Sun Oct 23, 2005 12:00 am; edited 3 times in total
Back to top View user's profile Send private message Visit poster's website
MonkeyNation
-


Joined: 05 Feb 2005
Posts: 921
Location: Cardiff

PostPosted: Sat Oct 22, 2005 11:42 pm    Post subject: Re: Flatfile-database Reply with quote

roganty wrote:
the file() function reads the file into an array


Code:
$open = fopen("db.txt", "r");

$fileArray=file($open);

fclose($open);

if(! is_array($fileArray) ){
 echo "<b>Error.</b><hr>";
}else{
 foreach( $fileArray as $val )
  print "<br />$val";
}


Kinda what I did, but you don't need to use fopen etc, you can just file().

TRUSTpunk -> I'd speculate that it probably isn't, but if it is I doubt it would be that much of a difference.
_________________
Back to top View user's profile Send private message AIM Address Yahoo Messenger MSN Messenger ICQ Number
cmxflash
-


Joined: 11 Dec 2004
Posts: 872

PostPosted: Sun Oct 23, 2005 12:47 am    Post subject: Reply with quote

Correct me if I'm wrong, but this should read the file and output it exactly as it is, right?

Code:

$n = file_get_contents("file.txt");
echo $n;
Back to top View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Aprelium Forum Index -> General Questions All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB phpBB Group