I have the two tables
Create TABLE Comment(
CommentText TEXT,
CommentDate DATE NOT NULL,
Time time,
PostedBy VARCHAR(30),
FOREIGN KEY (PostedBy) REFERENCES Employee(Name)
);
Create TABLE Employee(
Name VARCHAR(30),
Title VARCHAR(30),
Onshift BOOLEAN,
PRIMARY KEY(Name)
);
Now how can I do the following
$Comments = mysql_query("SELECT * FROM Comment");
$all_comments = array();
while($row = mysql_fetch_array($Comments)) {
$all_comments[] = $row;
}
foreach($all_comments as $commentrow){
if($commentrow['PostedBy']){ //Check if The Employee is Onshift
Echo("The employee was on shift");
}
else{
Echo("The employee was on shift");
}
}
I.E given the foreign key (postedby) lookup another value in that foregin keys table (in this case if the boolean if the staff is on shift?)
Your select should be something like
SELECT c.*, e.Name, e.Onshift FROM Comment as c join Employee as e on c.PostedBy = e.Name
And then you can just use
$commentrow['Onshift']
You can use this comment anyone of sql and test your code
//Priority on comment table
$sql = "SELECT cmd.*, emp.* FROM Comment AS cmd
LEFT JOIN employee AS emp ON cmd.name = emp.name";
//use anyone of thsi based on ur need
$sql = "SELECT cmd.*, emp.* FROM Comment AS cmd
LEFT JOIN employee AS emp ON emp.name = cmd.name";
$Comments = mysql_query($sql);
$all_comments = array();
while($row = mysql_fetch_array($Comments)) {
if($row['PostedBy']){ //Check if The Employee is Onshift
echo("The employee was on shift");
}
else{
echo("The employee was not in shift");
}
}