0

我有一堆要导出到 XML 文件的 SESSION 变量。客户详细信息很好,因为它们不是 foreach 循环的一部分。但是,我客户的订单是 foreach 循环的一部分。

SESSION 变量 $_SESSION['invoice'] 在 cart.php 中定义。$_SESSION['invoice'] 数组中的每个单独的值都需要检索并放入 XML。

例如

<title1>comic title1</title1><qty1>10</qty1><price1>$2.50</price1><cost1>$25.00</cost1><title2>comic title2</title2><qty2>5</qty2><price2>$2.00</price2><cost2>$10.00</cost2>

购物车.php 代码:

$_SESSION['invoice'][$comic_id]=$name . " " . $qty . " $" . $price . " $" . $cost;

xml.php 代码:

<?php
if(!isset($_SESSION)) {
     session_start();
}
foreach ($_SESSION['invoice'] as $value);

$test_array = array (
  $_SESSION['firstname'] => 'firstname',
  $_SESSION['lastname'] => 'lastname',
  $_SESSION['email'] => 'email',
  $_SESSION['addressline1'] => 'addressline1',
  $_SESSION['towncity'] => 'towncity',
  $_SESSION['postcode'] => 'postcode',
  'order' => array (
  $_SESSION['total'] => 'total',
  $_SESSION['invoice']  => 'order',
  ),
);
$xml = new SimpleXMLElement('<customer/>');
array_walk_recursive($test_array, array ($xml, 'addChild'));
print $xml->asXML();
4

2 回答 2

0

$order = json_encode($_SESSION['invoice']);

于 2012-11-12T09:40:20.363 回答
0
    $_SESSION['invoice'][$comic_id]=$name . " " . $qty . " $" . $price . " $" . $cost;

使用此代码,您可以连接字符串。尝试调试创建的值,例如:

    echo '<pre>'.print_r($_SESSION['invoice'][$comic_id], 1).'</pre>';
    echo '<pre>'.print_r($_SESSION['invoice'], 1).'</pre>';

你必须创建这样的东西:

    $_SESSION['invoice'][$comic_id]['firstname'] = 'Firstname';
    $_SESSION['invoice'][$comic_id]['price'] = $myPrice';
    // and so on...

但要注意。恕我直言,不建议您参加此类发票处理的会议!

于 2012-11-12T08:51:01.683 回答