CalebVenner Posted June 17, 2009 Share Posted June 17, 2009 I don't know why this is not working, I also do not know where to go from here. <html> <head> <title>File read demo</title> </head> <body> <?PHP for($FileNumber = 1; file_exists("orders/$FolderDate/Order #$FileNumber.txt"); $FileNumber++) { $file_handle = fopen("Order #$FileNumber.txt", "rb"); while (!feof($file_handle) ) { $line_of_text = fgets($file_handle); $parts = explode('=', $line_of_text); print $parts[0] . $parts[1]. "<BR>"; } fclose($file_handle); } ?> </body> </html> Thanks in advance for any help. Fixed the unassigned variable $FolderDate v.v Link to comment https://forums.phpfreaks.com/topic/162544-noob-problem-reading-data-from-multiple-text-files/ Share on other sites More sharing options...
Yesideez Posted June 17, 2009 Share Posted June 17, 2009 Seems you've got your logic in a bit of a twist. The file_exists() function tells you whether a file exists or not and returns either true or false so you're better off using that in an if() outside of your for() loop... if (file_exists("orders/$FolderDate/Order #$FileNumber.txt")) { $data=file("orders/$FolderDate/Order #$FileNumber.txt"); foreach ($data as $d) { $parts=explode('=',$d); echo $parts[0].$parts[1].'<br>'; } } Link to comment https://forums.phpfreaks.com/topic/162544-noob-problem-reading-data-from-multiple-text-files/#findComment-857856 Share on other sites More sharing options...
CalebVenner Posted June 17, 2009 Author Share Posted June 17, 2009 You lost me there, I meant noob when I said noob ^.^ The problem I am facing is that I have mutliple files named Order #1 - Order #100 (not that many though) And I am trying to display them on a webpage in an easy to read manner. I just can't get my head around how to make the variable of the files number increment and read for each file. Thanks for your help. Link to comment https://forums.phpfreaks.com/topic/162544-noob-problem-reading-data-from-multiple-text-files/#findComment-857858 Share on other sites More sharing options...
Yesideez Posted June 17, 2009 Share Posted June 17, 2009 OK let's go through what I've done line by line... if (file_exists("orders/$FolderDate/Order #$FileNumber.txt")) { This checks if the file in question exists - if it does, it executes the code inside the braces. $data=file("orders/$FolderDate/Order #$FileNumber.txt"); Because our previous if() turned out the file DID exist this reads the entire file into $data (as an array) and each line is a separate element. foreach ($data as $d) { This foreach() loops reads through the $data array and assigns each element in turn as $d $parts=explode('=',$d); This explodes the current element (aka. line) and splits on the "=" echo $parts[0].$parts[1].'<br>'; This echo's out elements 0 and 1 followed by a line break. } This ends the foreach() loop. } This ends the if() Link to comment https://forums.phpfreaks.com/topic/162544-noob-problem-reading-data-from-multiple-text-files/#findComment-857862 Share on other sites More sharing options...
Yesideez Posted June 17, 2009 Share Posted June 17, 2009 The only thing I've missed off (accidentally!) is a loop to count from 1 to 100 so the final code would look like this: for ($FileNumber=1;$FileNumber<=100;++$FileNumber) { if (file_exists("orders/$FolderDate/Order #$FileNumber.txt")) { $data=file("orders/$FolderDate/Order #$FileNumber.txt"); foreach ($data as $d) { $parts=explode('=',$d); echo $parts[0].$parts[1].'<br>'; } } } Link to comment https://forums.phpfreaks.com/topic/162544-noob-problem-reading-data-from-multiple-text-files/#findComment-857863 Share on other sites More sharing options...
CalebVenner Posted June 17, 2009 Author Share Posted June 17, 2009 Ok, thanks, I understand that now. Although I am still faced with the dillema of not knowing how to increment the variable $FileNumber, then output them, until no more files exist :s Its just due to the nature of the files they will be increasing or decreasing. So there will be no deffinite number known to me. Link to comment https://forums.phpfreaks.com/topic/162544-noob-problem-reading-data-from-multiple-text-files/#findComment-857864 Share on other sites More sharing options...
Yesideez Posted June 17, 2009 Share Posted June 17, 2009 Although I am still faced with the dillema of not knowing how to increment the variable $FileNumber, then output them, until no more files exist :s See my above code - I just realised I missed that out and added it. Link to comment https://forums.phpfreaks.com/topic/162544-noob-problem-reading-data-from-multiple-text-files/#findComment-857866 Share on other sites More sharing options...
CalebVenner Posted June 17, 2009 Author Share Posted June 17, 2009 Edited my last post, what I am working on is a little system (for educational reasons) where waiter/waitresses have little hand held devices for taking orders that are saved into text documents which i then want to display on a moniter in the kitchen area. So the number of files will be increasing with no exact number set. Hope you havn't replied while i was typing this:p Sorry I was not clearer in my problem earlier on. (Or Can I just have the for statement set to 100, or will that mean the code wont work if it cannot find anymore files, will it return an error?) Link to comment https://forums.phpfreaks.com/topic/162544-noob-problem-reading-data-from-multiple-text-files/#findComment-857869 Share on other sites More sharing options...
Yesideez Posted June 17, 2009 Share Posted June 17, 2009 $FileNumber=1; while (file_exists("orders/$FolderDate/Order #$FileNumber.txt") { $data=file("orders/$FolderDate/Order #$FileNumber.txt"); foreach ($data as $d) { $parts=explode('=',$d); echo $parts[0].$parts[1].'<br>'; } $FileNumber++; } You can try that, not sure if it would work as I've not tested it but it gives the basic idea of what you're looking for. Link to comment https://forums.phpfreaks.com/topic/162544-noob-problem-reading-data-from-multiple-text-files/#findComment-857872 Share on other sites More sharing options...
Yesideez Posted June 17, 2009 Share Posted June 17, 2009 (Or Can I just have the for statement set to 100, or will that mean the code wont work if it cannot find anymore files, will it return an error?) If you leave the for() loop set to 100 if the files don't exist you shouldn't receive an error but it depends on how error reporting is set up. Link to comment https://forums.phpfreaks.com/topic/162544-noob-problem-reading-data-from-multiple-text-files/#findComment-857875 Share on other sites More sharing options...
CalebVenner Posted June 17, 2009 Author Share Posted June 17, 2009 Hmmm, neither seem to be working for me, with the webpage (localserver setup) remaining blank but the webpage title being desplayed. And there is something wrong with my PHP or Apache setup where no errors are ever reported. Thanks heaps for your time and help, I appreciate it. Link to comment https://forums.phpfreaks.com/topic/162544-noob-problem-reading-data-from-multiple-text-files/#findComment-857877 Share on other sites More sharing options...
Yesideez Posted June 17, 2009 Share Posted June 17, 2009 Try adding this at the very start of the script to turn on all error reporting and see how it goes. error_reporting(E_ALL); ini_set('display_errors', 1); (Thanks to Mark Baker's signature for a quick look-up there!) Link to comment https://forums.phpfreaks.com/topic/162544-noob-problem-reading-data-from-multiple-text-files/#findComment-857880 Share on other sites More sharing options...
CalebVenner Posted June 17, 2009 Author Share Posted June 17, 2009 No luck :S. Could it be a problem in a config file somewhere (the error reporting)? Link to comment https://forums.phpfreaks.com/topic/162544-noob-problem-reading-data-from-multiple-text-files/#findComment-857883 Share on other sites More sharing options...
Yesideez Posted June 17, 2009 Share Posted June 17, 2009 Try this: for ($FileNumber=1;$FileNumber<=100;++$FileNumber) { if (file_exists("orders/$FolderDate/Order #$FileNumber.txt")) { $data=file("orders/$FolderDate/Order #$FileNumber.txt"); foreach ($data as $d) { $parts=explode('=',$d); echo $parts[0].$parts[1].'<br>'; } } else { echo 'File orders/'.$FolderDate.'/Order #'.$FileNumber.'.txt not found<br>'; } } Link to comment https://forums.phpfreaks.com/topic/162544-noob-problem-reading-data-from-multiple-text-files/#findComment-857889 Share on other sites More sharing options...
CalebVenner Posted June 17, 2009 Author Share Posted June 17, 2009 error_reporting(E_ALL); ini_set('display_errors', 1); File orders//Order #1.txt not found File orders//Order #3.txt not found File orders//Order #5.txt not found File orders//Order #7.txt not found File orders//Order #9.txt not found Up to 99, every odd number, so there is a problem there, I think I will remove the "#" from the file names as it could cause problems. Thanks Link to comment https://forums.phpfreaks.com/topic/162544-noob-problem-reading-data-from-multiple-text-files/#findComment-857899 Share on other sites More sharing options...
CalebVenner Posted June 17, 2009 Author Share Posted June 17, 2009 "Notice: Undefined offset: 1 in C:\PHP Root\Restaurant\DisplayOrder.php on line 22 Patron's Name: Caleb Venner Notice: Undefined offset: 1 in C:\PHP Root\Restaurant\DisplayOrder.php on line 22 Notice: Undefined offset: 1 in C:\PHP Root\Restaurant\DisplayOrder.php on line 22 Room: Cafe Notice: Undefined offset: 1 in C:\PHP Root\Restaurant\DisplayOrder.php on line 22 Notice: Undefined offset: 1 in C:\PHP Root\Restaurant\DisplayOrder.php on line 22 Table Number: 20 " The text in the files are indented using "/r/n" which is not campatible with PHP maybe? echo $parts[0].$parts[1].'<br>'; Line 22 ^^ Link to comment https://forums.phpfreaks.com/topic/162544-noob-problem-reading-data-from-multiple-text-files/#findComment-857902 Share on other sites More sharing options...
kicki Posted June 17, 2009 Share Posted June 17, 2009 If I got the filename like this, what should I do..... c:/test/audreco20090612084567.txt c:/test/audreco20090612084630.txt c:/test/audreco20090612084639.txt c:/test/audreco20090612084450.txt Link to comment https://forums.phpfreaks.com/topic/162544-noob-problem-reading-data-from-multiple-text-files/#findComment-857930 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.