Jump to content

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

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.