Jump to content

Display a value for each value in array stored on different database table


Mal1

Recommended Posts

This is kind of hard to explain. I have a website that sends a message by email using PHPMailer when someone adds details to the basket. I've copied this across to another similar but slightly different website and the form is sending but I can't seem to get the all important item (rug) id to send.

// drop a mail      
    $mail  = new PHPMailer(); // defaults to using php "mail()"
    $body  = "New Order Generated via the website.<br />";
    $body .= "Order ID: ".$this->id."<br /><br />";
    $body .= '<h3>Order Details</h3>'."\n";
   ***foreach ( $this->getItems() as $item )
     $body .= 'RugID '.$item['id'] .' x '. $item['qty'] ."\n";***
      
    $body .= '<h3>Customer Details</h3>'."\n";
    $body .= '<table>'."\n";
    $body .= '<tr><td>Name: </td><td>'.$firstname.' '.$lastname. '</td></tr>'."\n";

    $body .= '<tr><td>Email: </td><td>'.$email. '</td></tr>'."\n";
    $body .= '<tr><td>Street Address: </td><td>'.$street_adress. '</td></tr>'."\n";
    $body .= '<tr><td> </td><td>'.$suburb. '</td></tr>'."\n";
    $body .= '<tr><td>Post Code: </td><td>'.$postcode.'</td></tr>'."\n";
    $body .= '<tr><td>City: </td><td>'.$city. '</td></tr>'."\n";
    $body .= '<tr><td>Country: </td><td>'.$country. '</td></tr>'."\n";
    $body .= '<tr><td>Phone: </td><td>'.$phone. '</td></tr>'."\n";
    $body .= '<tr><td>Notes/Delivery Instructions: </td><td>'.$notes. '</td></tr>'."\n";
    $body .= '</table>'."\n";

    $address = $_SESSION['email'];
    
    $mail->SetFrom("website@XXX.com", 'XXX');
    $mail->AddAddress("XXX@hotmail.com");
    
    $mail->Subject    = "New Order";
    $mail->AltBody    = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
    
    $mail->MsgHTML($body);
    
    $mail->Send(); 
    

		return $strCrypt;
	}

The above code works on one site but the ***Code*** causes an error on the other site as what it is referring to isn't set up the same way.

 

Below is the entire code for the site I can't get it to work on, I've tried using everything I can think of ($this->orders['id'], $orders['id'], $rug->data['id'], $rug['id'] - which is what is used to display the ID on the basket and checkout pages) but my knowledge of php is limited and could be coming from completely the wrong angle:


<?php

ini_set('display_errors',  true);
error_reporting(1);

class Basket {
	var $items, $order, $same_shipping_adress, $id;
	var $items_locked;

	function Basket() {
		if (isset($_SESSION['same_shipping_adress']))
			$this->same_shipping_adress = $_SESSION['same_shipping_adress'];
		if (isset($_SESSION['order']))
			$this->order = unserialize($_SESSION['order']);
		if (isset($_SESSION['order_id']) && intval($_SESSION['order_id']) != 0)
			$this->id = $_SESSION['order_id'];
		else
			$this->id = 0;
	}

	function checkout($fields) {
		$billing_fields = array("firstname", "lastname", "email", "street_adress", "postcode", "suburb", "city", "county", "country", "phone");

		if (!isset($fields['same_shipping_adress']))
			$shipping_fields = array("street_adress", "postcode", "city", "suburb", "county", "country", "phone");
		else
			$shipping_fields = array();

		foreach ($billing_fields as $field)
			$this->order["$field"] = htmlspecialchars($fields['billing'][$field]);

		foreach ($shipping_fields as $field)
			$this->order["shipping_$field"] = htmlspecialchars($fields['shipping'][$field]);

		$this->order['same_shipping_adress'] = isset($_POST['same_shipping_adress'])?1:0;

		$_SESSION['order'] = serialize($this->order);
	}

	function simpleXor($InString, $Key) {
		$KeyList = array();
		$output = "";

		for($i = 0; $i < strlen($Key); $i++){
			$KeyList[$i] = ord(substr($Key, $i, 1));
		}

		for($i = 0; $i < strlen($InString); $i++)
			$output.= chr(ord(substr($InString, $i, 1)) ^ ($KeyList[$i % strlen($Key)]));

		return $output;
	}

	function generateOrder($cart) {
		global $database, $strTransactionType, $strThankYouEmail, $strVSPVendorName, $strVendorEMail, $strEncryptionPassword, $strCurrency, $strShortDescription, $base_url;

		$strBasket = "";
		$sngTotal = 0.0;
		$iBasketItems = 0;

		$query = "LOCK TABLES `cart`;";
		mysql_query($query);

		$res=$cart->get_items();
		while($item=mysql_fetch_array($res)) {
			$iBasketItems++;
			$price = $item['special_offer']?$item['discount_price']:$item['price'];
			$sngTotal=$sngTotal + $price;
			$strBasket=$strBasket . ":" . substr($item['short_description'], 0, 1000) . ":".$item['qty'];
			$strBasket=$strBasket . ":" . number_format($price/1.175,2,'.',''); /** Price ex-Vat **/
			$strBasket=$strBasket . ":" . number_format($price*7/47,2,'.',''); /** VAT component **/
			$strBasket=$strBasket . ":" . number_format($price,2,'.',''); /** Item price **/
			$strBasket=$strBasket . ":" . number_format($price,2,'.',''); /** Line total **/
		}

		$strBasket = $iBasketItems . $strBasket;

		$intRandNum = rand(0,32000)*rand(0,32000);
		$strVendorTxCode = $strVSPVendorName . $intRandNum;

		$strPost = "VendorTxCode=" . $strVendorTxCode;
		$strPost = $strPost . "&Amount=" . number_format($sngTotal,2, '.', ''); // Formatted to 2 decimal places with leading digit
		$strPost = $strPost . "&Currency=" . $strCurrency;
		$strPost = $strPost . "&Description=".$strShortDescription;

		$strPost = $strPost . "&SuccessURL=http://$base_url/?action=order_successful";
		$strPost = $strPost . "&FailureURL=http://$base_url/?action=order_failed";

		$strPost = $strPost . "&CustomerName=" . $this->order['firstname']." ".$this->order['lastname'];
		$strPost = $strPost . "&CustomerEMail=" . $this->order['email'];

		$strPost = $strPost . "&VendorEMail=" . $strVendorEMail;

		$strPost = $strPost . "&eMailMessage=$strThankYouEmail";

		$strPost = $strPost . "&BillingAddress=" . $this->order['street_adress'];
		$strPost = $strPost . "&BillingPostCode=" . $this->order['postcode'];
		if ($this->order['same_shipping_adress']) {
			$strPost = $strPost . "&DeliveryAddress=" . $this->order['street_adress'];
			$strPost = $strPost . "&DeliveryPostCode=" . $this->order['postcode'];
		}
		else {
			$strPost = $strPost . "&DeliveryAddress=" . $this->order['shipping_street_adress'];
			$strPost = $strPost . "&DeliveryPostCode=" . $this->order['shipping_postcode'];
		}

		$strPost=$strPost . "&ContactNumber=" . $this->order['phone'];

		$strPost=$strPost . "&Basket=" . $strBasket;

		$strPost=$strPost . "&AllowGiftAid=0";

		if ($strTransactionType!=="AUTHENTICATE")
			$strPost=$strPost . "&ApplyAVSCV2=0";

		$strPost=$strPost . "&Apply3DSecure=0";

		$strCrypt = base64_encode($this->SimpleXor($strPost,$strEncryptionPassword));

		foreach ($this->order as $key => $val)
			$$key = $database->escape($val);

		$_SESSION['uniq'] = md5(uniqid(rand(), true));
		if ($this->id) {
			$database->execute("UPDATE `orders` SET `firstname` = '$firstname',`lastname` = '$lastname',`email` = '$email',`street_adress` = '$street_adress',`suburb` = '$suburb',`postcode` = '$postcode',`city` = '$city', `county`='$county', `country` = '$country',`phone` = '$phone',`shipping_street_adress` = '$shipping_street_adress',`shipping_suburb` = '$shipping_suburb',`shipping_postcode` = '$shipping_postcode',`shipping_city` = '$shipping_city', `shipping_county`='$shipping_county', `shipping_country` = '$shipping_country',`shipping_phone` = '$shipping_phone', `created`=NOW(), `total`='$sngTotal', `uniqid`='".$_SESSION['uniq']."' WHERE `id`='{$this->id}'");

			$this->updateItems($cart);
		} else {
			$this->id = $database->execute("INSERT INTO `orders` (`id` , `firstname` , `lastname` , `email` , `street_adress` , `suburb` , `postcode` , `city` , `county`, `country` , `phone` , `shipping_street_adress` , `shipping_suburb` , `shipping_postcode` , `shipping_city` , `shipping_county`, `shipping_country` , `shipping_phone` , `created` , `state`, `total`, `uniqid`) ".
											"VALUES (NULL , '$firstname', '$lastname', '$email', '$street_adress', '$suburb', '$postcode', '$city', '$county', '$country', '$phone', '$shipping_street_adress', '$shipping_suburb', '$shipping_postcode', '$shipping_city', '$shipping_county', '$shipping_country', '$shipping_phone', NOW(), 'processing', '$sngTotal', '".$_SESSION['uniq']."');");

			$_SESSION['order_id'] = $this->id;

			$this->updateItems($cart);
		}
		$query = "UNLOCK TABLES";
		mysql_query($query);
		
		// drop a mail      
require_once('PHPMailer/class.phpmailer.php');
    $mail  = new PHPMailer(); // defaults to using php "mail()"
    $body  = "New Order Generated via the website.<br />";
    $body .= "Order ID: ".$this->id."<br /><br />";
    $body .= '<h3>Order Details</h3>'."\n";
    

***foreach ( DO NO KNOW WHAT TO USE HERE ) 
      $body .= "Rug Reference: ".DO NOT KNOW WHAT TO USE HERE."<br /><br />";***



    $body .= '<h3>Customer Details</h3>'."\n";
    $body .= '<table>'."\n";
    $body .= '<tr><td>Name: </td><td>'.$firstname.' '.$lastname. '</td></tr>'."\n";

    $body .= '<tr><td>Email: </td><td>'.$email. '</td></tr>'."\n";
    $body .= '<tr><td>Street Address: </td><td>'.$street_adress. '</td></tr>'."\n";
    $body .= '<tr><td> </td><td>'.$suburb. '</td></tr>'."\n";
    $body .= '<tr><td>Post Code: </td><td>'.$postcode.'</td></tr>'."\n";
    $body .= '<tr><td>City: </td><td>'.$city. '</td></tr>'."\n";
    $body .= '<tr><td>Country: </td><td>'.$country. '</td></tr>'."\n";
    $body .= '<tr><td>Phone: </td><td>'.$phone. '</td></tr>'."\n";
    $body .= '<tr><td>Notes/Delivery Instructions: </td><td>'.$notes. '</td></tr>'."\n";
    $body .= '</table>'."\n";

    $address = $_SESSION['email'];
    
    $mail->SetFrom("website@XXX.com", 'XXX');
    $mail->AddAddress("XXX@hotmail.com");
    
    $mail->Subject    = "New Order";
    $mail->AltBody    = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
    
    $mail->MsgHTML($body);
    
    $mail->Send(); 
    
		return $strCrypt;
	}
	
	

	function updateItems($cart) {
		$query = "DELETE FROM `orders_rugs` WHERE `order_id` = ".$this->id.";";
		mysql_query($query);
		$res=$cart->get_items();
		while($item=mysql_fetch_array($res)) {
			$query = "INSERT INTO `orders_rugs` (`rug_id`, `order_id`, `order_qty`, `sold_price`) VALUES ('".$item['item']."', '".$this->id."', '".$item['qty']."', '".($item['special_offer']?$item['discount_price']:$item['price'])."')";
			mysql_query($query);
		}
		$cart->clear_cart();
	}

	function decrypt ($crypted_string) {
		global $strEncryptionPassword;

		$crypted_string = str_replace(" ", "+", $crypted_string); // fix php $_GET handling
		$decrypted = $this->simpleXor(base64_decode($crypted_string), $strEncryptionPassword);

		$response = split("&", $decrypted);

		$fields = array();

		foreach ($response as $field) {
			$key_val = split("=", $field);
			$fields[$key_val[0]] = urldecode($key_val[1]);
		}

		return $fields;
	}

	function confirmOrder($crypt) {
		global $database;

		$protxResponse = $this->decrypt($crypt);

		if ($protxResponse['Status'] != "OK")
			return "ERROR";

		$database->execute("UPDATE `orders` SET `state`='accepted' WHERE `id`='{$this->id}' AND `uniqid` = '".$_SESSION['uniq']."'");

		$this->id = 0;
		$_SESSION['order_id'] = 0;
	}

	function cancelOrder($crypt) {
		global $database;

		//$protxResponse = $this->decrypt($crypt);
	
		$testID = $this->id;
		$testrugid = $database->query("SELECT * FROM `orders_rugs` WHERE `order_id`='".$testID."' LIMIT 1");
		$rugeid = $testrugid[0];
		$finRugID = $rugeid[rug_id];
		$finRugQTY = $rugeid[order_qty];

		#echo "RugID: ". $finRugID;
		#echo "<br>RugQTY: ". $finRugQTY;
		$database->execute("UPDATE `orders` SET `state`='cancelled' WHERE `id`='{$this->id}'");
		$database->execute("UPDATE `rugs` SET `sold`='0' WHERE `id`='".$finRugID."'");
		$database->execute("UPDATE `rugs` SET `stock`=`stock`+'".$finRugQTY."' WHERE `id`='".$finRugID."'");

		$this->id = 0;
		$_SESSION['order_id'] = 0;
	}
	function checkout_resign() {
		global $database;
		$testID = $this->id;
		$testrugid = $database->query("SELECT * FROM `orders_rugs` WHERE `order_id`='".$testID."' LIMIT 1");
		$rugeid = $testrugid[0];
		$finRugID = $rugeid[rug_id];
		$finRugQTY = $rugeid[order_qty];

		#echo "RugID: ". $finRugID;
		#echo "<br>RugQTY: ". $finRugQTY;
		$database->execute("UPDATE `orders` SET `state`='cancelled' WHERE `id`='{$this->id}'");
		$database->execute("UPDATE `rugs` SET `sold`='0' WHERE `id`='".$finRugID."'");
		$database->execute("UPDATE `rugs` SET `stock`=`stock`+'".$finRugQTY."' WHERE `id`='".$finRugID."'");

		$this->id = 0;
		$_SESSION['order_id'] = 0;
	}

	function unlockOrderedItems($order_id) {
		$query = "SELECT * FROM `orders_rugs` WHERE `order_id` = '".$order_id."' AND `locked` = '1';";
		$res=mysql_query($query);
		while($row=mysql_fetch_array($res)) {
			$query = "UPDATE `rugs` SET `stock`=`stock`+'".$row['order_qty']."' WHERE `id` = '".$row['rug_id']."'";
			mysql_query($query);
		}
		$query = "UPDATE `orders_rugs` SET `locked` = '0' WHERE `order_id` = '".$order_id."';";
		mysql_query($query);
	}

	function getOrderedItems() {
		$query = "SELECT * FROM `orders_rugs` LEFT JOIN `rugs` ON `orders_rugs`.`rug_id`=`rugs`.`id` WHERE `orders_rugs`.`order_id` = '".$this->id."';";
		return mysql_query($query);
	}
	
}
?>

Anything would help to point me in the right direction.

 

Each rug has an ID, but when orders are processed there's a table called rugs_orders which stores order_id and rug_id for that order. I presume it's here's that's being used to store the rug_id or else a session variable?

 

I've used *** as it doesn't seem to allow me to change the colour of the code. Highlighted code is:

foreach ( $this->getItems() as $item )
$body .= 'RugID '.$item['id'] .' x '. $item['qty'] ."\n";

 

This works on the original ^^

 

 

foreach ( DO NO KNOW WHAT TO USE HERE )
$body .= "Rug Reference: ".
DO NOT KNOW WHAT TO USE HERE
."<br /><br />";

 

No idea what to use in the new one ^^

Link to comment
Share on other sites

the $cart object apparently contains the content/items in the cart. the basket object also contains an $items property that isn't used, at least within the posted code (whoever wrote this code didn't do a very good job.)

 

since the code is already looping over the contents of the $cart object, at about line 65, you should build the information you want to put into the email into a php variable within that loop, then simply put that php variable into your mail code later.

Link to comment
Share on other sites

since the code is already looping over the contents of the $cart object, at about line 65, you should build the information you want to put into the email into a php variable within that loop, then simply put that php variable into your mail code later.

 

Thanks - not really sure what you mean - can't see where there is a variable set for the rug/item_id within the code. Not sure what I should be adding in...

Link to comment
Share on other sites

without knowing what the $cart->get_items() method code fetches from the database, it's not directly possible to help. you could use print_r($item) to see what is being fetched from each row.

Up at that function printing $item['id'] gives me what I want... but how putting that into the string in the email brings up nothing. So how would I get it down to where the email is?

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.