I want to create summery report from two tables. one table project_type another ffw
Table ffw :
|-----------------------------------------------------------------------|
| ffw_id    | division_id   | district_id   | project_type_id | name
|-----------------------------------------------------------------------|
| 1         | 30            | 1             |     2           |myAddress
|-----------------------------------------------------------------------|
| 2         | 12            | 2             |     1           | Asdfads |
|-----------------------------------------------------------------------|
| 3         | 30            | 6             |     1           | kkkkk   |
|-----------------------------------------------------------------------|
|  ..       | ..            | ..            |     ..          | .....   |
|-----------------------------------------------------------------------|
Table project_type:
|--------------------------------
| project_type_id | project_type | 
|--------------------------------|
| 1               | food         |
|--------------------------------|
| 2               | work         |
|--------------------------------|
| 3               | visit        |
|--------------------------------|
| ..              | ..           |
|--------------------------------|
My desired result from that two tables after applying division_id condition will be
|-------------------------------------------|
| no        | project_type  | count         | 
|-------------------------------------------|
| 1         | food          | 2             |
|-------------------------------------------|
| 2         | work          | 1             |
|-------------------------------------------|
| 3         | visit         | .             |
|-------------------------------------------|
|  ..       | ..            | ..            |
|-------------------------------------------|
I am trying this code but it displaying duplicate value in while loop
$qry = "
    SELECT * FROM `project_type` 
    LEFT JOIN `ffw` 
        ON project_type.project_type_id = ffw.project_type_id
    WHERE 1
";
if (strlen($_POST["division_id"]) > 0 && $_POST["division_id"] != "0")
{
    $qry .= " AND division_id = '".$_POST["division_id"]."'";
}
$query = mysql_query($qry);
You can use GROUP BY for summarize the result as like that:
SELECT pt.project_type, count(*) as count FROM project_type pt
LEFT JOIN ffw ff ON ff.project_type_id = pt.project_type_id
GROUP BY pt.project_type
This query will return you the summary of your records.
Here is the complete example of your code by using MYSQLi Object Oriented:
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT pt.project_type, count(*) as count FROM project_type pt
LEFT JOIN ffw ff ON ff.project_type_id = pt.project_type_id
WHERE 1 = 1 
";
if(strlen($_POST["division_id"])> 0 && $_POST["division_id"]!="0")
{
    $sql.= " AND division_id = '".$_POST["division_id"]."'";
}
$sql .= "GROUP BY pt.project_type";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
    $i = 1;
    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo $id. " - Name: " . $row["project_type"]. " " . $row["count"]. "<br>";
        $i++;
    }
} 
else 
{
    echo "0 results";
}
$conn->close();
Side Note: I suggest to use mysqli_* OR PDO, because mysql_* is deprecated and its not available in PHP 7
You can use group_by and get count as below :
$qry="SELECT pt.project_type_id as no, pt.project_type, count(*) as count FROM project_type pt LEFT JOIN ffw ON pt.project_type_id=ffw.project_type_id";
if(strlen($_POST["division_id"])>0&&$_POST["division_id"]!="0")
{
     $qry.=" Where division_id='".$_POST["division_id"]."'";
}
$qry.=" GROUP BY pt.project_type";
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With