richiejones24 Posted October 24, 2011 Share Posted October 24, 2011 I am trying to get the code at the bottom of the script to print just once during the loop but it either doesn't print at all or repeats with the loop im am using if (!$i++) to print once and i works the first time i use it. foreach($uploadFilename as $key => $myvar) { if (!$i++) print "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\"> <html xmlns=\"http://www.w3.org/1999/xhtml\"> <head> <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" /> <title>Upload Complete....</title> </head> <body> <body onload=\"document.forms.formname.submit);\"> <form id=\"formname\" name=\"form1\" method=\"post\" action=\"reg5.php\">\n"; echo "<input type=\"hidden\" name=\"image$key\" value=\""; echo end(explode('/',$myvar)); echo "\">\n"; if (!$i++) print "</form> </body> </html>\n"; } Quote Link to comment https://forums.phpfreaks.com/topic/249734-print-once-while-in-loop/ Share on other sites More sharing options...
xyph Posted October 24, 2011 Share Posted October 24, 2011 Why not simplify this? <?php $i = 0; while( your conditions ) { if( $i == 0 ) { // This will perform once } $i++; } ?> Or just echo outside of the loop <?php echo 'before the loop'; while( your conditions ) { echo 'in the loop'; } echo 'after the loop'; ?> If you echo $i in your loop you'll understand why it's not working. Quote Link to comment https://forums.phpfreaks.com/topic/249734-print-once-while-in-loop/#findComment-1281865 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.