I have code which selects data from my database and then sets these selected values to a variable.
However I need to run the query many times. I am wondering if I could use a while/for loop to run the query the x number of times and use a switch function to change the variable names accordingly? I am not sure if this is even worth doing let alone possible, any thoughts? Thanks.
Below is the code I am trying to achieve which works fine.
////////////////////////////////////////////////////////////////////////////////////////////////////
$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'];
}
?>
But rather than do that I am trying to run the query through a loop with the variable names updating.
<?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'];
}
The problem is how to update the variable names
$pBalance = $row['pOutputValue'];
$cBalance = $row['cOutputValue'];
Is this even possible? Or is it even worth doing?