Question

J'ai du code qui sélectionne les données de ma base de données, puis définit ces valeurs sélectionnées sur une variable.

Cependant, je dois exécuter la requête plusieurs fois. Je me demande si je pouvais utiliser un temps / pour Loop pour exécuter la requête du nombre x et utiliser une fonction de commutation pour modifier les noms de variable en conséquence? Je ne sais pas si cela vaut même la peine de faire et encore moins possible, des pensées? Merci.

Vous trouverez ci-dessous le code que j'essaie de réaliser, ce qui fonctionne bien.

////////////////////////////////////////////////////////////////////////////////////////////////////
$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'];

}

?>

Mais plutôt que de faire cela, j'essaie d'exécuter la requête via une boucle avec la mise à jour des noms de variables.

<?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'];


}

Le problème est de savoir comment mettre à jour les noms de variables

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

Est-ce seulement possible? Ou cela vaut-il même la peine de faire?

Était-ce utile?

La solution

Pourquoi ne pas simplement utiliser un tableau pour maintenir les valeurs? Alors le problème devient trivial.

Autres conseils

Tu peux le faire comme ça

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

Mais dans l'ensemble, ce ne sera pas très pratique, les tableaux sont probablement meilleurs pour de tels cas.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top