Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to prevent function output going to browse

Status
Not open for further replies.

dessie1981

Programmer
May 9, 2006
116
0
0
GB
Hi Everyone,

I have a function that outputs the the contents of a shopping basket that i have used in my online store in place to place.

Code:
function print_cart() {

	print "<center><table border=0 width='80%'>";
	print '<tr><td><center><b>PartNum&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</b></center></td><td><center><b>Model</b></center></td><td><b><center>
Quantity</b></center></td><td><center><b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;Price</b></center></td></tr>';
	print '<tr><td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td></tr>';
	$counter1 = 0;
	$counter2 = count($_SESSION['cart']);
	$total = 0;

	while ($counter1 < $counter2)
	{
		$p = $_SESSION['cart'][$counter1];
		$q = $_SESSION['quantity'][$counter1];
		$result = @mysql_query("Select * from products where id ='$p'");
		while ($row = mysql_fetch_array($result))
		{
			print '<tr><td>' . $row['part_no'] . '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td><td><center>' . $row['model'] . '</center></td><td><center>' . $q . '</center></td><td>' . '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;€' . $row['price']*$q . '</td></tr>';
			$total = $total + ($row['price'] * $q);
		}
		$counter1 ++;
	}

	if (!isset($_SESSION['dcc']))
	{
		$delivery = 15.00;
		$delivery = number_format($delivery,2);
		$total = $total + $delivery;
	}
	$vat = .21;
	$totalvat = $vat * $total;
	$totalincvat = $totalvat + $total;
	$newtotal = round($totalincvat,2);


	print '<tr><td><b>----</b><td></td></td><td></td><td>&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<b>--</b></td></tr>';
	if (!isset($_SESSION['dcc']))
	{
		print '<tr><td><b>Delivery Charge</b></td><td></td></td><td><td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<b>€&nbsp;' . $delivery . '</b></td></tr>';
	}
	print '<tr><td><b>Total</b></td><td></td><td></td><td>&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;<b>€' . $total . '</b></td></tr>';
	print '<tr><td><b>Total incVAT</b></td><td></td><td></td><td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<b>€' . $newtotal . '</b></td></tr>';

	print '</table>';
}

I am trying to include this in the body of a mail i want to send but it keeps printing the function to the browser.

Here is my mail code, im using phpmailer

Code:
$mail2 = new PHPMailer();
$mail2->From     = "xxxxx@xxx.com";
$mail2->FromName = "Test Mail";
$mail2->Host     = "xxxxxxxxxxxxxxxx";
$mail2->Mailer   = "smtp";
$mail2->AddAddress($addressto);
$mail2->isHTML(true);
$mail2->Subject = "Order";
$print_cart = 'test';
$mail2->Body = '<p>Order Placed on ' . date("r") . '</p>
		<br>
		<p>Organisation: ' . $organisation . '</p>
		<p>Customer: ' . $_SESSION['username'] . '</p>
		<p>Sord Order Number : ' . $neworderid . '</p>
		<p>Order......</p>
		<p> ' . print_cart() . '</p>
		';
if(!$mail2->Send())
{
	echo '<p align = "center">Error sending mail</p>';
	
}


If anyone can help me here that would be great.



Regards
Dessie
 
If you don't want it to print out to the browser, don't use [blue]print()[/blue] all over the place. concatenate the string and then return it.

something like:

Code:
[red]$my_string="";[/red]
[red]$my_string.=[/red] "<center><table border=0 width='80%'>";
    [red]$my_string.=[/red]'<tr><td><center><b>PartNum&nbsp;&nbsp;&nbsp;&nb
sp;&nbsp;&nbsp;&nbsp;</b></center></td><td><center><b>Model</b></center></td>
<td><b><center>Quantity</b></center></td><td><center><b>&nbsp;
&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;Price</b></center></td></tr>';
[red]$my_string.=[/red]'<tr><td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td></tr>';
...

[green]//at the end of the funtion return the string[/green]

return $my_string;

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top