我在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种网页编程语言运行所有内容。但是在ASP页面中运行查询的步骤已经修复,无法更改;并且在PHP页面上显示数据的步骤也已修复.PAS页面位于在 myexample.com 中,而PHP页面位于 mywork.com )
请指导我一步一步:
我希望专家能够指导我在ASP中将数据转换为数组,以及将数组传递到PHP页面的步骤。
然后,在PHP页面中,如何分解数组中的每个数据或将数据分配给变量的值? (我想需要使用循环,在这种情况下,任何循环方法都是最好的?)一旦我以循环或其他方式获得值,然后再循环到表中。
答案 0 :(得分:2)
我建议您的asp页面将SQL数据的“数组”输出为XML,PHP页面只读取XML(使用CURL)并将XML解析为php数组?
以XML格式输出SQL数据:
''#### 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&amp; ASP方面。
答案 1 :(得分:1)
你可以这样做:
在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字符串的函数。