Jump to content

Can you add a for loop into an fwrite function?


blurredvision

Recommended Posts

My main goal is to have a form that a user fills out which will write one line to a text file.  The user then has an option to repeat that line of code however many time he/she wants, which will create a new line and repeat the same text.  (This is for a program I use at work.)

 

Right now, I simply have a for loop that continually opens the handler (marked with an 'a' to put the pointer at the end) and appends the same line however many times the user specifies.  However, I'm running into an issue where I want to use a for loop as part of the argument of the fwrite function.

 

The code I tried is:

 

fwrite($fh, (for(*; *; *$variable1 . "," . $variable2));

 

The asterisks in the for loop are obviously my specifications, just placeholders for this demonstration.

 

I also tried wrapping the for loop in braces, but that didn't work either.

 

I'm still learning php, so forgive me for any cloudy discriptions or lack of more detail.

kenrbnsn,

 

That's exactly how I have it set up right now.  Guess I'll have to figure out something else, then, thanks.

 

Quick second question now that the first has been answered.  Is there a way to specify what line the file pointer goes to, instead of the simple "r", "w", and "a" modes?  If so, this would also help me achieve what I'm trying to do, but I'm guessing you can't :).

 

Thanks.

@blurredvision: You aren't reopening the file every time with kenrbnsn's way.

 

And you can try using fseek().

 

Ahh, ok.  I thought you were reopening the file, thanks for clearing that up.

 

I'll also check out fseek and see what it can do, thanks.

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.