Setting Variable as a dynamic string/variable combo

Hello all,

This question has been puzzling me for quite some time so I figured I would post it to a forum.

I have a sql statement that pulls a row in which one column is called DSC1_Movement. This column has a wide variety of outputs all of which change what formula it looks for.

I want to set $totalworkdoneftlbs1p equal to “$” . $row[‘DSC1_Movement’] . “movefeetlbspr”

Then I want the ability to use the new variable “$” . $row[‘DSC1_Movement’] . “movefeetlbspr” as $totalworkdoneftlbs1p to multiply that by a given number

Example DSC1_Movement = pullups
$totalworkdoneftlbs1p = $pullupsmovefeetlbspr
$totalworkdoneftlbs1 = $totalworkdoneftlbs1p * 24

I can never get the above to work because it does not realize “$” . $row[‘DSC1_Movement’] . “movefeetlbspr” as a variable.

Thanks,
Andrew

You assign a value to a variable like this

$var = ‘value’;

Why is that hard to follow?

Post your code.

I can’t put all of the previous code on here but i’ll try to be more complete in my explanation.

Selecting a single row from the database based on user selections.
Within the single row there is a column named DSC1_Movement that we want to know what the value is.
This value can be 83 different things. For this example it will = Pullups

Then these values are received from a desperate pull:
[php]$heightcalc = $rowpcaf[‘height_inches’];
$weightcalc = $rowpcaf[‘weight_lbs’];

					//length
					$headandnecklength = $rowpcaf['head_and_neck'];
					$torsolength = $rowpcaf['torso'];
					$pelvislength = $rowpcaf['pelvis'];
					$femurlength = $rowpcaf['femur'];
					$tibialength = $rowpcaf['tibia'];
					$ankletogroundlength = $rowpcaf['ankle_to_ground'];
					$footlength = $rowpcaf['foot'];
					$humeruslength = $rowpcaf['humerus'];
					$forearmlength = $rowpcaf['forearm'];
					$handlength = $rowpcaf['hand'];
					
					//Component Weight (WPI * Length)
					$headandneckcalc = 0; //need to translate this
					$torsocalc = ($torsolength * 2.89861632);
					$pelviscalc = ($pelvislength * 2.708574744);
					$femurcalc = ($femurlength * 2.781898001);
					$tibiacalc = ($tibialength * 1.063205488);
					$ankletogroundcalc = 0;
					$footcalc = ($footlength * .272112299);
					$humeruscalc = ($humeruslength * .733138386);
					$forearmcalc = ($forearmlength * .559867673);
					$handcalc = ($handlength * .264867324);

Then I submit these statements
$pullupmovebwtwm = $weightcalc - $handcalc - $forearmcalc;
$pushupmovebwtwm = $weightcalc - $footcalc - $forearmcalc - $handcalc;
$situpmovebwtwm = $headandneckcalc + $torsocalc + $pelviscalc;
$squatmovebwtwm = $femurcalc + $pelviscalc + $torsocalc + $headandneckcalc + $humeruscalc + $forearmcalc + $handcalc;

//BW Total Weight Moved Part 2
$pullupmovebwtwm2 = 0;
$pushupmovebwtwm2 = 0;
$situpmovebwtwm2 = 0;
$squatmovebwtwm2 = 0;

//BW Distance Moved Part 1
$pullupmovebwdm = ($humeruslength + $forearmlength)/12;
$pushupmovebwdm =($humeruslength)/12;
$situpmovebwdm = (($headandnecklength + $torsolength)/12)/2;
$squatmovebwdm = ($femurlength)/12;

//BW Distance Moved Part 2
$pullupmovebwdm2 = 0;
$pushupmovebwdm2 = 0;
$situpmovebwdm2 = 0;
$squatmovebwdm2 = 0;

//Weight Distance Moved
$pullupmovewdm = ($humeruslength + $forearmlength)/12;
$pushupmovewdm = ($humeruslength)/12;
$situpwdm = (($headandnecklength + $torsolength)/12)/2;
$squatwdm = ($femurlength)/12;

//Total
$Pullupsmovefeetlbspr = ($pullupmovebwtwm * $pullupmovebwdm) + ($pullupmovebwtwm2 * $pullupmovebwdm2);
$pushupsmovefeetlbspr = ($pushupmovebwtwm * $pushupmovebwdm) + ($pushupmovebwtwm2 * $pushupmovebwdm2);
$situpsmovefeetlbspr = ($situpmovebwtwm * $situpmovebwdm) + ($situpmovebwtwm2 * $situpmovebwdm2);
$squatsmovefeetlbspr = ($squatmovebwtwm * $squatmovebwdm) + ($squatmovebwtwm2 * $squatmovebwdm2);[/php]

These only have 4 of the possible movements for testing purposes.

Then I want to submit this:
[php]$totalworkdoneftlbs1p = “$” . $row[‘DSC1_Movement’] . “movefeetlbspr”;
$totalworkdoneftlbs1 = $totalworkdoneftlbs1p * $row[‘DSC1_Reps’];
echo $totalworkdoneftlbs1;
[/php]
and have the echo be the calculation $Pullupsmovefeetlbspr.

Does that clarify what I am trying to do?

The code doesn’t help. In plain english, what are you trying to do?

So far, all I understand is that you want the type of exercise that is stored in the DSC1_Movement column.

I want to set a variable equal to a string and a dynamic field. Then I want that variable to recognize that it is equal to the name of another variable and output that value. The dynamic field will only be known after a call to the database is made.

$csometext = 10

Dynamic Field = c

$a = “$” . Dynamic Field . “sometext”
$b = $a * 100
echo $a = 1000

That is how you are trying to implement it. Think more abstractly. What are you actually trying to accomplish?

[ol][li]Get value from database that is stored in column XYZ.[/li] [li]Multiply that value * 100 to show rep count.[/li] [li]display rep count to page.[/li][/ol]

Something more like that.

I wish I could do it like that the problem is that every single movement that can come out of the column requires completely different calculations thus the need to be able have it look to what movement was pulled and select the appropriate variable. The only option if the below does not work is 830 lines of if statements or hardcoding the calculations into the database, neither of which I want to do.

The members here have ALOT of experience in programs more complicated than yours. If we know what you are trying to do, it is far easier to come up with a solution than a malformed hatchet job. A dynamic variable is not needed. How the system is designed, makes it more scalable.

And actually, hardcoding the calculations in a database, is no different than hardcoding them into the code itself.

Alright while I will attempt to do this a different way. Can a dynamic variable be written in php like the one I stated above. I cannot find any examples of it on the web.

Forget you have or know any code. What is the big picture. You are asking for answers for code of how you think it should be done. Let us decide the how, just explain the what.

Your cryptic column and variable names don’t help whatsoever. You need to use much better naming and use underscores_to_seperate_words. What is DSC1 or rowpcaf or bsp?

We are very experienced and still cannot understand what you are doing. The best I can get out of it is that it has something to do with working out.

Assuming I know what you are after, which is really hazy, take this:

[php]$totalworkdoneftlbs1p = “$” . $row[‘DSC1_Movement’] . “movefeetlbspr”;
$totalworkdoneftlbs1 = $totalworkdoneftlbs1p * $row[‘DSC1_Reps’];
echo $totalworkdoneftlbs1; [/php]

$totalworkdoneftlbs1p is not very descriptive, so I don’t know what it stands for, but:

$row[‘DSC1_Movement’] holds a string, you said in the example pullups. So, we will use that.

[php]$exercise = $row[‘DSC1_Movement’];
$reps = $totalworkdoneftlbs1p * $row[‘DSC1_Reps’];
echo “$exercise: $reps”;[/php]
Would in theory give something like,

Pullups: 20

But, this part of the naming convention, ftlbs, makes me think you are determining the foot pounds of the workout.

Sponsor our Newsletter | Privacy Policy | Terms of Service