私のデータベースからデータを選択し、これらの選択した値を変数に設定するコードがあります。PHP:変数の名前を動的に変更できますか?
しかし、何度もクエリを実行する必要があります。 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'];
これも可能ですを更新する方法ですか?それともそれは価値があるのですか?
ああ、私はそれらを混乱させてしまったので、決して適切に学習する必要はないと思います。ありがとう – NeverPhased
配列を使用すると、1つのクエリを作成してすべての値を1つの配列に格納し、各値を変数に割り当てることができますか? – NeverPhased
基本的に、$ pMarketShareと$ cMarketShareではなく、$ marketShares ['p']と$ marketShares ['c'] – KingCronus