Jump to content

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
https://forums.phpfreaks.com/topic/149687-simple-maths-functions/
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...

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.

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

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.