Jump to content

Simple Maths Functions


nishmgopal

Recommended Posts

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

Link to comment
Share on other sites

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...

Link to comment
Share on other sites

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...

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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).

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.