0

I'm new to PHP and now I have some problems about the logic to query the data from my database.

First if I have 2 databases which are Application file and Position file.

Application file includes

  1. Application_ID, 2.Applicant_ID, 3.Position_ID

and Position file includes

1.Position_ID, 2.Position_Name, 3.Department_Name

Let me show the unfinished codes...

Link (I copy everything in HTML provided field) -> https://jsfiddle.net/2e50wvzq/

I would like to focus here...

$result4 = mysql_query("SELECT * FROM position"); 
while ($rows4 = mysql_fetch_array($result4, MYSQL_ASSOC)){ 

    $pos_dept = $rows4['Position_Department'];

    $result5 =  mysql_query("SELECT * FROM position WHERE Position_Department = '".$pos_dept."' "); 

    while($rows5 = mysql_fetch_array($result5, MYSQL_ASSOC)){

            $pos_id = $rows5['Position_ID'];

    }

    for($i = 0; $i < $counter; $i++){       
            if($dept_array[$i] == $pos_dept){

            }else{

                        $result6 = mysql_query("SELECT App_Data_ID FROM application_data_file WHERE Position_ID = '".$pos_id."' "); 


                        $pos_app = 0;


                        while ($rows6= mysql_fetch_array($result6, MYSQL_ASSOC)){ 

                            $pos_a = $rows6['App_Data_ID'];

                            $pos_app++;

                        }

                        array_push($dept_array," '".$pos_dept."' ");


                        ?>
                        <TR>

                        <TD> <?php echo $rows4['Position_Department']; ?> </TD>
                        <TD> <?php echo $rows4['Recruitment_Seat'];    ?> </TD>

                        <TD> <?php echo $pos_app; ?>                    </TD>
                        <TD> <?php echo $recruited;   ?>                    </TD>
                        <TD> <?php echo $Vacancy;  ?>                   </TD>



                        </TR>

<?php 
                } 
        } 
}
?>

Assume that the data in databases are state as...

Application File

  1. 00001, 00001, 00001

  2. 00002, 00003, 00001

  3. 00003, 00002, 00002

  4. 00004, 00004, 00001

  5. 00005, 00006, 00002

and Position File

  1. 00001, Programmer, Department of IT

  2. 00002, Accountant, Department of Accounting

As my codes, the result in the table should be as this picture (focus in "Total Applicants" tab in the first table)

-> enter image description here

From my thinking so far, if I use the codes as I posted above the result will turn to be like table 2 (or maybe similar).

How can I provide the result like table 1 result? Please help.

2
  • If it doesn't work or works not as expected, please inform me, otherwise, you should mark as accepted so that in future others would notice. Commented May 29, 2016 at 8:32
  • @EdvinTenovim sorry for late checking, i'm going through your code now Commented May 29, 2016 at 13:42

1 Answer 1

1

Just double check names in my query but code works in overall (because I tested it):

$sql = 'SELECT Department_Name, COUNT(*) AS Total FROM `application_file` LEFT JOIN `position_file` ON (application_file.Position_ID = position_file.Position_ID) GROUP BY Department_Name ORDER BY application_file.Position_ID ';
$result = $db->query($sql);
$total = 0;

if ($result->num_rows > 0)
{
    echo '<table class="table">';
    echo '<tr><td>Department</td><td>Total Recruitments</td><td>Total Applicant</td><td>Making Contract</td><td>Slot left</td></tr>';

    while($row = $result->fetch_assoc())
    {
        echo '<tr><td>'.$row['Department_Name'].'</td><td>x</td><td>'.$row['Total'].'</td><td>x</td><td>x</td></tr>';
        $total += $row['Total'];
    }

    echo '<tr><td>Total</td><td>x</td><td>'.$total.'</td><td>x</td><td>x</td></tr>';
}
else
    echo '0 results';
Sign up to request clarification or add additional context in comments.

4 Comments

as i go through your code, "Total" is not in my database, actually it is something that should be calculated from all rows above. Also, now it tells me that there is fatal error in "$result = $db->query($sql);"
That's true, Total is not one of your columns, but Total is a new column that appears from my query. I also noticed you're using PHP's mysql instead of mysqli, so that's why there is a fatal error. Please check how to configure mysqli as mysql is deprecated.
and what can I replace "Total" in $sql line?
You don't need to replace it, since it's a new column, it doesn't matter what the query is as long as it is doesn't provide any errors. The problem (fatal error) is in PHP mysqli structure (yours is mysql).

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.