nishmgopal Posted March 18, 2009 Share Posted March 18, 2009 Hi guys, this is my code: $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. Can anyone help? Quote Link to comment https://forums.phpfreaks.com/topic/150005-foreach-loop/ Share on other sites More sharing options...
Mark Baker Posted March 18, 2009 Share Posted March 18, 2009 foreach ($row['Score'] as $key => $score) { $weight = $row1['Weight'][$key]; $diff= += ($score - $weight); } Quote Link to comment https://forums.phpfreaks.com/topic/150005-foreach-loop/#findComment-787779 Share on other sites More sharing options...
nishmgopal Posted March 18, 2009 Author Share Posted March 18, 2009 Hi, I tried that code, and i got: Warning: Invalid argument supplied foreach() in......... Quote Link to comment https://forums.phpfreaks.com/topic/150005-foreach-loop/#findComment-787782 Share on other sites More sharing options...
drisate Posted March 18, 2009 Share Posted March 18, 2009 Does all the rows have a Score? Quote Link to comment https://forums.phpfreaks.com/topic/150005-foreach-loop/#findComment-787785 Share on other sites More sharing options...
nishmgopal Posted March 18, 2009 Author Share Posted March 18, 2009 yep... Quote Link to comment https://forums.phpfreaks.com/topic/150005-foreach-loop/#findComment-787786 Share on other sites More sharing options...
drisate Posted March 18, 2009 Share Posted March 18, 2009 ok do this /*foreach ($row['Score'] as $key => $score) { $weight = $row1['Weight'][$key]; $diff= += ($score - $weight); }*/ echo "<pre>"; print_r($row); echo "</pre>"; And post us the result Quote Link to comment https://forums.phpfreaks.com/topic/150005-foreach-loop/#findComment-787791 Share on other sites More sharing options...
nishmgopal Posted March 18, 2009 Author Share Posted March 18, 2009 hey i tried that and got a blank screen with no errors. Quote Link to comment https://forums.phpfreaks.com/topic/150005-foreach-loop/#findComment-787792 Share on other sites More sharing options...
Mark Baker Posted March 18, 2009 Share Posted March 18, 2009 Backtracking in your code: while ($row=mysql_fetch_array($result)) { $tblRows .= "<tr>"; $tblRows .= "<td>{$row['Skill_Name']}</td>"; $tblRows .= "<td>{$row['Score']}</td>"; $tblRows .= "</tr>\n"; $weight = $row['Weight']; $score = $row['Score']; $diff = $score - $weight; } Quote Link to comment https://forums.phpfreaks.com/topic/150005-foreach-loop/#findComment-787811 Share on other sites More sharing options...
nishmgopal Posted March 18, 2009 Author Share Posted March 18, 2009 when i do this i only get the result for the last row...how do i get result for every row? also is ther any way to the the values of: " $tblRows .= "<td>{$row['Score']}</td>" as an array? Quote Link to comment https://forums.phpfreaks.com/topic/150005-foreach-loop/#findComment-787846 Share on other sites More sharing options...
Mark Baker Posted March 18, 2009 Share Posted March 18, 2009 $diff = 0; $weights = $scores = array(); while ($row=mysql_fetch_array($result)) { $tblRows .= "<tr>"; $tblRows .= "<td>{$row['Skill_Name']}</td>"; $tblRows .= "<td>{$row['Score']}</td>"; $tblRows .= "</tr>\n"; $weight = $row['Weight']; $score = $row['Score']; $diff += $score - $weight; $weights[] = $weight; $scores[] = $score; } Quote Link to comment https://forums.phpfreaks.com/topic/150005-foreach-loop/#findComment-787896 Share on other sites More sharing options...
nishmgopal Posted March 18, 2009 Author Share Posted March 18, 2009 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... $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."); $weights=array() while ($row1=mysql_fetch_array($result1)) { $tblRows1 .= "<tr>"; $tblRows1 .= "<td>{$row1['Skill_Name']}</td>"; $tblRows1 .= "<td>{$row1['Weight']}</td>"; } $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."); $diff = 0; $weights = $scores = array(); while ($row=mysql_fetch_array($result)) { $tblRows .= "<tr>"; $tblRows .= "<td>{$row['Skill_Name']}</td>"; $tblRows .= "<td>{$row['Score']}</td>"; $tblRows .= "</tr>\n"; $weight = $row['Weight']; $score = $row['Score']; $diff += $score - $weight; $weights[] = $weight; $scores[] = $score; } echo $diff; Quote Link to comment https://forums.phpfreaks.com/topic/150005-foreach-loop/#findComment-787948 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.