I have three tables
tbl_project
db_projectid db_projectname
1 test
2 xxx
tbl_activities
db_id db_projectid db_category
1 1 Civil Work
2 1 Mechanical
3 1 Electrical
tbl_dailypercentage
db_dpid db_aid db_projectid db_status
1 1 1 red
2 1 1
3 2 1
db_projectid is a primary key in tbl_project
db_id is a primary key and db_projectid is a foreign key in tbl_activities
db_dpid is a primary key and db_aid is a foreign key in tbl_dailypercentage I tried this query
select
activities.db_id,
activities.db_category as cat,
dailypercentage.db_status,
dailypercentage.db_aid,
dailypercentage.db_projectid
from tbl_activities as activities,tbl_dailypercentage as dailypercentage
where
dailypercentage.db_projectid='$projectId'
and
activities.db_id=dailypercentage.db_aid
and
dailypercentage.db_status='red'
But i Have ab error
Undefined variable: cat
while($row=mysqli_fetch_array($statusQuery)){
$status=$row['db_status'];
$cat=$row['cat'];
}
<td> <?php if($cat=="Civil Work" && $status="red"){
echo"<p style='color:$status'>".($sumcivil/$civilCount)."</p>";}
else{echo ($sumcivil/$civilCount);}?>
</td>
I try also many thing the left join and the right join
to have the result i want
The Result i want is
For Project who have an id=1
category status
civil work red
mechanical work
electrical work
For Project who have an id=2
category status
civil work
mechanical work
electrical work
You showed this code:
while($row=mysqli_fetch_array($statusQuery)){
$status=$row['db_status'];
$cat=$row['cat'];
}
If your query returns no rows, $status and $cat will never get defined. I guess this is what went wrong. What happens if you run that query with a MySQL client like phpMyadmin?
Similarly, if your query returns more than one row, $status and $cat will capture the values of only the last row.
It's important to work out what you want to happen with no rows and multiple rows.
Based only on the tables in your question I have put together the following schema (I've not added constraints to the foreign keys as this is just for testing).
create table tbl_project (
`db_projectid` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
`db_projectname` VARCHAR(255) NOT NULL DEFAULT 'Undefined',
PRIMARY KEY (`db_projectid`)
);
create table tbl_activities (
`db_id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
`db_projectid` INT(1) NOT NULL,
`db_category` VARCHAR(255) NOT NULL DEFAULT 'Undefined',
PRIMARY KEY (`db_id`)
);
create table tbl_dailypercentage (
`db_dpid` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
`db_aid` INT(1) NOT NULL,
`db_projectid` INT(1) NOT NULL,
`db_status` VARCHAR(255) NULL,
PRIMARY KEY (`db_dpid`)
);
insert into tbl_project (db_projectname) values ('Test'), ('xxx');
insert into tbl_activities (db_projectid, db_category) values ('1', 'Civil Work'), ('1', 'Mechanical'), ('1', 'Electrical');
insert into tbl_dailypercentage (db_aid, db_projectid, db_status) values
('1', '1', 'red'),
('1', '1', null),
('2', '1', null);
select a.db_category as cat, dp.db_status from tbl_project p
left join tbl_activities a on p.db_projectid = a.db_projectid
left join tbl_dailypercentage dp on dp.db_aid = a.db_id
where p.db_projectid = 1
group by a.db_id;
I've had to add the group by into the final query as you have multiple rows in tbl_dailypercentage with the same db_aid and db_projectid values.
After that, you want to tweak your php code, putting the td stuff inside the while loop:
<?php while($row=mysqli_fetch_array($statusQuery)):?>
$status=$row['db_status'];
$cat=$row['cat'];
<td> <?php if($cat=="Civil Work" && $status="red"){
echo"<p style='color:$status'>".($sumcivil/$civilCount)."</p>";}
else{echo ($sumcivil/$civilCount);}?>
</td>
<?php endwhile; ?>