Connecting Tech Pros Worldwide Help | Site Map

How make value of field to be mathematical equation

w33nie's Avatar
Member
 
Join Date: Jan 2007
Location: Sydney
Posts: 56
#1: Feb 10 '07
My table is pretty well complete, but I would prefer it if the value for Points could be turned into a mathematical equation,
and this equation would use the data from the other fields in the table to achieve a value.

I want this done in phpMyAdmin 2.7, if possible, so that when I change the values in other fields, the Points variable changes, due to its equation.

The equation I would want would be something like:
"won * 3 + drawn * 1"
(won and drawn are 2 other fields in my database)
and have this equation become the value of my Points field.

and would it be possible to have more than one variable as an equation?


I doubt you'll need the php code from my page, but here it is anyway.
Expand|Select|Wrap|Line Numbers
  1. <? $hostname = "***.secureserver.net"; // The mySQL DB server. 
  2. $username = "acssl_table"; // The username you created for this database. 
  3. $password = "Victoria3"; // The password you created for the username. 
  4. $usertable = "table"; // The name of the table you made. 
  5. $dbName = "acssl_table"; // This is the name of the database you made. 
  6.  
  7. MYSQL_CONNECT($hostname, $username, $password) OR DIE("DB connection unavailable");
  8. @mysql_select_db( "$dbName") or die( "Unable to select database"); 
  9. ?> 
  10. <? 
  11. //error message (not found message)begins 
  12. $XX = "No Record Found, to search again please close this window"; 
  13. //query details table begins
  14. $query = mysql_query("SELECT * FROM `table` ORDER BY points DESC, g_difference DESC, g_for DESC LIMIT 0, 30 "); 
  15. while ($row = @mysql_fetch_array($query)) 
  16. $variable1=$row["team"];
  17. $variable2=$row["played"]; 
  18. $variable3=$row["won"]; 
  19. $variable4=$row["drawn"];
  20. $variable5=$row["lost"];
  21. $variable6=$row["g_for"]; 
  22. $variable7=$row["g_against"]; 
  23. $variable8=$row["g_difference"]; 
  24. $variable9=$row["points"];
  25. //table layout for results 
  26.  
  27. print ("<tr>");
  28. print ("<td width='150'><span class='BodyTable'>$variable1</span></td>");
  29. print ("<td width='50' align='center'><span class='BodyTable'>$variable2</span></td>");
  30. print ("<td width='50' align='center'><span class='BodyTable'>$variable3</span></td>");
  31. print ("<td width='50' align='center'><span class='BodyTable'>$variable4</span></td>");
  32. print ("<td width='50' align='center'><span class='BodyTable'>$variable5</span></td>");
  33. print ("<td width='50' align='center'><span class='BodyTable'>$variable6</span></td>");
  34. print ("<td width='50' align='center'><span class='BodyTable'>$variable7</span></td>");
  35. print ("<td width='75' align='center'><span class='BodyTable'>$variable8</span></td>");
  36. print ("<td width='75' align='center'><span class='BodyTable'>$variable9</span></td>");
  37. print ("</tr>"); 
  38. }
  39. //below this is the function for no record!!
  40. if (!$variable1)
  41. print ("$XX");
  42. //end 
  43. ?>
ronverdonk's Avatar
Moderator
 
Join Date: Jul 2006
Location: The Netherlands
Posts: 4,139
#2: Feb 11 '07

re: How make value of field to be mathematical equation


Simply: you cannot.

A database is to hold (atomic) data and is not a replacement for a spreadsheet.

Any calculations on that data can be done using an SQL statement with the builtin functions..

Ronald :cool:
w33nie's Avatar
Member
 
Join Date: Jan 2007
Location: Sydney
Posts: 56
#3: Feb 12 '07

re: How make value of field to be mathematical equation


how would I be able to get those fields to become math equations then?
ronverdonk's Avatar
Moderator
 
Join Date: Jul 2006
Location: The Netherlands
Posts: 4,139
#4: Feb 12 '07

re: How make value of field to be mathematical equation


Quote:

Originally Posted by w33nie

how would I be able to get those fields to become math equations then?

Like this, just SQL arithmetic:
Expand|Select|Wrap|Line Numbers
  1. mysql> select * from test;
  2. +------+--------+------+-------+
  3. | id   | team   | won  | drawn |
  4. +------+--------+------+-------+
  5. |    1 | Team 1 |    3 |     2 |
  6. |    2 | Team 2 |    4 |     0 |
  7. |    3 | Team 3 |    0 |     4 |
  8. +------+--------+------+-------+
  9. 3 rows in set (0.00 sec)
  10.  
  11. mysql> select team, (won*3+drawn*1) as total from test;
  12. +--------+-------+
  13. | team   | total |
  14. +--------+-------+
  15. | Team 1 |    11 |
  16. | Team 2 |    12 |
  17. | Team 3 |     4 |
  18. +--------+-------+
  19. 3 rows in set (0.00 sec)
Ronald :cool:
w33nie's Avatar
Member
 
Join Date: Jan 2007
Location: Sydney
Posts: 56
#5: Feb 13 '07

re: How make value of field to be mathematical equation


how would i implement that into my php script?

i really have no idea where to start or what to do.
ronverdonk's Avatar
Moderator
 
Join Date: Jul 2006
Location: The Netherlands
Posts: 4,139
#6: Feb 13 '07

re: How make value of field to be mathematical equation


Starting at the $query, the code would be as follows. Notice

(1) the change in the SELECT statement
(2) that $variable5a holds the calculated field 'total_points'.

[php]$query = mysql_query("SELECT *, (won*3+drawn*1) as total_points FROM `table` ORDER BY total_points DESC, g_difference DESC, g_for DESC LIMIT 0, 30 ");
while ($row = @mysql_fetch_array($query))
{
$variable1=$row["team"];
$variable2=$row["played"];
$variable3=$row["won"];
$variable4=$row["drawn"];
$variable5=$row["lost"];
$variable5a=$row['total_points'];
$variable6=$row["g_for"];
$variable7=$row["g_against"];
$variable8=$row["g_difference"];
$variable9=$row["points"];
//table layout for results

print ("<tr>");
print ("<td width='150'><span class='BodyTable'>$variable1</span></td>");
print ("<td width='50' align='center'><span class='BodyTable'>$variable2</span></td>");
print ("<td width='50' align='center'><span class='BodyTable'>$variable3</span></td>");
print ("<td width='50' align='center'><span class='BodyTable'>$variable4</span></td>");
print ("<td width='50' align='center'><span class='BodyTable'>$variable5</span></td>");
print ("<td width='50' align='center'><span class='BodyTable'>$variable5a</span></td>");
print ("<td width='50' align='center'><span class='BodyTable'>$variable6</span></td>");
print ("<td width='50' align='center'><span class='BodyTable'>$variable7</span></td>");
print ("<td width='75' align='center'><span class='BodyTable'>$variable8</span></td>");
print ("<td width='75' align='center'><span class='BodyTable'>$variable9</span></td>");
print ("</tr>");
}[/php]

Ronald :cool:
Reply