Jump to content

Loop problem


lindm

Recommended Posts

Is it possible to execute a return function a certain times based on a variable containing a number. I don't want echo or anything so. This is my code that doesn't work:

 

<?php 
$file = fopen("formsaved.html", "w") or exit("Unable to open file!");  

function board($number) {for ($num=1; $num<=10; $num++ )  
{  
return "test";  
} } 





$stringData =  

' 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" /> 
<title>Untitled Document</title> 
</head> 

<body> 
<form id="form" name="form" method="post" action="writeform.php"> 
  <table width="610" border="0" cellspacing="0" cellpadding="0"> 
    <tr> 
      <td valign="top">'.board(5).'</td> 
     
    </tr> 
    </table> 
  
</form> 
</body> 
</html> 

' 


; 
fwrite($file, $stringData);   
fclose($file); 

header("Location: formsaved.html"); 


?>

 

In short. The above should return, $number of times...but doesn't per definition...any solutions?

 

The reason I use return and not echo is becuase the above code is part of a variable containing html code to be written to a file...

 

Link to comment
https://forums.phpfreaks.com/topic/67088-loop-problem/
Share on other sites

Doesn't seem to go for me. Below is modified code:

<?php 
$file = fopen("formsaved.html", "w") or exit("Unable to open file!");  

function board()
{
   return "test";
}






$stringData =  

' 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" /> 
<title>Untitled Document</title> 
</head> 

<body> 
<form id="form" name="form" method="post" action="writeform.php"> 
  <table width="610" border="0" cellspacing="0" cellpadding="0"> 
    <tr> 
      <td valign="top">'.for ($num=1; $num<=10; $num++)
   echo board();}.'</td> 
     
    </tr> 
    </table> 
  
</form> 
</body> 
</html> 

' 


; 
fwrite($file, $stringData);   
fclose($file); 

header("Location: formsaved.html"); 


?>

 

 

Link to comment
https://forums.phpfreaks.com/topic/67088-loop-problem/#findComment-336476
Share on other sites

echo doesn't return a string so you can't concatinate it to one. Same with for. Try:

$stringData =  

' 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" /> 
<title>Untitled Document</title> 
</head> 

<body> 
<form id="form" name="form" method="post" action="writeform.php"> 
  <table width="610" border="0" cellspacing="0" cellpadding="0"> 
    <tr> 
      <td valign="top">';
for ($num=1; $num<=10; $num++)
   $stringData .= board();

$stringData .=     </tr> 
    </table> 
  
</form> 
</body> 
</html> 

';

Link to comment
https://forums.phpfreaks.com/topic/67088-loop-problem/#findComment-336491
Share on other sites

It worked, just corrected a small ' missing after "$stringData .=". So it is not possible to have a single function for my problem that is called simply by '.function().'  ?

 

 

<?php 
$file = fopen("formsaved.html", "w") or exit("Unable to open file!");  

function board()
{
   return "test";
}






$stringData =  

' 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" /> 
<title>Untitled Document</title> 
</head> 

<body> 
<form id="form" name="form" method="post" action="writeform.php"> 
  <table width="610" border="0" cellspacing="0" cellpadding="0"> 
    <tr> 
      <td valign="top">';
for ($num=1; $num<=10; $num++)
   $stringData .= board();

$stringData .= '    </tr> 
    </table> 
  
</form> 
</body> 
</html> 
'; 
fwrite($file, $stringData);   
fclose($file); 

header("Location: formsaved.html"); 


?>

 

 

Link to comment
https://forums.phpfreaks.com/topic/67088-loop-problem/#findComment-336534
Share on other sites

You can have a single function return that entire string, but not multiple ones. For example:

function board($num)
{
$stringData =  

' 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" /> 
<title>Untitled Document</title> 
</head> 

<body> 
<form id="form" name="form" method="post" action="writeform.php"> 
  <table width="610" border="0" cellspacing="0" cellpadding="0"> 
    <tr> 
      <td valign="top">';
for ($i=1; $i<=$num; $i++)
   $stringData .= "test";

$stringData .= '    </tr> 
    </table> 
  
</form> 
</body> 
</html> 
';
}

echo board(10);

 

That returns the big string with the "test" string repeated as many times as you pass to the function.

Link to comment
https://forums.phpfreaks.com/topic/67088-loop-problem/#findComment-336574
Share on other sites

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.