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
https://forums.phpfreaks.com/topic/249734-print-once-while-in-loop/
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.

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.