1

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?

NeverPhased
  • 1,546
  • 3
  • 17
  • 31

2 Answers2

3

Why not just use an array to hold the values? Then the problem becomes trivial.

KingCronus
  • 4,509
  • 1
  • 24
  • 49
  • ah ok ill look into that, I never use or learned arrays properly as I was confused by them, maybe I need to go learn them properly. Thanks – NeverPhased Dec 07 '11 at 10:59
  • So using an array I could make one query and store all the values in a single array then assign each value to a variable? – NeverPhased Dec 07 '11 at 11:01
  • Basically, instead of $pMarketShare and $cMarketShare you could have: $marketShares['p'] and $marketShares['c'] – KingCronus Dec 07 '11 at 11:08
1

You can do it like this

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

But overall it will not be very handy, arrays are probably better for such cases.

Nameless
  • 2,306
  • 4
  • 23
  • 28