webproclaim Posted October 26, 2006 Share Posted October 26, 2006 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 Column3c1r1value c2r1value c3r1valuec1r2value c2r2value c3r2valueI 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. Quote Link to comment Share on other sites More sharing options...
php_joe Posted October 26, 2006 Share Posted October 26, 2006 You could use [b]file_get_contents()[/b] to get the information and then use [b]explode()[/b] to seperate each column.Joe Quote Link to comment Share on other sites More sharing options...
Orio Posted October 26, 2006 Share Posted October 26, 2006 This can also be done nicely with regular expressions.Orio. Quote Link to comment Share on other sites More sharing options...
webproclaim Posted October 26, 2006 Author Share Posted October 26, 2006 php_joe...Do you have a quick example you could paste in here? Quote Link to comment Share on other sites More sharing options...
php_joe Posted October 26, 2006 Share Posted October 26, 2006 The text file is like this:Column1,Column2,Column3c1r1value,c2r1value,c3r1valuec1r2value,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 Quote Link to comment Share on other sites More sharing options...
webproclaim Posted October 26, 2006 Author Share Posted October 26, 2006 The following code produces the following error...Parse error: parse error, unexpected T_STRING in C:\Sites\mysite\testfolder\test.php on line 6I 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;?> Quote Link to comment Share on other sites More sharing options...
php_joe Posted October 27, 2006 Share Posted October 27, 2006 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 Quote Link to comment Share on other sites More sharing options...
btherl Posted October 27, 2006 Share Posted October 27, 2006 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. Quote Link to comment Share on other sites More sharing options...
php_joe Posted October 27, 2006 Share Posted October 27, 2006 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 Link to comment Share on other sites More sharing options...
php_joe Posted October 27, 2006 Share Posted October 27, 2006 [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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.