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

Link to comment
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

$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
Share on other sites

(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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

"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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.