Jump to content

Is there a better way to do this?


wolfcry

Recommended Posts

Hey all,

 

I'm building a pretty extensive calculator program for my users but I'm stuck on the best way to doe the math operation. I currently have all data stored in a database (such as the integers and the operation symbols +, -, *, / , % etc) because the calculator page is completely dynamic. The symbols are queried from the DB via id number.

 

Right now I'm using a switch statement to handle everything but that switch statement is huge, it's almost 24 cases long and I want to compact this into an array for functionality purposes and speed.

 

I've tried everything from eval() (which I do not want to use unless I absolutely have no other choice),  concatenating the variables together and while this shows what equation is supposed to take place, it doesn't perform the actual equation because the operator symbol is just a string. I'm truly stuck and have been researching a solution almost two days.

 

here's a clip of the switch and case depends upon which operation the user wishes to perform:

 

switch($Calc){
case 2:
$Total = $firstvalue + $secondvalue;
break;
case 3:
$Total = $firstvalue - $secondvalue;
break;
case 4:
$Total = $firstvalue * $secondvalue;
break;
case 5:
$Total = $firstvalue / $secondvalue;
break;
case 6:
$Total = $firstvalue % $secondvalue;
break;
default: echo 'No modifier selected!';
break;	
}

 

I said earlier, this is just a snippet of the switch, the original is 24 cases long and I really don't want to have to use a switch if I can get away with it but I'm not sure how to do this so I can use an array or a nested array loop.

 

Thank you for your help.

Link to comment
Share on other sites

Thanks MasterACE14 and scootash for your replies, it's appreciated.

 

While it may be the 'simpliest' method, is it the 'safest'? That's what I'm considered about regarding using it. The data is stored and dynamically built into selection boxes but still, those values can be changed using certain browser components now-a-days to include just about anything.

 

or am I just being unnecessarily paranoid?

Link to comment
Share on other sites

Sorry to bother you guys again, but I can't get this to function properly and I can't see where the error is coming from.

 

function ModifyCalc($sum, $ModType, $ModValue, $OptModType, $OptModValue){
$NewSum = 0;

if ($ModType >= 2 && $OptModType == 1){

	eval ('$NewSum = $sum' . $ModType . '$ModValue ;');

	return $NewSum;
}

 

To summarize, the user inputs the data in the calculator, hit submit and a function is called on that page, which you can see above, passing the data to the function parameters as you see above. The '$Opt' variables are only used (but do have default values) if the user wishes to further modify their calculation.

 

When I echo $NewSum, I receive 0 which means to me Eval return NULL which doesn't make sense.

 

When I test eval() by itself with constant values on a test page, it works but once I include it in a function or if() it fails, what am I doing wrong? I've checked the manual and I don't see anything wrong with my code.

Link to comment
Share on other sites

Sorry, I thought I posted the error, my bad. It is:

 

Parse error: syntax error, unexpected T_VARIABLE in C:\wamp\www\inc\functions_inc.php(110) : eval()'d code on line 1

 

and I'm calling the function like so:

 

if (($ModType != 1 && $ModValue != 0) || ($OptModType != 1 && $OptModValue != 0)) {

		echo ModifyCalc($sum, $ModType, $ModValue, $OptModType, $OptModValue);

	}

 

 

Link to comment
Share on other sites

are you missing the second curly brace in here?

function ModifyCalc($sum, $ModType, $ModValue, $OptModType, $OptModValue){
$NewSum = 0;

if ($ModType >= 2 && $OptModType == 1){

	eval ('$NewSum = $sum' . $ModType . '$ModValue ;');

	return $NewSum;
      }

} // to close the function

Link to comment
Share on other sites

Hey Kicken,

 

Ah, yes I was. I'm pulling the symbols from a db table using a reference id so I could use integer triggers in the actual script rather than hard coding everything.

 

I guess what I'll do now is store the symbols in an array and pull them out based on index value, which of course will be the reference id. I'll just have to change the code up a bit.

 

Thanks everyone for your help, I really appreciated it and all is working as it should now.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.