【问题标题】:How to send array from classic asp to table in php page?如何将数组从经典 asp 发送到 php 页面中的表?
【发布时间】:2012-03-02 06:49:05
【问题描述】:

我在 ASP 页面中进行 SQL 查询,得到 100 行和 7 列的数据。

例如,我从我的 URL 中的查询字符串或我提交为 POST / GET 的表单中读取,

我得到这个:“myexample.com/order.php?month=2012-03”,我在 ASP 页面中使用“2012-03”作为我的值来运行查询“SELECT * FROM CustomerSalesOrder WHERE date LIKE '2012-03%'

现在,如上所述,我得到了 100 行和 7 列关于我的客户在 3 月份购买了什么的数据。

如何将这些数据转换成数组并传递给PHP页面以生成表格并在表格上显示数据?

(我知道我可以用 1 种 Web 编程语言运行一切。但是在 ASP 页面中运行查询的步骤已修复,不能更改;在 PHP 页面上显示数据的步骤也已修复。位于 ASP 页面在 myexample.com 中,而 PHP 页面位于 mywork.com)

请指导我一步一步:

  • 将我的数据转储到 ASP 页面中的数组中,然后
  • 如何将我的数据(以数组形式)传递给 PHP 页面,以正确​​格式化的表格形式显示数据

希望高手指导我在ASP中将数据转换成数组,以及将数组传递到PHP页面的步骤。

那么,在 PHP 页面中,如何分解数组中的每个数据或将数据分配给变量的值? (我想需要使用循环,在这种情况下任何循环方法都是最好的?)一旦我以循环或其他方式获得值,然后再次循环到表中。

【问题讨论】:

    标签: php sql arrays asp-classic parameter-passing


    【解决方案1】:

    我可以建议您的 asp 页面将 SQL 数据的“数组”输出为 XML,而 PHP 页面只是读取 XML(使用 CURL)并将 XML 解析为 php 数组?

    将 SQL 数据输出为 XML:

    ''#### Build SQL Query to grab data as XML (you may want to put this in a stored procedure
    dim SqlStr
    SqlStr = "SELECT CONVERT(VARCHAR(MAX), ( SELECT [ColA] AS 'A', [ColB] AS 'B', [ColC] AS 'C', [ColD] AS 'D', [ColE] AS 'E', [ColF] AS 'F', [ColG] AS 'G'" & _
             "                 FROM    CustomerSalesOrder " & _
             "                 WHERE [Date] LIKE '2012-03%' " & _
             "               FOR " & _
             "                 XML PATH('R') , " & _
             "                     ROOT('ROOT') " & _
             "               )) as XML"
    
    set rs = db.execute(SqlStr)
    
    do while not rs.eof
        XmlStr = rs("XML")
        rs.movenext
    loop
    
    ''#### XML Headers
    Response.CacheControl = "no-cache"
    Response.AddHeader "Pragma", "no-cache"
    Response.Expires = -1
    Response.ContentType = "text/xml"
    Response.Write "<?xml version='1.0' encoding='utf-8'?>"
    Response.Write XmlStr
    

    PHP 页面可以这样读取:

    $URL = "URL to the ASP page";
    $ch  = curl_init();
    curl_setopt($ch, CURLOPT_URL, $URL);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1");
    $output = trim(curl_exec($ch));
    $xml = simplexml_load_string($output);
    
    //#### Prep Array
    var $TargetArray[];
    $ArrayPointer = 0;
    
    //#### Parse XML
    foreach($xml->children() as $child) {
        $ColA = trim($child->A);
        $ColB = trim($child->B);
        $ColC = trim($child->C);
        $ColD = trim($child->D);
        $ColE = trim($child->E);
        $ColF = trim($child->F);
        $ColG = trim($child->G);
    
        //#### Push values into your array (This may be buggy, my php & multi dimensional arrays is a tad rusty)
        $row = array('ColA' => $ColA, 'ColB' => $ColB, 'ColC' => $ColC, 'ColD' => $ColD, 'ColE' => $ColE, 'ColF' => $ColF, 'ColG' => $ColG); 
        $TargetArray[$ArrayPointer] = $data;
        $ArrayPointer++;
    }
    

    或者,如果您只想将 CURL XML 喷射到 HTML 表格中,您可以按如下方式执行此操作:

    //#### Parse XML from CURL into HTML Table
    echo "<table>";
    echo "<tr>";
    echo "  <th>ColA</th>";
    echo "  <th>ColB</th>";
    echo "  <th>ColC</th>";
    echo "  <th>ColD</th>";
    echo "  <th>ColE</th>";
    echo "  <th>ColF</th>";
    echo "  <th>ColG</th>";
    echo "</tr>";
    foreach($xml->children() as $child) { 
        echo "<tr>";
        echo "  <th>" . trim($child->A) . "</th>";
        echo "  <th>" . trim($child->B) . "</th>";
        echo "  <th>" . trim($child->C) . "</th>";
        echo "  <th>" . trim($child->D) . "</th>";
        echo "  <th>" . trim($child->E) . "</th>";
        echo "  <th>" . trim($child->F) . "</th>";
        echo "  <th>" . trim($child->G) . "</th>";
        echo "</tr>";
    }
    echo "</table>";
    

    我确信这需要一些调整,但它应该足以让您同时使用它的 PHP 和 ASP 方面。

    【讨论】:

    • 对于 ASP 代码,您应该确保 Response.Codepage 是 65001。
    【解决方案2】:

    你可以这样做:

    在您的 ASP 页面中,您可以添加一个条件,如果存在 json 参数,您将来自查询的数据显示为 JSON(仅 JSON 代码,无 html)。

    在 PHP 页面显示来自 ASP 页面的数据:

    $aspData = json_decode(file_get_contents('http://myexample.com/order.php?month=2012-03&json=1'), true);
    echo "<table>";
    foreach($aspData as $row){
      echo "<tr>";
      foreach($row as $colName => $colVal){
        echo "<td>$colVal</td>";
      }
      echo "</tr>";
    }
    echo "</table>";
    

    我不懂 ASP,但应该有将数组转换为 JSON 字符串的功能。

    【讨论】:

    • aspjson.com 可以工作,但该语言没有内置任何内容。 JSON 在当时并不流行,微软将所有精力都集中在 XML 上。
    猜你喜欢
    • 2011-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-29
    • 2011-03-22
    • 2012-06-05
    • 1970-01-01
    相关资源
    最近更新 更多