Jump to content

Reading from Text File as a database


webproclaim

Recommended Posts

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.
Link to comment
https://forums.phpfreaks.com/topic/25204-reading-from-text-file-as-a-database/
Share on other sites

The text file is like this:

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

[code]$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];
}[/code]

$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
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;
?>
No, it's not your file, it's my code.

if you use this:
[code]$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[#])
}[/code]
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
You can use an entire variable as a variable name like this:

[code]$varname = 'brian';
$$varname = "Hello world\n";
print $brian;[/code]

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.
There you go then :)

[code]$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>";
[/code]
[quote author=btherl link=topic=112824.msg458424#msg458424 date=1161923525]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.
[/quote]

It works, but my mistake was that you can't do it before the = sign, but it's ok to have it afterwards.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.