Jump to content

Recommended Posts

Hy guys as usual the unusal is on my mind  ;D

 

i am still on the CMS i ma making and some tooths are on my mind.... i have a table in which i store some file data and some table which is used for the storage of the locations, trust me it has to be that way because the files have to fulfill some criteria to be stored on different locations, like min size, max size resolution extension proportions and so on... now in the other table i have to store in one row the sorting criteria.... originally i tooth just to use a query which is stored in the location table, but then i realized that when i generate the entry in the data table i would have to perform again a query to figure out where the location is because i would query against the data i just enteed to figure out its storage location... now figure how that efficient is when you have 2mil entry`s in the data table...

 

So i figured i would store the comparing values in a string which would be processed in php:

 

something like:

'row + row1' or 'row / row1'

 

and because of that i figure i could even end up using something like '( row / row1) * a' or even '( row / row1) * 5'

 

that are all normal operations which would be pretty easy to implement and i am asking if there is a function or already written function which duos that either in mysql or PHP.

 

I could write the function alone but i am a bit uncertain how i would realize the ( ) function.....

 

Link to comment
https://forums.phpfreaks.com/topic/198259-how-can-i-calculate-this/
Share on other sites

http://php.net/manual/en/function.eval.php

http://php.net/manual/en/function.str-replace.php

 

$expression ="@row1 * @row2";

 

You will need to make a table that stores the text that will be replaced with a query such as "@row1" => "row1"

 

so when your sring "@row1 * @row 2"

do a dump of that table that results in:

$evals["key"]=array ("@row1", "@row2");

$evals["arg"]=array("row1","row2");

and build a function to:

return eval(str_replace($expression,$evals["key"],$evals["arg"] ));

 

I'm sure a better naming convention could have done this example wonders.

 

I realized that I forgot to include the secondary lookup on the query:

 

so let $content=array ("row1" => 8, "row2" => 10);

this should be a fetched assoc relating to the actual query you are trying to generate information from, should be a seperate table from the one outlined earlier--that table is just for translating a code into a row value, that way your rows don't have to be unique to what evaluation you are attempting (this may be too much overhead, depending on implementation).

 

return eval(str_replace($expression,$evals["key"],$content[$evals["arg"]] ));

I think the primary thing you are looking for is eval() which will take a string and do the mathematical evaluation. 

 

How I took your request is best served as an example:

 

table_name
row1              row2                 row3                  row4
10                     15                     30                    "row1*row2/row3"

For simplicity sake I am taking out the equivalence table with the "@" because it probably isn't necessary.

 

If you are looking for a function to evaluate row4, you could populate $content from a sql query:

$content=mysql_fetch_assoc(mysql_query("SELECT row1, row2, row3, row4 from table_name"));

 

now what you would need to do is:

1) create an associative array of all the rows => values from the query that are in the string to be evaluated.  I am doing this using prior knowledge, but with a few tricks you could discern dynamically what is in the expression, and just build it out.  since there is only one equation in this example, and it happens to be at the end I am going to pop it off, and then work with it, then push it back:

 

$eval=array_pop($content);

So now we have $eval="row1*row2/row3"

$content=Array("row1" => 10, "row2" => 15, "row3" = 30)

 

So... all we need to do is the following:

 

$row4_evaluated=eval(str_replace($eval, array_keys($content), array_values($content)));
$content["row4"]=$row4_evaluated;

 

Is that a better explanation?

 

 

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.