printing in a function

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
C_Calav
Forum Contributor
Posts: 395
Joined: Wed Jun 02, 2004 10:55 pm
Location: New Zealand

printing in a function

Post by C_Calav »

hi guys, i posted a while back about a heredoc function type thing viewtopic.php?t=26968&highlight= and now i can get html working.

Code: Select all

function print_form()
{
echo <<<STOP

<html>
<body>

code

</body>
</html>

STOP;
}
how do i put for example something line this in?

Code: Select all

<?php
echo $C_First=$_POST['C_First']; 
?>
thanx!
swdev
Forum Commoner
Posts: 59
Joined: Mon Oct 25, 2004 8:04 am

Post by swdev »

Hi C_Calav

Simply put the variables you want printed out in curly braces like this

Code: Select all

function print_form()
{
$var1 = 'Hello World';
$var2 = 27;

echo <<<STOP

<html>
<body>

<p>{$var1}</p>

<p>{$var2}</p>


</body>
</html>

STOP;
}
Hope this helps
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

Well if you want the herdoc outputted as a return variable:

Code: Select all

function print_form()
{
$var = <<<STOP

<html>
<body>

code

</body>
</html>

STOP;

return $var;
}
It is explained here: http://www.php.net/types.string
User avatar
C_Calav
Forum Contributor
Posts: 395
Joined: Wed Jun 02, 2004 10:55 pm
Location: New Zealand

Post by C_Calav »

cheers guys! that looks like what i want! will try out when i get home from work.
LostMyLove
Forum Newbie
Posts: 20
Joined: Mon Sep 27, 2004 12:20 pm

Post by LostMyLove »

Code: Select all

function print_form()
 {
print <<<STOP

<html>
<body>

code

</body>
</html>

STOP

}
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

Post by nigma »

In regards to using echo and prints heredoc syntax you don't need to put braces around the variables because of variable interpolation. See below links for more information:

http://www.php.net/echo
http://www.php.net/print
User avatar
C_Calav
Forum Contributor
Posts: 395
Joined: Wed Jun 02, 2004 10:55 pm
Location: New Zealand

Post by C_Calav »

thanx guys.. all going well so far! usuing "swdev" method.

im having problems trying to get this to work. do i format the number first or....?

thanx guys!

Code: Select all

<?php echo number_format($row&#1111;"P_Price"], 2, ".", ","); ?>
User avatar
C_Calav
Forum Contributor
Posts: 395
Joined: Wed Jun 02, 2004 10:55 pm
Location: New Zealand

Post by C_Calav »

ok guys,, getting a bit tricky now with this while loop. ill post my code for ease of seeing what im doing. anyone help me out if im on the right track?

now im getting a
Parse error: parse error, unexpected $
at the end of the page (which is at the end of the next function {not showen here})

so i believe im missing something or doing something wrong!

any help would be great :)

Code: Select all

echo <<<STOP
    
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html>
<head>
<title>Model Aircraft</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<style type="text/css" media="all">
	@import "model.css";
	</style>
			
</head>
<body>
<div id="container"><h1>Models</h1>

<!-- Begin Content -->
<div id="content">

<form method="post">

<h2>Summary</h2>

<p class="text"> 

<table width="500">
      <!--DWLayoutTable-->
      <tr> 
        <td width="80" height="21" valign="top">First Name:</td>
        <td width="135" valign="top">&#123;$C_First&#125;</td>
        <td width="91" valign="top">Last Name: </td>	
	<td width="172" valign="top">&#123;$C_Last&#125;</td>
      </tr>
      <tr> 
        <td height="21" valign="top">Email:</td>
        <td valign="top">&#123;$C_Email&#125;</td>
        <td valign="top">Phone:</td>
        <td valign="top">&#123;$C_Phone&#125;</td>
      </tr>
      <tr> 
        <td height="21" valign="top">Address 1:</td>   
  	<td valign="top">&#123;$C_Address1&#125;</td>
        <td valign="top">Address 2:</td>  
  	<td valign="top">&#123;$C_Address2&#125;</td>
      </tr>
      <tr> 
        <td height="21" valign="top">Country:</td>
	<td valign="top">&#123;$C_Country&#125;</td>
        <td valign="top">City:</td>
        <td valign="top">&#123;$C_City&#125;</td>
      </tr>
</table>
	
<br />
	
<table width="450">
  <tr>
     <td width ="100">Select Zone:</td>
     <td>&#123;$C_Freight&#125;</td>
  </tr>
		
</table>

<br /> 
 
<table width="500">
  <tr> 
        <td width="91" height="40" valign="top"><strong>Aircraft</strong></td>
        <td width="105" valign="top"><strong>Name</strong></td>
        <td colspan="2" valign="top"><strong>Price Ea</strong></td>
        <td width="51" align="left" valign="top"><strong>Qty</strong></td>
        <td colspan="2" valign="top"><strong>Sub-Total</strong></td>
  </tr>
   
STOP;

     while($row = mysql_fetch_array($result))
     &#123; 
     $totalCost += ($row&#1111;"qty"] * $row&#1111;"P_Price"]); 

echo <<<STOP

      <tr> 
        <td height="64" valign="top"><img src=http://www.modelaircraft.co.nz/Pics/&#123;$row&#1111;"P_Stock"]&#125;.jpg alt="image of plane" width="82" height="62" class="planeimage" /></td>
	<td>&#123;$row&#1111;"P_Name"]&#125;</td>
    	<td colspan="2" valign="middle">$&#123;$row&#1111;"P_Price"]&#125;</td>
   	 <td align="left" valign="middle">&#123;$row&#1111;"qty"]&#125;</td>
   	 <td colspan="2" valign="middle"> $ </td>
      </tr>

STOP; 
	&#125; //end loop

echo <<<STOP

      <tr> 
        <td height="21" colspan="6" valign="top"><hr size="1" color="#666666" NOSHADE /></td>
        <td width="66" valign="top"><!--DWLayoutEmptyCell-->&nbsp;</td>
      </tr>
      <tr> 
        <td height="30" colspan="3" valign="center"><!--DWLayoutEmptyCell-->&nbsp;</td>
        <td colspan="2" valign="top"><strong>Freight:</strong>&nbsp;</td>
        
    <td colspan="2" valign="top">$</td>
      </tr>
      <tr> 
        <td height="30" colspan="3" valign="top"><!--DWLayoutEmptyCell-->&nbsp;</td>
        <td colspan="2" valign="top"><strong>Total: </strong></td>
        
    <td colspan="2" valign="top">$</td>
      </tr>
</table>
	
<p class = "text">
<input name="Submit" type="submit" value="Submit" class="button"/>
</p>
	
</p>
</form>
</div>
<!-- End Content -->
</div>
</body>
</html>

STOP;

&#125;
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

If you're serious about programming, don't use heredoc. Seperate content from logic, else you'll have bloat, confusing structures and end up with pretty much unmaintainable code.

Simplify what you want to do and break it into logical units each performing just as much as necessary. HTML is HTML - no need to generate it with a function. For a more elabourate rant: http://wiki.devnetwork.net/index.php/TemplatesTutorial
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

Post by phpScott »

try looking at this line

Code: Select all

<td colspan="2" valign="top">$</td>
towards the bottom of your code
User avatar
C_Calav
Forum Contributor
Posts: 395
Joined: Wed Jun 02, 2004 10:55 pm
Location: New Zealand

Post by C_Calav »

thanx guys for your replies!

patrikG: i have to use this function as all this code is for a payment page. i was supplied with the code to link with a SSL page. pretty weird code they sent me.

phpScott: cheers for that good spoting will check it when i get home from work!
User avatar
C_Calav
Forum Contributor
Posts: 395
Joined: Wed Jun 02, 2004 10:55 pm
Location: New Zealand

Post by C_Calav »

hey guys, sorry to keep dragging this post on.. but i still cant get it to work!
replaced all the $ with a placeholder to see if that was the problem but it wasnt. so same problem still applies.

Am i doing everything right? it seems to be getting caught up somewhere as my parse error is at the end of my page (after the next function)

thanx if you need anymore info just ask i hope this is enough to solve this problem!
Parse error: parse error, unexpected $

Code: Select all

echo <<<STOP 
    
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

<html> 
<head> 
<title>Model Aircraft</title> 
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> 
<style type="text/css" media="all"> 
   @import "model.css"; 
   </style> 
          
</head> 
<body> 
<div id="container"><h1>Models</h1> 

<!-- Begin Content --> 
<div id="content"> 

<form method="post"> 

<h2>Summary</h2> 

<p class="text"> 

<table width="500"> 
      <!--DWLayoutTable--> 
      <tr> 
        <td width="80" height="21" valign="top">First Name:</td> 
        <td width="135" valign="top">&#123;$C_First&#125;</td> 
        <td width="91" valign="top">Last Name: </td>    
   <td width="172" valign="top">&#123;$C_Last&#125;</td> 
      </tr> 
      <tr> 
        <td height="21" valign="top">Email:</td> 
        <td valign="top">&#123;$C_Email&#125;</td> 
        <td valign="top">Phone:</td> 
        <td valign="top">&#123;$C_Phone&#125;</td> 
      </tr> 
      <tr> 
        <td height="21" valign="top">Address 1:</td>    
     <td valign="top">&#123;$C_Address1&#125;</td> 
        <td valign="top">Address 2:</td>  
     <td valign="top">&#123;$C_Address2&#125;</td> 
      </tr> 
      <tr> 
        <td height="21" valign="top">Country:</td> 
   <td valign="top">&#123;$C_Country&#125;</td> 
        <td valign="top">City:</td> 
        <td valign="top">&#123;$C_City&#125;</td> 
      </tr> 
</table> 
    
<br /> 
    
<table width="450"> 
  <tr> 
     <td width ="100">Select Zone:</td> 
     <td>&#123;$C_Freight&#125;</td> 
  </tr> 
       
</table> 

<br /> 

<table width="500"> 
  <tr> 
        <td width="91" height="40" valign="top"><strong>Aircraft</strong></td> 
        <td width="105" valign="top"><strong>Name</strong></td> 
        <td colspan="2" valign="top"><strong>Price Ea</strong></td> 
        <td width="51" align="left" valign="top"><strong>Qty</strong></td> 
        <td colspan="2" valign="top"><strong>Sub-Total</strong></td> 
  </tr> 
    
STOP; 

     while($row = mysql_fetch_array($result)) 
     &#123; 
     $totalCost += ($row&#1111;"qty"] * $row&#1111;"P_Price"]); 

echo <<<STOP 

      <tr> 
        <td height="64" valign="top"><img src=http://www.modelaircraft.co.nz/Pics/&#123;$row&#1111;"P_Stock"]&#125;.jpg alt="image of plane" width="82" height="62" class="planeimage" /></td> 
   <td>&#123;$row&#1111;"P_Name"]&#125;</td> 
       <td colspan="2" valign="middle">x&#123;$row&#1111;"P_Price"]&#125;</td> 
       <td align="left" valign="middle">&#123;$row&#1111;"qty"]&#125;</td> 
       <td colspan="2" valign="middle"> x </td> 
      </tr> 

STOP; 
   &#125; //end loop 

echo <<<STOP 

      <tr> 
        <td height="21" colspan="6" valign="top"><hr size="1" color="#666666" NOSHADE /></td> 
        <td width="66" valign="top"><!--DWLayoutEmptyCell-->&nbsp;</td> 
      </tr> 
      <tr> 
        <td height="30" colspan="3" valign="center"><!--DWLayoutEmptyCell-->&nbsp;</td> 
        <td colspan="2" valign="top"><strong>Freight:</strong>&nbsp;</td> 
        
    <td colspan="2" valign="top">x</td> 
      </tr> 
      <tr> 
        <td height="30" colspan="3" valign="top"><!--DWLayoutEmptyCell-->&nbsp;</td> 
        <td colspan="2" valign="top"><strong>Total: </strong></td> 
        
    <td colspan="2" valign="top">x</td> 
      </tr> 
</table> 
    
<p class = "text"> 
<input name="Submit" type="submit" value="Submit" class="button"/> 
</p> 
    
</p> 
</form> 
</div> 
<!-- End Content --> 
</div> 
</body> 
</html> 

STOP; 

&#125;
User avatar
C_Calav
Forum Contributor
Posts: 395
Joined: Wed Jun 02, 2004 10:55 pm
Location: New Zealand

Post by C_Calav »

is my question clear enough guys for you guys too help?
Post Reply