blurredvision Posted May 27, 2008 Share Posted May 27, 2008 I'm wanting to insert a for loop inside of an fwrite function, but I can't seem to get it to work. It keeps telling me there was an unexpected for loop. Can this be done, and if so, could someone help me with the syntax? Thanks. Link to comment https://forums.phpfreaks.com/topic/107496-can-you-add-a-for-loop-into-an-fwrite-function/ Share on other sites More sharing options...
jonsjava Posted May 27, 2008 Share Posted May 27, 2008 could you post the code? then answer can be yes or no, depending on where you are putting the for loop. Link to comment https://forums.phpfreaks.com/topic/107496-can-you-add-a-for-loop-into-an-fwrite-function/#findComment-551000 Share on other sites More sharing options...
blurredvision Posted May 27, 2008 Author Share Posted May 27, 2008 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. Link to comment https://forums.phpfreaks.com/topic/107496-can-you-add-a-for-loop-into-an-fwrite-function/#findComment-551030 Share on other sites More sharing options...
kenrbnsn Posted May 27, 2008 Share Posted May 27, 2008 You can't put a for loop into an fwrite. You can do this: <?php $fp = fopen('yourfilehere','a'); for($i=0;$i<$usercount;$i++) fwrite($fp, $variable1 . ',' . variable2 . "\r\n"); fclose($fp); ?> Ken Link to comment https://forums.phpfreaks.com/topic/107496-can-you-add-a-for-loop-into-an-fwrite-function/#findComment-551036 Share on other sites More sharing options...
blurredvision Posted May 27, 2008 Author Share Posted May 27, 2008 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. Link to comment https://forums.phpfreaks.com/topic/107496-can-you-add-a-for-loop-into-an-fwrite-function/#findComment-551042 Share on other sites More sharing options...
DarkWater Posted May 27, 2008 Share Posted May 27, 2008 @blurredvision: You aren't reopening the file every time with kenrbnsn's way. And you can try using fseek(). Link to comment https://forums.phpfreaks.com/topic/107496-can-you-add-a-for-loop-into-an-fwrite-function/#findComment-551052 Share on other sites More sharing options...
blurredvision Posted May 27, 2008 Author Share Posted May 27, 2008 @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. Link to comment https://forums.phpfreaks.com/topic/107496-can-you-add-a-for-loop-into-an-fwrite-function/#findComment-551056 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.