Jump to content

foreach loop


nishmgopal

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/150005-foreach-loop/
Share on other sites

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;
}

Link to comment
https://forums.phpfreaks.com/topic/150005-foreach-loop/#findComment-787811
Share on other sites

$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;
}

Link to comment
https://forums.phpfreaks.com/topic/150005-foreach-loop/#findComment-787896
Share on other sites

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;

Link to comment
https://forums.phpfreaks.com/topic/150005-foreach-loop/#findComment-787948
Share on other sites

Archived

This topic is now archived and is closed to further replies.

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