Jump to content


Photo

Reading from Text File as a database


  • Please log in to reply
9 replies to this topic

#1 webproclaim

webproclaim
  • Members
  • PipPip
  • Member
  • 15 posts

Posted 26 October 2006 - 05:12 PM

I need some help regarding how to read from a comma delimited text file as if it were a database.

Suppose I had a file called file.txt with the following contents...

Column1 Column2 Column3
c1r1value c2r1value c3r1value
c1r2value c2r2value c3r2value


I know how to do this with MySQL but not with a text file. I dont merely want to read the file to the page but be able to connect to it as a database and selectively read column values. Any help would be greatly appreciated.

#2 php_joe

php_joe
  • Members
  • PipPipPip
  • Advanced Member
  • 175 posts

Posted 26 October 2006 - 05:23 PM

You could use file_get_contents() to get the information and then use explode() to seperate each column.

Joe
[url=http://darkswordsguide.com][/url]

#3 Orio

Orio
  • Staff Alumni
  • Advanced Member
  • 2,491 posts

Posted 26 October 2006 - 05:27 PM

This can also be done nicely with regular expressions.

Orio.
Think you're smarty?

(Gone until 20 to November)

#4 webproclaim

webproclaim
  • Members
  • PipPip
  • Member
  • 15 posts

Posted 26 October 2006 - 05:28 PM

php_joe...Do you have a quick example you could paste in here?

#5 php_joe

php_joe
  • Members
  • PipPipPip
  • Advanced Member
  • 175 posts

Posted 26 October 2006 - 05:40 PM

The text file is like this:

Column1,Column2,Column3
c1r1value,c2r1value,c3r1value
c1r2value,c2r2value,c3r2value

$file = file_get_contents("database.txt");
$lines = explode("\n", $file);
foreach($lines as $key => $value){
$pieces = explode(",", $lines);
$row_{$key}_1 = $pieces[0];
$row_{$key}_2 = $pieces[1];
$row_{$key}_3 = $pieces[2];
}

$row_0_1 = Column1
$row_0_2 = Column2
$row_0_3 = Column3
$row_1_1 = c1r1value
$row_1_2 = c2r1value
$row_1_3 = c3r1value
$row_2_1 = c1r2value
$row_2_2 = c2r2value
$row_2_3 = c3r2value

[url=http://darkswordsguide.com][/url]

#6 webproclaim

webproclaim
  • Members
  • PipPip
  • Member
  • 15 posts

Posted 26 October 2006 - 07:50 PM

The following code produces the following error...

Parse error: parse error, unexpected T_STRING in C:\Sites\mysite\testfolder\test.php on line 6

I changed the database.txt file to contain commas instead of tabs and I still get this error. Any ideas?

<?php
$file = file_get_contents("database.txt");
$lines = explode("\n", $file);
foreach($lines as $key => $value){
$pieces = explode(",", $lines);
$row_{$key}_1 = $pieces[0];
$row_{$key}_2 = $pieces[1];
$row_{$key}_3 = $pieces[2];
}

echo $row_1_2;
?>

#7 php_joe

php_joe
  • Members
  • PipPipPip
  • Advanced Member
  • 175 posts

Posted 27 October 2006 - 04:13 AM

No, it's not your file, it's my code.

if you use this:
$file = file_get_contents("test.txt");
$lines = explode("\n", $file);
foreach($lines as $key => $value){
//Play with each line in here ($lines[#])
$pieces = explode(',', $value);
//Play with each piece of information in here ($pieces[#])
}
Then you can modify each piece of information, but I'm not sure how to assign a unique variable to each piece. Maybe someone else can help you.

Joe
[url=http://darkswordsguide.com][/url]

#8 btherl

btherl
  • Staff Alumni
  • Advanced Member
  • 3,893 posts
  • LocationAustralia

Posted 27 October 2006 - 04:32 AM

You can use an entire variable as a variable name like this:

$varname = 'brian';
$$varname = "Hello world\n";
print $brian;

But I don't think there's any way to make the variable name inline, like $var_{$othervar}_1.  $name = 'var_{$othervar}_1'; $$name = 'val'; will work though.

#9 php_joe

php_joe
  • Members
  • PipPipPip
  • Advanced Member
  • 175 posts

Posted 27 October 2006 - 04:47 AM

There you go then :)

$file = file_get_contents("database.txt");
$lines = explode("\n", $file);
foreach($lines as $key => $value){
$row = "row_{$key}";
$$row = $value;
$$row = explode(',', $value);
}

echo "$row_0[0]<br>";
echo "$row_0[1]<br>";
echo "$row_0[2]<br>";
echo "$row_1[0]<br>";
echo "$row_1[1]<br>";
echo "$row_1[2]<br>";
echo "$row_2[0]<br>";
echo "$row_2[1]<br>";
echo "$row_2[2]<br>";

[url=http://darkswordsguide.com][/url]

#10 php_joe

php_joe
  • Members
  • PipPipPip
  • Advanced Member
  • 175 posts

Posted 27 October 2006 - 04:49 AM

But I don't think there's any way to make the variable name inline, like $var_{$othervar}_1.  $name = 'var_{$othervar}_1'; $$name = 'val'; will work though.


It works, but my mistake was that you can't do it before the = sign, but it's ok to have it afterwards.
[url=http://darkswordsguide.com][/url]




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users