繁体   English   中英

更新用于在 PHP 中解析 XML 的 URL

[英]Updating URL for parsing XML in PHP

我想弄清楚如何更新我用来解析 XML 的 URL(使用simplexml_load_file() )。

有一个button有一个$productid ,当用户添加他们的短代码[show_cart_button productid=#]时,该button由用户分配给它。

//code for the button input that gets called    

function print_add_cart_button($productid, $atts = array()) {
   //some other form code for the button

   $replacement .= '<input type="hidden" name"cart_product" value"' . $productid . '"/>';
  return $replacement;
}

这是我用来显示和调用产品信息的function

function show_shopping_cart_handler($atts) {
   if (isset($_POST['cart_product'])) {
      $id = "&PRODUCTID=" . $_POST['cart_product'];
    // uses the input name from the button
   }

   $url = "https://secure.bmtmicro.com/cart?CID=2/WP" . $id;
   
   $xml = simplexml_load_file($url) or die("error.");

   foreach ($xml->producttable->row as $product) {
      $output .= '<span>' . $product->productname . '</span>';
   }

   return $output;
}

URL 正常工作的方法是将&PRODUCTID=(product#)添加到 URL 的末尾。 例如,如果您单击两个不同的按钮,则 URL 应如下所示: https://secure.bmtmicro.com/cart?CID=2/WP&PRODUCTID=1&PRODUCTID=2 : https://secure.bmtmicro.com/cart?CID=2/WP&PRODUCTID=1&PRODUCTID=2 2/ https://secure.bmtmicro.com/cart?CID=2/WP&PRODUCTID=1&PRODUCTID=2 1 https://secure.bmtmicro.com/cart?CID=2/WP&PRODUCTID=1&PRODUCTID=2 2(显然 1 和 2 将需要显示按钮$productid编号)。

从上面的代码中您应该可以看到,每次单击button时,它只会重写 URL 并显示该产品,而不是向列表中添加新产品。 我尝试将simplexml_load_file()设置为foreach() ,但是单击按钮时不会显示任何产品。 有谁知道我可以让该 URL 更新/正常工作的方法吗?

每次

 if (isset($_POST['cart_product'])) {
      $id = "&PRODUCTID=" . $_POST['cart_product'];
    // uses the input name from the button
   }

被调用,它正在覆盖 $id。 您需要获取 url 的任何现有值。

您必须获取所有当前值($_GET['cart_productions']),将它们放入一个数组中,获取新帖子,将其添加到该数组中,然后使用 http_build_query() 将所有这些值添加到 url 中.

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM