Jump to content

Noob Problem, Reading data from multiple text files.


CalebVenner

Recommended Posts

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

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>';
  }
}

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.

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()

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>';
    }
  }
}

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.

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?)

$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.

(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.

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.

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>';
  }
}

 

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 :)

"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 ^^

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.