Jump to content

Print Once While in Loop


richiejones24

Recommended Posts

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


}

Link to comment
Share on other sites

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.

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.