Jump to content

Using Loop


nishmgopal

Recommended Posts

Hey Guys

 

I am trying to loop through my table so all the records where job_ID=1 is displayed, so far I have the code below and it only returns the first value:

 

$sql2="SELECT * FROM ID_Table WHERE Job_ID IN (SELECT Job_ID FROM Job_ID WHERE Job_ID=$Job_ID1)";
$result3 = mysql_query($sql2)
       or die ("Couldn't execute query.");

while ($row3=mysql_fetch_array($result3)){

  		$Skill_ID=$row3['Skill_ID'];
	$Weight=$row3['Weight'];
	}

 

So basically I want all the skill_id's and the weights associtated with a particular job_id.

 

Can anyone help?

 

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

hi just try this

 

$sql2="SELECT * FROM ID_Table WHERE Job_ID IN (SELECT Job_ID FROM Job_ID WHERE Job_ID=$Job_ID1)";
   $result3 = mysql_query($sql2)
           or die ("Couldn't execute query.");
    
    while ($row3=mysql_fetch_array($result3)){
    
            $Skill_ID[]=$row3['Skill_ID'];
            $Weight[]=$row3['Weight'];
          }

Link to comment
https://forums.phpfreaks.com/topic/149377-using-loop/#findComment-784522
Share on other sites

I tried that code and the return i got was:

 

Array Array

 

Also my tables looks like this:

 

ID_Table:

 

ID Job_ID Skill_ID Weight

1    1        1          4

2    1        2          3

3    1        3          4

4    2        1          5

 

So what this means is that, the job with Job_ID = 1 has 3 skills (1,2,3) with their weights 4, 3 and 4 respectively.

 

So running my sql should return 1,2,3 with 4,3,4 for job id 1.

Link to comment
https://forums.phpfreaks.com/topic/149377-using-loop/#findComment-784526
Share on other sites

$sql2="SELECT * FROM ID_Table WHERE Job_ID IN (SELECT Job_ID FROM Job_ID WHERE Job_ID=$Job_ID1)";
   $result3 = mysql_query($sql2)
           or die ("Couldn't execute query.");
   
    while ($row3=mysql_fetch_array($result3)){
   
            $Skill_ID[]=$row3['Skill_ID'];
            $Weight[]=$row3['Weight'];
          }
echo "<pre>";
print_r($Skill_ID);
print_r($Weight);
echo "</pre>";

echo "The first skill returned is {$Skill_ID[0]}<br />";
echo "The first weight returned is " . $Weight[0] . "<br />";

 

array I would read up on arrays and their usage, as it is imperative to know how to use them for sql etc.

 

Link to comment
https://forums.phpfreaks.com/topic/149377-using-loop/#findComment-784560
Share on other sites

Thank you for that.

 

But how can I use that $Skill_ID[] in my next sql statement.

 

Basically now I am trying to pull out the name of the skill based on the Skill_ID, the code is:

 

$sql3="SELECT * FROM Skill_ID WHERE Skill_ID='$Skill_ID[]";
$result4 = mysql_query($sql3)
           or die ("Couldn't execute query.");
   
    while ($row4=mysql_fetch_array($result4)){

 

but it is not working. 

 

All I want to do is, get all the skill id's that any particular job_id is associated with, and then get all the skill names based on all the skill_id's.  I thought the best way for this was to have multiple sql statemens with stored varibles?

 

Link to comment
https://forums.phpfreaks.com/topic/149377-using-loop/#findComment-784580
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.