0

I have a while loop that is supposed to first get the individual_id from a table called submittedresume using the job_id that it gets from another function. It would then use that id in another while loop to get the first_name and last_name from the individual table. It would then use another while loop to get the submitted_id from the submitted resume table.

I split the first and last while loop to get distinct values from the output.

My first while loop. It first gets the individual_id from a table called submittedresume using the job_id that it gets from another function. It gives me the correct output of two user ids: 9 and 4.

 global $database;
    $query = "SELECT DISTINCT individual_id FROM submittedresume WHERE job_post_id='$id'";
    $output = $database->query($query);
    while ($row = mysqli_fetch_array($output)) {
        $indvId = $row[0];
    }

This is the second inner while loop. It gives me an output of Mary Jane (No repeat) and Tom Sawyer.

 $indvId = $row[0];
        $sql = "SELECT * FROM individual WHERE individual_id='$indvId'";
        $result = $database->query($sql);                
        while ($details = mysqli_fetch_array($result)) {  
            $first = $details['first_name'];
            $last = $details['last_name'];
            echo $first;
            echo $last;
        }

This is my whole function:

public function displayApplications($id){
    global $database;
    $query = "SELECT DISTINCT individual_id FROM submittedresume WHERE job_post_id='$id'";
    $output = $database->query($query);
    while ($row = mysqli_fetch_array($output)) {
        $indvId = $row[0];
        $sql = "SELECT * FROM individual WHERE individual_id='$indvId'";
        $result = $database->query($sql);                
        while ($details = mysqli_fetch_array($result)) {  
            $first = $details['first_name'];
            $last = $details['last_name'];
            $sqlquery = "SELECT DISTINCT resume_title FROM submittedresume WHERE individual_id='$indvId' order by submitted_id";
            $data = $database->query($sqlquery); 
            if (mysqli_num_rows($data) == 0) {
                    echo "Database is empty <br>";
                } else {
                     while (($name = mysqli_fetch_array($data))) {
                           echo $first . " " . $last . " "."<a href=handleDownload.php?id=$id>$name[0]</a><br>";
                       }
                    }
        }
    }
}

This is what I'm getting right now:

first_name | last_name | resume_name
Mary       | Jane      | resume_1
Mary       | Jane      | resume_2

This is what I'm looking for:

first_name | last_name | resume_name
Mary       | Jane      | resume_2
Tom        | Sawyer    | resume_1
1
  • you need to rewrite your queries and use JOIN instead Commented Aug 16, 2015 at 3:23

2 Answers 2

2

I think after while use foreach loop:

For example:

 $query = "SELECT DISTINCT individual_id FROM submittedresume WHERE job_post_id='$id'";
$output = $database->query($query);
$details = mysqli_fetch_array($result);

 foreach($details as $key => $value){
       echo 'Key: '. $key . ' '. 'Value: '. $value;
 }
Sign up to request clarification or add additional context in comments.

2 Comments

I'm getting double output when using foreach. Is there a way for me to remove the doubles? Key: 0 Value: 4Key: individual_id Value: 4Key: 0 Value: 9Key: individual_id Value: 9
I've updated my answer. I guess it's my wrong to use while inside a foreach loop because while loops every data that queries
0

$indvId = $row[0]; is returning the 1st item in the result array

I personally would use a foreach loop but you could do it the way you have it by adding a counter ie: $count = 0; before the loop and $count++; inside the loop and $indvId = $row[$i];

2 Comments

The foreach method sounds interesting. If I were to use foreach, how would I go about it?
can you post print_r($output) so I can see what the array looks like?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.