Jump to content

[SOLVED] Shopping Cart into array


DarkPrince2005

Recommended Posts

Okay guys, I've got a shopping cart that uses a mysql database. It works fine. But now i want to basically add it all to an array and include it into an email script. I want to use an array cause everytime i use a mysql command in the email function it bugs out and does nothing.

 

here is my scripts thus far

while ($row = mysql_fetch_array($results)) {
  echo "<tr>
		<td align='center' valign='center'>
				<input type='hidden' name='modified_hidden' value='$row[cart_id]'>
				<input type='hidden' name='qty' value='$row[a]'>$row[a]
		</td>
		<td align='center' name='id' value='$row[id] valign='center'>$row[id]</td>
		<td align='center' valign='center'>$row[name]</td>
		<td align='center' valign='center'>$row[price]</td>
		<td align='center' valign='center'>";
  //get extended price
  $extprice = number_format($row['price'] * $row['a'], 2);
  echo $extprice;}
  echo "</td></tr>
  <tr><td colspan='5' align='center'><input type='submit' name='submit' value=\"Next ->\"></td></tr></table>";
  $_SESSION['cart'][] = array('cart_id'=> $row['cart_id'],'qty'=>$row['a'],'id'=>$row['id'],'name' => $row['name'], 'price' => $row['price']); 

 

function email()
{

include "config.php";

  
$sessid = session_id();
$firstname = $_SESSION['firstname'];
$surname = $_SESSION['surname'];
$suburb = $_SESSION['suburb'];
$city = $_SESSION['city'];
$pcode = $_SESSION['pcode'];
$postaladdress = $_SESSION['postaladdress'];
$postalsuburb = $_SESSION['postalsuburb'];
$postalcity = $_SESSION['postalcity'];
$postalcode = $_SESSION['postalcode'];
$telw = $_SESSION['telw'];
$telh = $_SESSION['telh'];
$cell = $_SESSION['cell'];
$email = $_SESSION['email'];
$fax = $_SESSION['fax'];

$a = $_SESSION['cart']['a'];
$id = $_SESSION['cart']['id'];

require("class.phpmailer.php");

$arremails='blah@something.com';

$mail = new PHPMailer();

$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "mail.something.com"; // SMTP server

$mail->From = "info@something.com";
$mail->AddAddress($arremails); 

$mail->Subject = "Order Form";
$mail->IsHTML(true);
$mail->Body = "<table width='600' border='0' cellpadding='5' cellspacing='1'>
        <tr> 
            <td width='150'>First name</td>
            <td class='content'>$firstname</td>
        </tr>
        <tr> 
            <td width='150'>Surname</td>
            <td class='content'>$surname</td>
        </tr>
        <tr> 
            <td width='150'>Address</td>
            <td class='content'>$addess</td>
        </tr>
        <tr> 
            <td width='150'>Suburb</td>
            <td class='content'>$suburb</td>
        </tr>
        <tr> 
            <td width='150'>City</td>
            <td class='content'>$city</td>
        </tr>
        <tr> 
            <td width='150'>Postal Code</td>
            <td class='content'>$pcode</td>
        </tr>
        <tr> 
            <td width='150'>Postal Address</td>
            <td class='content'>$postaladdress</td>
        </tr>
        <tr> 
            <td width='150'>Postal Suburb</td>
            <td class='content'>$postalsuburb</td>
        </tr>
	<tr> 
            <td width='150'>Postal City</td>
            <td class='content'>$postalcity</td>
        </tr>
        <tr> 
            <td width='150'>Postal Code</td>
            <td class='content'>$postalcode</td>
        </tr>
        <tr> 
            <td width='150'>Telephone Number(W)</td>
            <td class='content'>$telw</td>
        </tr>
        <tr> 
            <td width='150'>Telephone Number(H)</td>
            <td class='content'>$telh</td>
        </tr>
        <tr> 
            <td width='150'>Cellphone Number</td>
            <td class='content'>$cell</td>
        </tr>
        <tr> 
            <td width='150'>Email Address</td>
            <td class='content'>$email</td>
        </tr>
        <tr> 
            <td width='150'>Fax Number</td>
            <td class='content'>$fax</td>
        </tr>
    </table>
    <br>
<table border='0' width='600' align='center' cellpadding='5'>
<tr>
<td colspan='7' bgcolor='#BAC6E3'>Product/Order Information</td>
</tr>
  <tr>
    <td>Quantity</td>
    <td>Product ID</td>
    <td>Item Name</td>
    <td>Price Each</td>
    <td>Extended Price</td>
    <td></td>
    <td></td>
</tr>
<tr>
		<td align='center' valign='center'>
				$a
		</td>
		<td align='center'>$id</td>
		<td align='center'>$name</td>
		<td align='center'>$price</td>
		<td align='center'></td></tr>
</table>
<center>";
if(!$mail->Send()) {
  echo "<script language=JavaScript>window.location='mailfail.html'</script><br>";
} else {
  echo "<script language=JavaScript>window.location='thankyou.html'</script><br>";
};
}

 

 

Link to comment
Share on other sites

I want to use an array cause everytime i use a mysql command in the email function it bugs out and does nothing.

 

And how to you expect to get the values into an array if you don't get them from the database? Why don't we address the real problem of why you are unable to get the values from the database rather than trying to make a workaround when one isn't needed.

 

Let's start with some information on the errors encountered when it "bugs out".

 

Without going into great detail in reading your scripts above it looks like you have a block of code that echo's out the shopping cart contents to the page. Then it appears you have a function for sending the email. But, I don't see how you are passing the requisite vallues to the function or how the function obtains those values (i.e. the cart details).

 

Are you wanting to have the email sent automatically when the page to display the cart is generated or is there some user initiated event that would send the email?

Link to comment
Share on other sites

I wanted to retrieve the cart content via a mysql select statement in the email, but as soon as i add any kind of php code into the email it basically fails and just returns a blank page without sending anything. the email is to be sent after the client completed the payment at paygate, and if it was successful.

Link to comment
Share on other sites

There's no reason you can query the database and then construct the email based upon that query. Something must be wrong in the code you are using. Here is an example of how I would think it should be done (can't say for sure as I don't know enough about your app). I think your function is way too dependant upon session data. For an order I would expect all the pertinent data about an order to be stored in the DB. But, anyway, this is just an example - can't guarantee it will work, but should point you in the right direction.

 

function email()
{
    require("class.phpmailer.php"); 
    include "config.php";

    $arremails='blah@something.com';
    $sessid = session_id();
    $a = $_SESSION['cart']['a'];
    $id = $_SESSION['cart']['id'];

    //Get the order details
    $order_data = '';
    $query = "SELECT * FROM orders WHERE id='$id'";
    $result = mysql_query($query);
    while($record = mysql_fetch_assoc($result))
    {
        $order_data .= "
<tr>
  <td align=\"center\" valign=\"center\">{$a}</td>
  <td align=\"center\">{$id}</td>
  <td align=\"center\">{$record['name']}</td>
  <td align=\"center\">{$record['price']}</td>
  <td align=\"center\"></td>
</tr>";
    }

    //Build the body of the email
    $body = "<table width=\"600\" border=\"0\" cellpadding=\"5\" cellspacing=\"1\">
        <tr><td width=\"150\">First name</td><td class=\"content\">{$_SESSION['firstname']}</td></tr>
        <tr><td width=\"150\">Surname</td><td class=\"content\">{$_SESSION['surname']}</td></tr>
        <tr><td width=\"150\">Address</td><td class=\"content\">$addess</td></tr>
        <tr><td width=\"150\">Suburb</td><td class=\"content\">{$_SESSION['suburb']}</td></tr>
        <tr> <td width=\"150\">City</td><td class=\"content\">{$_SESSION['city']}</td></tr>
        <tr><td width=\"150\">Postal Code</td><td class=\"content\">{$_SESSION['pcode']}</td></tr>
        <tr><td width=\"150\">Postal Address</td><td class=\"content\">{$_SESSION['postaladdress']}</td></tr>
        <tr><td width=\"150\">Postal Suburb</td><td class=\"content\">{$_SESSION['postalsuburb']}</td></tr>
<tr><td width=\"150\">Postal City</td><td class=\"content\">{$_SESSION['postalcity']}</td></tr>
        <tr><td width=\"150\">Postal Code</td><td class=\"content\">{$_SESSION['postalcode']}</td></tr>
        <tr><td width=\"150\">Telephone Number(W)</td><td class=\"content\">{$_SESSION['telw']}</td></tr>
        <tr><td width=\"150\">Telephone Number(H)</td><td class=\"content\">{$_SESSION['telh']}</td></tr>
        <tr><td width=\"150\">Cellphone Number</td><td class=\"content\">{$_SESSION['cell']}</td></tr>
<tr><td width=\"150\">Email Address</td><td class=\"content\">{$_SESSION['email']}</td></tr>
        <tr><td width=\"150\">Fax Number</td><td class=\"content\">{$_SESSION['fax']}</td></tr>
</table>
<br>
<table border=\"0\" width=\"600\" align=\"center\" cellpadding=\"5\">
<tr><td colspan=\"7\" bgcolor=\"#BAC6E3\">Product/Order Information</td></tr>
<tr>
  <td>Quantity</td>
  <td>Product ID</td>
  <td>Item Name</td>
  <td>Price Each</td>
  <td>Extended Price</td>
  <td></td>
  <td></td>
</tr>
{$order_data}
</table>";

    //Send the email
    $mail = new PHPMailer();
    $mail->IsSMTP(); // telling the class to use SMTP
    $mail->Host = "mail.something.com"; // SMTP server
    $mail->From = "info@something.com";
    $mail->AddAddress($arremails); 
    $mail->Subject = "Order Form";
    $mail->IsHTML(true);
    $mail->Body = $body;


    if(!$mail->Send()) {
        echo "<script language=JavaScript>window.location=\"mailfail.html\"</script><br>";
    } else {
        echo "<script language=JavaScript>window.location=\"thankyou.html\"</script><br>";
    }
}

Link to comment
Share on other sites

Thanks mjdamato, that actually pushed me in the right direction and I'm getting it to work except it isn't calculating the total.

 

function email()
{
    require("class.phpmailer.php"); 
    include "config.php";
mysql_connect("localhost","storaapu_admin","admin");
mysql_select_db("storaapu_storageinstyle");
    $arremails='jacof@spign.co.za';
    $sessid = session_id();
    $a = $_SESSION['cart']['a'];
    $id = $_SESSION['cart']['id'];

    //Get the order details
    $order_data = '';
    $query = "select cart.product_id as 'id', sum(cart.cart_qty) as 'a', cart.cart_qty, cart.cart_session_id, cart.cart_id,
	products.product_name as 'name',products.product_price as ' price',(products.product_price * cart.cart_qty) as 'total' from cart,products where cart.cart_session_id like '$sessid' and products.product_id = cart.product_id group by cart.product_id";
    $result = mysql_query($query);
    while($record = mysql_fetch_assoc($result))
    {
        $order_data .= "
<tr>
  <td align=\"center\" valign=\"center\">$record[a]</td>
  <td align=\"center\">$record[id]</td>
  <td align=\"center\">$record[name]</td>
  <td align=\"center\">$record[price]</td>";
  $extprice = number_format($record['price'] * $record['a'], 2);
  $order_data .="<td>$extprice</td>";
  $order_data .="</td><td align=\"center\">$record[total]</td>
</tr>";
}
while ($row=mysql_fetch_array($result)){
$E +=$row['total'];
$total = number_format($E,2);
$total1=str_replace(".", "", $total);}
$grandtotal = $total+85; // output: 78
$value=str_replace(",","",$total1)+8500;
   $order_data .= "<tr>
    <td> </td><td colspan='4' align='left' class='cart-table'>
         Your total including shipping is:</td>
    <td align='center' class='cart-table'>R $total</td><td></td></tr>";
    

    //Build the body of the email
    $body = "<table width=\"600\" border=\"0\" cellpadding=\"5\" cellspacing=\"1\">
        <tr><td width=\"150\">First name</td><td class=\"content\">{$_SESSION['firstname']}</td></tr>
        <tr><td width=\"150\">Surname</td><td class=\"content\">{$_SESSION['surname']}</td></tr>
        <tr><td width=\"150\">Address</td><td class=\"content\">{$_SESSION['address']}</td></tr>
        <tr><td width=\"150\">Suburb</td><td class=\"content\">{$_SESSION['suburb']}</td></tr>
        <tr> <td width=\"150\">City</td><td class=\"content\">{$_SESSION['city']}</td></tr>
        <tr><td width=\"150\">Postal Code</td><td class=\"content\">{$_SESSION['pcode']}</td></tr>
        <tr><td width=\"150\">Postal Address</td><td class=\"content\">{$_SESSION['postaladdress']}</td></tr>
        <tr><td width=\"150\">Postal Suburb</td><td class=\"content\">{$_SESSION['postalsuburb']}</td></tr>
<tr><td width=\"150\">Postal City</td><td class=\"content\">{$_SESSION['postalcity']}</td></tr>
        <tr><td width=\"150\">Postal Code</td><td class=\"content\">{$_SESSION['postalcode']}</td></tr>
        <tr><td width=\"150\">Telephone Number(W)</td><td class=\"content\">{$_SESSION['telw']}</td></tr>
        <tr><td width=\"150\">Telephone Number(H)</td><td class=\"content\">{$_SESSION['telh']}</td></tr>
        <tr><td width=\"150\">Cellphone Number</td><td class=\"content\">{$_SESSION['cell']}</td></tr>
<tr><td width=\"150\">Email Address</td><td class=\"content\">{$_SESSION['email']}</td></tr>
        <tr><td width=\"150\">Fax Number</td><td class=\"content\">{$_SESSION['fax']}</td></tr>
</table>
<br>
<table border=\"0\" width=\"600\" align=\"center\" cellpadding=\"5\">
<tr><td colspan=\"7\" bgcolor=\"#BAC6E3\">Product/Order Information</td></tr>
<tr>
  <td>Quantity</td>
  <td>Product ID</td>
  <td>Item Name</td>
  <td>Price Each</td>
  <td>Extended Price</td>
  <td></td>
  <td></td>
</tr>
{$order_data}
</table>";

    //Send the email
    $mail = new PHPMailer();
    $mail->IsSMTP(); // telling the class to use SMTP
    $mail->Host = "mail.spign.co.za"; // SMTP server
    $mail->From = "info@something.com";
    $mail->AddAddress($arremails); 
    $mail->Subject = "Order Form";
    $mail->IsHTML(true);
    $mail->Body = $body;


    if(!$mail->Send()) {
       
    } else {
       
    }
}

 

it's suppose to add 85 to the total for shipping cost, but any combination/sollution I try only returns a value without even calculating the total?

Link to comment
Share on other sites

I see a couple things wrong.

 

1. You have two while loops for pulling records from the query. Unless you reset the pointer to the first record after the first while loop, there are no records for the 2nd while loop to process. Besides you should just process the reocrds one.

 

2. Inthe query you are pulling the  products.product_price field as ' price' - (note the space). I assume that is a typo.

 

<?php

function email()
{
    require("class.phpmailer.php"); 
    include "config.php";
    mysql_connect("localhost","storaapu_admin","admin");
    mysql_select_db("storaapu_storageinstyle");
    $arremails='jacof@spign.co.za';
    $sessid = session_id();
    $a = $_SESSION['cart']['a'];
    $id = $_SESSION['cart']['id'];

    //Get the order details
    $order_data = '';
    $grand_total = 85;
    $query = "SELECT cart.product_id as 'id', sum(cart.cart_qty) as 'a', cart.cart_qty,
                     cart.cart_session_id, cart.cart_id,	products.product_name as 'name',
                     products.product_price as 'price',(products.product_price * cart.cart_qty) as 'total'
              FROM cart, products
              WHERE cart.cart_session_id LIKE '$sessid'
                AND products.product_id = cart.product_id
              GROUP BY cart.product_id";
    $result = mysql_query($query);
    while($record = mysql_fetch_assoc($result))
    {
    $extprice = number_format($record['price'] * $record['a'], 2);
        $grand_total += $record['total'];
        $order_data .= "
<tr>
  <td align=\"center\" valign=\"center\">{$record['a']}</td>
  <td align=\"center\">{$record['id']}</td>
  <td align=\"center\">{$record['name']}</td>
  <td align=\"center\">{$record['price']}</td>
      <td>{$extprice}</td>
      </td><td align=\"center\">{$record['total']}</td>
</tr>";
}

    //Format the grand total
    $grand_total = number_format($grand_total, 2);

    //Build the body of the email
    $body = "<table width=\"600\" border=\"0\" cellpadding=\"5\" cellspacing=\"1\">
        <tr><td width=\"150\">First name</td><td class=\"content\">{$_SESSION['firstname']}</td></tr>
        <tr><td width=\"150\">Surname</td><td class=\"content\">{$_SESSION['surname']}</td></tr>
        <tr><td width=\"150\">Address</td><td class=\"content\">{$_SESSION['address']}</td></tr>
        <tr><td width=\"150\">Suburb</td><td class=\"content\">{$_SESSION['suburb']}</td></tr>
        <tr> <td width=\"150\">City</td><td class=\"content\">{$_SESSION['city']}</td></tr>
        <tr><td width=\"150\">Postal Code</td><td class=\"content\">{$_SESSION['pcode']}</td></tr>
        <tr><td width=\"150\">Postal Address</td><td class=\"content\">{$_SESSION['postaladdress']}</td></tr>
        <tr><td width=\"150\">Postal Suburb</td><td class=\"content\">{$_SESSION['postalsuburb']}</td></tr>
<tr><td width=\"150\">Postal City</td><td class=\"content\">{$_SESSION['postalcity']}</td></tr>
        <tr><td width=\"150\">Postal Code</td><td class=\"content\">{$_SESSION['postalcode']}</td></tr>
        <tr><td width=\"150\">Telephone Number(W)</td><td class=\"content\">{$_SESSION['telw']}</td></tr>
        <tr><td width=\"150\">Telephone Number(H)</td><td class=\"content\">{$_SESSION['telh']}</td></tr>
        <tr><td width=\"150\">Cellphone Number</td><td class=\"content\">{$_SESSION['cell']}</td></tr>
<tr><td width=\"150\">Email Address</td><td class=\"content\">{$_SESSION['email']}</td></tr>
        <tr><td width=\"150\">Fax Number</td><td class=\"content\">{$_SESSION['fax']}</td></tr>
</table>
<br>
<table border=\"0\" width=\"600\" align=\"center\" cellpadding=\"5\">
<tr><td colspan=\"7\" bgcolor=\"#BAC6E3\">Product/Order Information</td></tr>
<tr>
  <td>Quantity</td>
  <td>Product ID</td>
  <td>Item Name</td>
  <td>Price Each</td>
  <td>Extended Price</td>
  <td></td>
  <td></td>
</tr>
{$order_data}
<tr>
      <td> </td>
      <td colspan=\"4\" align=\"left\" class=\"cart-table\">   Your total including shipping is:</td>
      <td align=\"center\" class=\"cart-table\">R {$grand_total}</td>
      <td> </td>
    </tr>
</table>";

    //Send the email
    $mail = new PHPMailer();
    $mail->IsSMTP(); // telling the class to use SMTP
    $mail->Host = "mail.spign.co.za"; // SMTP server
    $mail->From = "info@something.com";
    $mail->AddAddress($arremails); 
    $mail->Subject = "Order Form";
    $mail->IsHTML(true);
    $mail->Body = $body;


    if(!$mail->Send()) {
       
    } else {
       
    }
}
?>

Link to comment
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.