foreach loop

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

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

foreach loop

Post by nishmgopal »

Hi guys, 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 ='$_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?
atonalpanic
Forum Commoner
Posts: 29
Joined: Mon Mar 02, 2009 10:20 pm

Re: foreach loop

Post by atonalpanic »

You probably want to take $_SESSION[Job_Name] out of the string and concatenate it or use curly brackets like such...

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 ='{$_SESSION[Job_Name]}'";
 
Also, that might have not been your error. What problem are you having with the code?
nishmgopal
Forum Contributor
Posts: 101
Joined: Tue Mar 03, 2009 9:38 am

Re: foreach loop

Post by nishmgopal »

hi, I have changed my code and now it looks like:

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 ='{$_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().....
User avatar
William
Forum Contributor
Posts: 332
Joined: Sat Oct 25, 2003 4:03 am
Location: New York City

Re: foreach loop

Post by William »

nishmgopal wrote:hi, I have changed my code and now it looks like:

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 ='{$_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?
nishmgopal
Forum Contributor
Posts: 101
Joined: Tue Mar 03, 2009 9:38 am

Re: foreach loop

Post by nishmgopal »

yes i am trying to subtract each individual score from their individual weight and output them each....
atonalpanic
Forum Commoner
Posts: 29
Joined: Mon Mar 02, 2009 10:20 pm

Re: foreach loop

Post by atonalpanic »

Opps. I didn't read the rest of your code.

Code: Select all

 
 foreach ($row['Score'] as  $score) {
    $weight = $row1['Weight'][$key];
    $diff= ($score - $weight);
 }
 
Your trying to access $row in the wrong scope.
nishmgopal
Forum Contributor
Posts: 101
Joined: Tue Mar 03, 2009 9:38 am

Re: foreach loop

Post by nishmgopal »

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

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 ='{$_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;
User avatar
William
Forum Contributor
Posts: 332
Joined: Sat Oct 25, 2003 4:03 am
Location: New York City

Re: foreach loop

Post by William »

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?
nishmgopal
Forum Contributor
Posts: 101
Joined: Tue Mar 03, 2009 9:38 am

Re: foreach loop

Post by nishmgopal »

basically I have a table with job name and skill name and a weight. I have another table with person name, skill and a score.

so for example:

Job table:

Name Skill Weight
Developer C 5
Developer Java 3

Person Table

Name Skill Score
Bob C 4
Tom Java 4

So i want to compare the two table, and do:

Bob(C) - Developer (C)= -1
Tom(Java) - Developer(Java)=1

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!!
User avatar
William
Forum Contributor
Posts: 332
Joined: Sat Oct 25, 2003 4:03 am
Location: New York City

Re: foreach loop

Post by William »

What exactly is weight and score for?

Edit: I see 4 tables, you listed two. Anyway I can see the structure of all 4 tables?
nishmgopal
Forum Contributor
Posts: 101
Joined: Tue Mar 03, 2009 9:38 am

Re: foreach loop

Post by nishmgopal »

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)
Attachments
tbl_structure.png
tbl_structure.png (119.52 KiB) Viewed 543 times
User avatar
php_east
Forum Contributor
Posts: 453
Joined: Sun Feb 22, 2009 1:31 pm
Location: Far Far East.

Re: foreach loop

Post by php_east »

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.
nishmgopal
Forum Contributor
Posts: 101
Joined: Tue Mar 03, 2009 9:38 am

Re: foreach loop

Post by nishmgopal »

right...back to the drawing board...il report back. thanks for you help
User avatar
William
Forum Contributor
Posts: 332
Joined: Sat Oct 25, 2003 4:03 am
Location: New York City

Re: foreach loop

Post by William »

I just wrote this off the top of my head, I don't have time to really check but to give you an "idea" of what you can do...

Code: Select all

 
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.
nishmgopal
Forum Contributor
Posts: 101
Joined: Tue Mar 03, 2009 9:38 am

Re: foreach loop

Post by nishmgopal »

I would appreciate any help you can give me...anything that can help, thank you
Post Reply