离子从数组中获取多个对象

时间:2016-10-18 13:20:33

标签: php angularjs ionic-framework

我有离子应用程序通过电子邮件发送订单详细信息电子邮件包括项目名称和大小和数量,但当购物车中有多个项目时它只发送汽车详细信息中的最后一项 这是我的控制器代码

.controller('checkoutCtrl', ['$scope', '$state', '$ionicPopup','$http','SERVER','sharedCartService',// The following is the constructor function for this page's controller. See https://docs.angularjs.org/guide/controller
// You can include any angular dependencies as parameters for this function
// TIP: Access Route Parameters for your page via $stateParams.parameterName
function ($scope, $state, $ionicPopup, $http, SERVER, sharedCartService) {
  $scope.cart=sharedCartService.cart;
  $scope.total_qty=sharedCartService.total_qty;
  $scope.total_amount=sharedCartService.total_amount;
  $scope.data = {};
var itemNames ={};

  $scope.submit = function(){
      var link = 'http://www.myweb.net/catzOrderEmail.php';

      for( var i = 0, len = $scope.cart.length; i < len; i++ ) {

    	   itemNames = $scope.cart[i].cart_item_title;
        alert(itemNames);
        

    	}

      $http.post(link, {
        Item_Name : itemNames ,//$scope.itemName,
        Item_size : $scope.data.Size,
        Item_qty : $scope.data.Qty,

        total_qty : $scope.total_qty,
        total_amount : $scope.total_amount,
        FirstName : $scope.data.FirstName,
        LastName : $scope.data.LastName,
        Email : $scope.data.Email,
        Country : $scope.data.Country,
        City : $scope.data.City,
        Street : $scope.data.Street,
        Zip : $scope.data.Zip,
        Phone : $scope.data.Phone
      }).then(function (res){
        this.myForm.reset();
        $state.go('home');
      });

  };

}])
<?php
require "phpmailer/class.phpmailer.php"; //include phpmailer class
//require_once("geoiploc.php"); // Must include this
require_once("conf.php");
    //http://stackoverflow.com/questions/15485354/angular-http-post-to-php-and-undefined
    $postdata = file_get_contents("php://input");
	if (isset($postdata)) {
    $headers  = "MIME-Version: 1.0\r\n";
    $headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
		$request  = json_decode($postdata);
    $SubInput = "New Order";

		$Item_Name = $request->Item_Name;
    $Item_size = $request->Item_size;
    $Item_qty = $request->Item_qty;
		$total_qty = $request->total_qty;
		$total_amount = $request->total_amount;
    $FirstName = $request->FirstName;
    $LastName = $request->LastName;
    $Email = $request->Email;
    $Country = $request->Country;
    $City = $request->City;
    $Street = $request->Street;
    $Zip = $request->Zip;
    $Phone = $request->Phone;


    $to 			= "info@catzgear.net";
    $hourdiff 		= "3";
    $timeadjust 	= ($hourdiff * 3600);
    $date 			= date("d-m-Y");
    $MsTime			= date("H:i:s",time() + $timeadjust);
    //$userip	 		= $_SERVER['REMOTE_ADDR'];
    //$countryName	= getCountryFromIP($userip, " NamE ");
    $message 		= "<table width='100%' border='0' cellspacing='0' cellpadding='0'>
      <tr>
        <td align='center' valign='top' bgcolor='#ababab' style='background-color:#ababab;'><br>
        <br>
        <table width='600' border='0' cellspacing='0' cellpadding='0'>
          <tr>
            <td align='left' valign='top'><img src='http://www.metalvoice.tv/images/maintop.png' width='600' height='17' style='display:block;'></td>
          </tr>
          <tr>
            <td align='left' valign='top' bgcolor='#ffffff' style='background-color:#ffffff;'>
    		<table width='570' border='0' align='center' cellpadding='0' cellspacing='0' style='margin-bottom:15px;'>
              <tr>
                <td align='left' valign='middle' style='padding:10px;'></td>
                </tr>
              </table>
              <table width='570' border='0' align='center' cellpadding='0' cellspacing='0' style='margin-bottom:15px;'>
                <tr>
                  <td width='360' align='left' valign='middle' style='font-family:Arial, Helvetica, sans-serif; color:#4e4e4e; font-size:13px; padding-right:10px;'>
                  <div style='font-size:20px;'>
    			Date:  $date<br><br>
    			Time:  $MsTime<br><br>
    			Item Name:  $Item_Name<br><br>
          Item size:  $Item_size<br><br>
          Item Qty:  $Item_qty<br><br>
          total qty: $total_qty<br><br>
          total Amount: $total_amount<br><br>
          First Name: $FirstName<br><br>
          Last Name: $LastName<br><br>
          Email: $Email<br><br>
          Country : $Country<br><br>
          City: $City<br><br>
          Street: $Street<br><br>
          Zip: $Zip<br><br>
    			Phone: $Phone <br>

                  </div>
                  </td>
                  <td align='right' valign='middle'><table width='210' border='0' cellspacing='0' cellpadding='0'>
                    <tr>
                    </tr>
                    <tr>
                    </tr>
                    <tr>
                    </tr>
                  </table></td>
                </tr>
                </table>
       </td>
          </tr>
    	    </tr>
          </table>

        <br>
        <br></td>
    </table> ";
		// if ($Item_Name != "" ||
    //     $FirstName != "" ||
    //     $LastName != "" ||
    //     $Email != "" ||
    //     $Country != "" ||
    //     $City != "" ||
    //     $Street != "" ||
    //     $Zip != "" ||
    //     $Phone != ""
    //     ) {
      // Instantiate Class
      $mail = new PHPMailer();
      // Set up SMTP
      $mail->IsSMTP();                // Sets up a SMTP connection
      $mail->SMTPAuth = true;         // Connection with the SMTP does require authorization
     // $mail->SMTPSecure = "ssl";      // Connect using a TLS connection
      $mail->Host = "mail.catzgear.net";  //Gmail SMTP server address
      $mail->Port = 25;  //Gmail SMTP port
      $mail->Encoding = '7bit';

      // Authentication
      $mail->Username   = "info@catzgear.net"; // Your full Gmail address
      $mail->Password   = $MyPass; // Your Gmail password

      // Compose
      $mail->SetFrom($Email, $FirstName);
      $mail->Subject = $SubInput;      // Subject (which isn't required)
      $mail->MsgHTML($message);

      // Send To
      $mail->AddAddress($to, "Catz Gear"); // Where to send it - Recipient
      $result = $mail->Send();		// Send!
    $message = $result ? 'Successfully Sent!' : 'Sending Failed!';
    unset($mail);

	 }
	// else {
	// 	echo "Sending Failed!";
	// }
?>

如何获取购物车中的所有商品名称

最好的问候

1 个答案:

答案 0 :(得分:0)

那是因为你只发送最后一个元素在你的API中,for循环首先执行,最后只有最后一个元素 itemNames,你发送给php。

所以你可以做两件事,

  1. 在循环内调用ajax(不好)

  2. 将所有这些元素推到一起然后发送。

    itemNames.push($ scope.cart [I] .cart_item_title);

  3. 为此确保itemNames是一个数组。并使用此数组对象在php中执行您的操作。

    希望这对你有意义