Page 1 of 1
printing in a function
Posted: Wed Oct 27, 2004 3:00 am
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!
Posted: Wed Oct 27, 2004 4:41 am
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
Posted: Wed Oct 27, 2004 9:40 am
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
Posted: Wed Oct 27, 2004 2:49 pm
by C_Calav
cheers guys! that looks like what i want! will try out when i get home from work.
Posted: Wed Oct 27, 2004 8:54 pm
by LostMyLove
Code: Select all
function print_form()
{
print <<<STOP
<html>
<body>
code
</body>
</html>
STOP
}
Posted: Wed Oct 27, 2004 9:03 pm
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
Posted: Thu Oct 28, 2004 2:55 am
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ї"P_Price"], 2, ".", ","); ?>
Posted: Thu Oct 28, 2004 3:16 am
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">{$C_First}</td>
<td width="91" valign="top">Last Name: </td>
<td width="172" valign="top">{$C_Last}</td>
</tr>
<tr>
<td height="21" valign="top">Email:</td>
<td valign="top">{$C_Email}</td>
<td valign="top">Phone:</td>
<td valign="top">{$C_Phone}</td>
</tr>
<tr>
<td height="21" valign="top">Address 1:</td>
<td valign="top">{$C_Address1}</td>
<td valign="top">Address 2:</td>
<td valign="top">{$C_Address2}</td>
</tr>
<tr>
<td height="21" valign="top">Country:</td>
<td valign="top">{$C_Country}</td>
<td valign="top">City:</td>
<td valign="top">{$C_City}</td>
</tr>
</table>
<br />
<table width="450">
<tr>
<td width ="100">Select Zone:</td>
<td>{$C_Freight}</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))
{
$totalCost += ($rowї"qty"] * $rowї"P_Price"]);
echo <<<STOP
<tr>
<td height="64" valign="top"><img src=http://www.modelaircraft.co.nz/Pics/{$rowї"P_Stock"]}.jpg alt="image of plane" width="82" height="62" class="planeimage" /></td>
<td>{$rowї"P_Name"]}</td>
<td colspan="2" valign="middle">${$rowї"P_Price"]}</td>
<td align="left" valign="middle">{$rowї"qty"]}</td>
<td colspan="2" valign="middle"> $ </td>
</tr>
STOP;
} //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--> </td>
</tr>
<tr>
<td height="30" colspan="3" valign="center"><!--DWLayoutEmptyCell--> </td>
<td colspan="2" valign="top"><strong>Freight:</strong> </td>
<td colspan="2" valign="top">$</td>
</tr>
<tr>
<td height="30" colspan="3" valign="top"><!--DWLayoutEmptyCell--> </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;
}
Posted: Thu Oct 28, 2004 4:45 am
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
Posted: Thu Oct 28, 2004 6:33 am
by phpScott
try looking at this line
Code: Select all
<td colspan="2" valign="top">$</td>
towards the bottom of your code
Posted: Thu Oct 28, 2004 2:50 pm
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!
Posted: Sun Oct 31, 2004 12:14 am
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">{$C_First}</td>
<td width="91" valign="top">Last Name: </td>
<td width="172" valign="top">{$C_Last}</td>
</tr>
<tr>
<td height="21" valign="top">Email:</td>
<td valign="top">{$C_Email}</td>
<td valign="top">Phone:</td>
<td valign="top">{$C_Phone}</td>
</tr>
<tr>
<td height="21" valign="top">Address 1:</td>
<td valign="top">{$C_Address1}</td>
<td valign="top">Address 2:</td>
<td valign="top">{$C_Address2}</td>
</tr>
<tr>
<td height="21" valign="top">Country:</td>
<td valign="top">{$C_Country}</td>
<td valign="top">City:</td>
<td valign="top">{$C_City}</td>
</tr>
</table>
<br />
<table width="450">
<tr>
<td width ="100">Select Zone:</td>
<td>{$C_Freight}</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))
{
$totalCost += ($rowї"qty"] * $rowї"P_Price"]);
echo <<<STOP
<tr>
<td height="64" valign="top"><img src=http://www.modelaircraft.co.nz/Pics/{$rowї"P_Stock"]}.jpg alt="image of plane" width="82" height="62" class="planeimage" /></td>
<td>{$rowї"P_Name"]}</td>
<td colspan="2" valign="middle">x{$rowї"P_Price"]}</td>
<td align="left" valign="middle">{$rowї"qty"]}</td>
<td colspan="2" valign="middle"> x </td>
</tr>
STOP;
} //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--> </td>
</tr>
<tr>
<td height="30" colspan="3" valign="center"><!--DWLayoutEmptyCell--> </td>
<td colspan="2" valign="top"><strong>Freight:</strong> </td>
<td colspan="2" valign="top">x</td>
</tr>
<tr>
<td height="30" colspan="3" valign="top"><!--DWLayoutEmptyCell--> </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;
}
Posted: Sun Oct 31, 2004 3:16 pm
by C_Calav
is my question clear enough guys for you guys too help?