nishmgopal Posted March 16, 2009 Share Posted March 16, 2009 Hi Guy This is my code: $query="SELECT `Skill_Name`, `Weight` FROM ID_Table JOIN Job_ID ON ID_Table.Job_ID = Job_ID.Job_ID WHERE Job_ID.Job_Name ='Manager'"; $result = mysql_query($query) or die ("Couldn't execute query."); while ($row=mysql_fetch_array($result)) { $tblRows .= "<tr>"; $tblRows .= "<td>{$row['Skill_Name']}</td>"; $tblRows .= "<td>{$row['Weight']}</td>"; $tblRows .= "</tr>\n"; } $query="SELECT `Skill_Name`, `Score` FROM Person_Skill JOIN Person_ID ON Person_Skill.Person_ID = Person_ID.Person_ID WHERE Person_ID.Person_Name ='Nish'"; $result = mysql_query($query) or die ("Couldn't execute query."); while ($row=mysql_fetch_array($result)) { $tblRows .= "<tr>"; $tblRows .= "<td>{$row['Skill_Name']}</td>"; $tblRows .= "<td>{$row['Score']}</td>"; $tblRows .= "</tr>\n"; } $diff={$row['Score']} - {$row['Weight']}; echo "$diff"; ?> I want to do some simple maths functions with my score and weight variable, but the above code just returns 0! Do I need a loop or something? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/149687-simple-maths-functions/ Share on other sites More sharing options...
nishmgopal Posted March 16, 2009 Author Share Posted March 16, 2009 I have tried many things and now my code looks like this: $query1="SELECT `Skill_Name`, `Weight` FROM ID_Table JOIN Job_ID ON ID_Table.Job_ID = Job_ID.Job_ID WHERE Job_ID.Job_Name ='Manager'"; $result1 = mysql_query($query1) or die ("Couldn't execute query."); while ($row1=mysql_fetch_array($result1)) { $tblRows1 .= "<tr>"; $tblRows1 .= "<td>{$row1['Skill_Name']}</td>"; $tblRows1 .= "<td>{$row1['Weight']}</td>"; $tblRows1 .= "</tr>\n"; } $query="SELECT `Skill_Name`, `Score` FROM Person_Skill JOIN Person_ID ON Person_Skill.Person_ID = Person_ID.Person_ID WHERE Person_ID.Person_Name ='Nish'"; $result = mysql_query($query) or die ("Couldn't execute query."); while ($row=mysql_fetch_array($result)) { $tblRows .= "<tr>"; $tblRows .= "<td>{$row['Skill_Name']}</td>"; $tblRows .= "<td>{$row['Score']}</td>"; $tblRows .= "</tr>\n"; } $diff=($row['Score'] - $row1['Weight']); echo "$diff"; ?> But I keep getting a return of 0... Quote Link to comment https://forums.phpfreaks.com/topic/149687-simple-maths-functions/#findComment-786146 Share on other sites More sharing options...
jackpf Posted March 16, 2009 Share Posted March 16, 2009 It could be something to do with the fact that you're getting data from the database outside of the while() loop? Try echoing your data out without applying anything to it to see if it actually exists within the variable... Quote Link to comment https://forums.phpfreaks.com/topic/149687-simple-maths-functions/#findComment-786274 Share on other sites More sharing options...
Psycho Posted March 16, 2009 Share Posted March 16, 2009 Yep, byt he time you get to the math operation, you have already run through all of the records from the first query and all the records from the second query. Since you use teh same variable name for the records ($row), the value for $row1['Weight'] from teh first query is wiped out when you get to the calculation. If you were to use different variable names in those while loops you could get the difference between the LAST weight and the LAST score using something like: $diff=($row2['Score'] - $row1['Weight']); But, I'm not sure that is what you are trying to accomplish. Are you trying to get the difference of the total scores and the total weights? That seems doable. but, if you are wanting the difference on a record by record level, I don't know how the records are supposed to match up. If there is some way to correlate the records you could get it all with one query. Quote Link to comment https://forums.phpfreaks.com/topic/149687-simple-maths-functions/#findComment-786293 Share on other sites More sharing options...
nishmgopal Posted March 17, 2009 Author Share Posted March 17, 2009 i actually wanted a record by record difference...is this doable? Quote Link to comment https://forums.phpfreaks.com/topic/149687-simple-maths-functions/#findComment-786536 Share on other sites More sharing options...
Psycho Posted March 17, 2009 Share Posted March 17, 2009 i actually wanted a record by record difference...is this doable? Only if you can determine how the records are related. The current code has two seperate lists of records. How are they related? As I stated, if there is a relation you "should" be able to get the data with a single query (including the difference value). Quote Link to comment https://forums.phpfreaks.com/topic/149687-simple-maths-functions/#findComment-786695 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.