0

我有以下 PHP 代码:

echo "var s1 = [";
$count = 0;
while($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC))
  {
  if ($count++ > 0) echo ", ";
  echo $row['OrdersBal'];
  }
echo "];\n";
echo "var ticks = [";
$count2 = 0;
while($row2 = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC))
  {
  if ($count2++ > 0) echo ", ";
  echo "'" . $row2['CardName'] . "'";
  }         
echo "];\n";

它目前正在输出:

var s1 = [37966.550000, 19876.170000, 17314.580000, 15614.410000, 7575.000000];
var ticks = [];

但我希望它输出:

var s1 = [37966.550000, 19876.170000, 17314.580000, 15614.410000, 7575.000000];
var ticks = ['Parameter Technology', 'Earthshaker Corporation', 'Microchips', 'Mashina Corporation', 'SG Electronics'];

如果我移动第二个 while 语句使其首先出现,那么该语句将正确输出。这让我相信我不能以这种方式连续执行两个 while 语句,但我不确定我的替代方案是什么。谢谢你的帮助!

4

1 回答 1

2

在循环期间使用变量存储文本,然后将它们打印出来:

$count = 0;
$orders = "";
$cardNames = "";
while($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC))
{
    if ($count++ > 0)
    {
        $orders .= ", ";
        $cardNames .= ", ";
    }
    $orders .= $row['OrdersBal'];
    $cardNames .= $row['CardName'];
}

echo "var s1 = [" . $orders . "];\n";
echo "var ticks = [" . $cardNames . "];\n";
于 2013-03-17T20:02:53.573 回答