Page 1 of 1

help needed for php email

Posted: Sun Jun 22, 2003 10:46 pm
by raind4
Hi,

I am having problems with php email.

Here is my partial code:
<form name="frmCart" method="get">
<table><tr><td><Qty></td><td>Product</td><td>Price</td></tr>
<?php>
while($row = mysql_fetch_array($result))
{
// Increment the total cost of all dcd_products
$totalCost += ($row["qty"] * $row["price"]);
?>
<tr><td><select name="<?php echo $row["product_id"]; ?>">
<?php
for($i = 1; $i <= 20; $i++){
echo "<option ";
if($row["qty"] == $i){
echo " SELECTED ";}
echo ">" . $i . "</option>";}
?>
</select>
</td><td><?php echo $row["product_id"]; ?></td>
<td>$<?php echo number_format($row["price"]); ?></td></tr>
<?php
}
// Display the total
?>
<tr>
<td>Total: $<?php echo number_format($totalCost); ?></td>
</tr>
</table>
</form>
//php email
<?php
/**********************************************
I want the email to send a list of product_id and totalCost.
I don't know how to append the product_id into array and output
them in the code below.
**********************************************/
$to = " <info@mysite.com>" . ", " ;
$subject = "info";
$headers .= "MIME-Version: 1.0\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\n";
$message = "
<html> <head> <title>info</title> </head> <body>
Products: ?? don't know what to put??<br>
Price: $totalCost</p>
</body>
</html>";
mail($to, $subject, $message, $headers);
?>

Posted: Mon Jun 23, 2003 3:57 pm
by m@ndio
hi,

try this:

Code: Select all

<?php 
//the line below is an array I created but obviously fill in the gaps with the name of yours

$products = array("product 1","product 2","product 3"); 

$to = "info@joebloggs.com" . ", " ; 
$subject = "info"; 
$headers .= "MIME-Version: 1.0\n"; 
$headers .= "Content-type: text/html; charset=iso-8859-1\n"; 
$message = "<html><head><title>info</title></head><body>products:<br>";

//this is the bit I added
//start
for ($i=0;$i<count($products);$i++){
	$message.=$products[$i]."<br>";
}
//end

$message.="<p>Price: $totalCost</p></body></html>"; 

mail($to, $subject, $message, $headers); 
?>
that should work :wink:

Posted: Mon Jun 23, 2003 9:03 pm
by raind4
The code works exactly how I wanted, except the array values of
$product need to be passed in.
$products = array("product 1","product 2","product 3"); should be
$product = array($row["product_id"]); something like this

I cannot retrive values of $row["product_id"] since I am outside the while loop:
<?php
while($row = mysql_fetch_array($result))
{ echo $row["product_id"];
}
?>
If I have the email code inside the loop, I can pass in product_id, but receive one email per loop.

How can I pass in all the values of "product_id" from while loop into $products array?

Posted: Tue Jun 24, 2003 2:43 am
by m@ndio
try this:

Code: Select all

<?php 
      for ($i=0;$i<count($product);$i++)
     { 
          echo $product[$i]; 
      }
?>
Get rid of the while loop cos you have already stuck the data in an array. now just loop through the array results.

let me know if this works, I can't test code from where I am..

Posted: Tue Jun 24, 2003 8:51 pm
by raind4
How should I use this if statement?

The sequence of my code is this:
$result = mysql_query("select * from cart inner join products on cart.product_id = products.product_id where cart.cookieId = '" . GetCartId() . "' order by products.product_id asc");

<?php
while($row = mysql_fetch_array($result))
{ echo $row["product_id"];
//bounch of other code
}

//email code.
$to = " <info@mysite.com>" . ", " ;
$subject = "info";
//more as stated in orginal message
?>

If I use
<?php
for ($i=0;$i<count($product);$i++)
{
echo $product[$i];
}
?>
instead of the while loop, how does it know what $product is?

Posted: Wed Jun 25, 2003 3:10 am
by twigletmac
raind4 - please can you put your PHP code in bbcode tags.

Mac

Posted: Wed Jun 25, 2003 12:27 pm
by m@ndio
oops sorry try this:

Code: Select all

$i = 0;

while($row = mysql_fetch_array($result)) 
{
   while ($i = each($row))
  {
   echo "Product: ".current($i)."<br>";
  }
}