1

データベースからデータを選択し、選択した値を変数に設定するコードがあります。

ただし、クエリを何度も実行する必要があります。while/for ループを使用してクエリを x 回実行し、スイッチ関数を使用してそれに応じて変数名を変更できるかどうか疑問に思っています。これが可能であるどころか、行う価値があるかどうかはわかりませんが、何か考えはありますか? ありがとう。

以下は、私が達成しようとしているコードで、正常に動作します。

////////////////////////////////////////////////////////////////////////////////////////////////////
$output1 = mysql_query("

SELECT `pOutputValue`,`cOutputValue` 
FROM `output` WHERE `teamID` = '$teamID' && `period` = '$currentStage' && `outputType`      = 'balance'

")or die($output1."<br/><br/>".mysql_error());

//assign the text located at the logo field (the path of the logo) to a variable     $slogan
while($row = mysql_fetch_assoc($output1)) 
{
$pBalance = $row['pOutputValue'];
$cBalance = $row['cOutputValue'];

}



////////////////////////////////////////////////////////////////////////////////////////////////
$output2 = mysql_query("

SELECT `pOutputValue`,`cOutputValue` 
FROM `output` WHERE `teamID` = '$teamID' && `period` = '$currentStage' && `outputType`     = 'marketShare'

")or die($output2."<br/><br/>".mysql_error());

//assign the text located at the logo field (the path of the logo) to a variable $slogan
while($row = mysql_fetch_assoc($output2)) 
{
$pMarketShare = $row['pOutputValue'];
$cMarketShare = $row['cOutputValue'];

}
//////////////////////////////////////////////////////////////////////////////////////////////////
$output3 = mysql_query("

SELECT `pOutputValue`,`cOutputValue` 
FROM `output` WHERE `teamID` = '$teamID' && `period` = '$currentStage' && `outputType` = 'salePrice'

")or die($output3."<br/><br/>".mysql_error());

//assign the text located at the logo field (the path of the logo) to a variable $slogan
while($row = mysql_fetch_assoc($output3)) 
{
$pSalePrice = $row['pOutputValue'];
$cSalePrice = $row['cOutputValue'];

}

?>

しかし、そうするのではなく、変数名を更新してループを介してクエリを実行しようとしています。

<?php

$i = "0";

while($i<4)

{

switch ($i)

case "0";
$type = "balance";
break;

case "1";
$type = "marketShare";
break;

case "2";
$type = "salePrice";
break;

case "3";
$type = "unitPrice";
break;



$output = mysql_query("

SELECT `pOutputValue`,`cOutputValue` 
FROM `output` WHERE `teamID` = '$teamID' && `period` = '$currentStage' && `outputType` = '$type'

")or die($output."<br/><br/>".mysql_error());

//assign the text located at the logo field (the path of the logo) to a variable $slogan
while($row = mysql_fetch_assoc($output)) 
{
$c$type = $row['pOutputValue'];
$p$type = $row['cOutputValue'];


}

問題は、変数名を更新する方法です

  $pBalance = $row['pOutputValue'];
  $cBalance = $row['cOutputValue'];

これは可能ですか?それともやる価値はありますか?

4

2 に答える 2

3

配列を使用して値を保持しないのはなぜですか? その後、問題は簡単になります。

于 2011-12-07T10:57:52.480 に答える
1

あなたはこのようにすることができます

$name = 'c' . $type;
$$name = $row['cOutputValue'];
$name = 'p' . $type;
$$name = $row['pOutputValue'];

しかし、全体的にはあまり便利ではありません。そのような場合には、おそらく配列の方が適しています。

于 2011-12-07T11:00:09.397 に答える