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!
$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 ='$_SESSION[Job_Name]'";
$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 ='$_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";
}
foreach ($row['Score'] as $score)($row1['Weight'] as $weight)
{
$diff= ( - $weight);
}
echo $diff;
?>
What I am trying to do is, for each score and weight that is found, I want to do score - weight. I thought using the foreach loop would be the right way of doing it, but Im having some trouble.
$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 ='{$_SESSION[Job_Name]}'";
$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 ='{$_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";
}
foreach ($row['Score'] as $score) {
$weight = $row1['Weight'][$key];
$diff= ($score - $weight);
}
echo $diff;
?>
the problem I am having is that i get an error sayin invalid argument supplied for foreach().....
I'm really confused on what you're trying to do. Are you trying to take ALL the scores and subtract ALL the weight into one result or are you trying to subtract each individual score from their individual weight and output them each?
It just returns one value...and im not even sure where its coming from...im starting to think my code is seriously messed for what im trying to do... il post it here again incase it can shed some light.
all i want to do is calculate the difference between each scores and weights...
I'm not exactly sure what kind of application you're designing but I'm sure the database could be structure a lot better which would allow you to do this with one query and a simple loop. I could be wrong of course. Care to give us a little more detail on what you're doing overall so maybe we can help give you some suggestions?
This is simplified version of what im trying to make...and its about matchin the right people to the right job...imn just having trouble with doing the maths!!
I have attached my table structure..the weight for the job represents the importance of that skill (eg- if C has a weight of 5 then is it necessary, the scale is 1-5). The sscore for the person represents how good they are at the skill, eg a score of 5 means they are excellent, the scale is 1-5)
nishmgopal wrote:im starting to think my code is seriously messed for what im trying to do...
it looks a little bit like it, yes.
why not print all the values instead of saving them in an array.
also, you have one diff var but a whole loop of calculations, so you would end up with one only.
just echo everything you have inside the loop out, and if you want to keep values in an array, then your result ( $diff ) should also be stored as an array.
SELECT
Person_ID.Person_Name, Person_Skill.Skill_name, (Person_Skill.Score-ID_Table.Weight) AS total, Job_ID.Job_Name
FROM
Job_ID
JOIN ID_Table
USING (Job_ID)
JOIN Person_Skill
USING (Skill_Name)
JOIN Person_ID
USING (Person_ID)
WHERE
Person_ID.Person_Name = 'PersonName'
ORDER BY
total
DESC
This query is not optimized, I don't know your indexes plus your database structure could be changed a lot. For instance using Skill_Name as a reference is bad. I can give you advice on how you could change your database structure if you'd like. The above SQL query might work, might not, honestly think of it more like pseudo code.
Edit: Fixed it because of some SQL errors.
Last edited by William on Wed Mar 18, 2009 4:47 pm, edited 2 times in total.