Simple Maths functions

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
nishmgopal
Forum Contributor
Posts: 101
Joined: Tue Mar 03, 2009 9:38 am

Simple Maths functions

Post by nishmgopal »

Hi Guys,

I have two table, and one of the table there is a field called Score and the other one has a field called Weight.

I want to compare the two and display a name only if score is = or > weight.

my code is below:

Code: Select all

 
$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 ='{$_SESSION[Person_Name]}'";
 
$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";
}
 
$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 ='{$_SESSION[Job_Name]}'";
 
$result = mysql_query($query) or die ("Couldn't execute query.");
 
while ($row=mysql_fetch_array($result))
{
    $tblRows1 .= "<tr>";
    $tblRows1 .= "<td>{$row['Skill_Name']}</td>";
    $tblRows1 .= "<td>{$row['Weight']}</td>";
    $tblRows1 .= "</tr>\n";
}
 
So I want to compare the weight and score and perform some simple maths on it, such as:

score - weight etc

would it be as simple as:

$some_variable=($row['Score'] - $row['Weight']) ??

Thanks
prgmctan
Forum Newbie
Posts: 19
Joined: Mon Feb 23, 2009 11:29 am

Re: Simple Maths functions

Post by prgmctan »

nishmgopal wrote:Hi Guys,

I have two table, and one of the table there is a field called Score and the other one has a field called Weight.

I want to compare the two and display a name only if score is = or > weight.

my code is below:

Code: Select all

 
$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 ='{$_SESSION[Person_Name]}'";
 
$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";
}
 
$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 ='{$_SESSION[Job_Name]}'";
 
$result = mysql_query($query) or die ("Couldn't execute query.");
 
while ($row=mysql_fetch_array($result))
{
    $tblRows1 .= "<tr>";
    $tblRows1 .= "<td>{$row['Skill_Name']}</td>";
    $tblRows1 .= "<td>{$row['Weight']}</td>";
    $tblRows1 .= "</tr>\n";
}
 
So I want to compare the weight and score and perform some simple maths on it, such as:

score - weight etc

would it be as simple as:

$some_variable=($row['Score'] - $row['Weight']) ??

Thanks
I think you're going to need to create new variables for the second query or it will become overwritten, but I could be wrong.
nishmgopal
Forum Contributor
Posts: 101
Joined: Tue Mar 03, 2009 9:38 am

Re: Simple Maths functions

Post by nishmgopal »

I tried that, and this is my code:

Code: Select all

 
$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";
 
?>
 
And the return I get is 0.

Do I need to loop? and if so how? Thanks
nishmgopal
Forum Contributor
Posts: 101
Joined: Tue Mar 03, 2009 9:38 am

Re: Simple Maths functions

Post by nishmgopal »

can some please help...I have tried numerous things wth no luck...
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Simple Maths functions

Post by John Cartwright »

Please do not bump your post within 24 hours of the last.
prgmctan
Forum Newbie
Posts: 19
Joined: Mon Feb 23, 2009 11:29 am

Re: Simple Maths functions

Post by prgmctan »

nishmgopal wrote:I tried that, and this is my code:

Code: Select all

 
$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";
 
?>
 
And the return I get is 0.

Do I need to loop? and if so how? Thanks
Are the scores and weights related to the people? If so, you should combine your tables and make your life a lot easier.
User avatar
php_east
Forum Contributor
Posts: 453
Joined: Sun Feb 22, 2009 1:31 pm
Location: Far Far East.

Re: Simple Maths functions

Post by php_east »

i don't understand this. why are you posting the same problem again under a different name ?
you said in the previous thread that you will investigate or try and report back your findings, and so we left it at that, viewtopic.php?f=1&t=97198
now you appear here under a different title with the exact same problem.

there is a difference between asking for help and leading us to work for you. i felt i wasted my time trying to help you previously. at least if you wanted someone else to come in and help, you could have done so in the already existing thread. or is it that the ideas/solutions given were not at all useable ? and the time spent studying your problem entirely wasted ?
nishmgopal
Forum Contributor
Posts: 101
Joined: Tue Mar 03, 2009 9:38 am

Re: Simple Maths functions

Post by nishmgopal »

I havent re-posted this...i posted the loop question before this, and I havent had the chance to try the suggestion you and other made, but will be doing it today, and will be reporting back.

Not asking anyone to leade my work, but I am asking for advice...isnt that what this forum is for?

I appreciate the help given, and will be seeing if I can use the advise and will report back later today.

Thank you
Post Reply