I have a couple of tables...one contains sites and another contains some users. There is a link binding specific users to specific sites. When I run my query:
select sites.site, users.fullname
from sites
inner join users
on sites.nameid = users.id;
I get the following output:
SITE | FULLNAME
site A | John Doe
site B | John Doe
site C | John Doe
site D | Roger Rabbit
site E | Roger Rabbit
site F | Batman
What I'm looking for is one FULLNAME with the list of sites below:
SITE | FULLNAME
site A | John Doe
site B
site C
site D | Roger Rabbit
site E
site F | Batman
I don't know if this is even possible or if I have to use something else to get the format I'm looking for.
I'm going to add some more details here that I should have included....first off...my query has changed to this:
SELECT completed.date, users.username, iata.code, tasks.task
FROM completed
JOIN users
ON users.id = completed.name
JOIN iata
ON iata.id = completed.code
JOIN tasks
ON tasks.id = completed.task
WHERE MONTH(completed.date) = MONTH(NOW()) AND YEAR(completed.date) = YEAR(NOW());
That gives me everything I want...my php /html that I'm using to display looks like this:
<!DOCTYPE html>
<html>
<head>
<title>SPOC Report</title>
<meta charset=utf-8>
</head>
<body>
<?php
require 'db/spoc_config.php';
?>
<?php
$conn = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql_count = "
SELECT completed.date, users.username, iata.code, tasks.task
FROM completed
JOIN users
ON users.id = completed.name
JOIN iata
ON iata.id = completed.code
JOIN tasks
ON tasks.id = completed.task
WHERE MONTH(completed.date) = MONTH(NOW()) AND YEAR(completed.date) = YEAR(NOW());
";
$result_count = $conn->query($sql_count);
$count = mysqli_fetch_row($result_count);
$str_count = implode($count);
date_default_timezone_set('UTC');
$today = date('l, jS F Y');
$week = date('W');
$to = "[email protected]";
$subject = "SPOC: Monthly Report " . $today;
$heading = "<b>SPOC Report: </b>";
$message = "<h2>" . $heading . " " . $today . "</h2>";
$message .= '<table style="font-family: verdana,arial,sans-serif;font-size:11px;color:#333333;border-width: 1px;border-color: #666666;border-collapse: collapse;border-spacing: 2px;">';
$message .= '
<th style="border-width: 1px;padding: 8px;border-style: solid;border-color: #666666;background-color: #dedede;">Spoc</th>
<th style="border-width: 1px;padding: 8px;border-style: solid;border-color: #666666;background-color: #dedede;">Site</th>
<th style="border-width: 1px;padding: 8px;border-style: solid;border-color: #666666;background-color: #dedede;">Task</th>';
if ($result_count->num_rows > 0) {
while($row = $result_count->fetch_assoc()) {
$message .= '<tr style="white-space: normal;line-height: normal;font-weight: normal;font-variant: normal;font-style: normal;text-align: start;">
<td style="border-width: 1px;padding: 8px;border-style: solid;border-color: #666666;background-color: #ffffff;display: table-cell;vertical-align: inherit;">'.$row["username"].'</td>
<td style="border-width: 1px;padding: 8px;border-style: solid;border-color: #666666;background-color: #ffffff;display: table-cell;vertical-align: inherit;">'.$row["code"].'</br></td>
<td style="border-width: 1px;padding: 8px;border-style: solid;border-color: #666666;background-color: #ffffff;display: table-cell;vertical-align: inherit;">'.$row["task"].'</br></td>
</tr>';
}
} else {
echo "0 results";
}
$message .= '</table>';
$header = "From:[email protected] \r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$retval = mail ($to,$subject,$message,$header,'[email protected]');
$conn->close();
?>
</body>
</html>
The output however looks like this:
SPOC | SITE | TASK
JohnDoe | SITE A | TASK1
JohnDoe | SITE A | TASK2
JohnDoe | SITE B | TASK1
JohnDoe | SITE C | TASK1
ROGERRABBIT | SITE A | TASK1
ROGERRABBIT | SITE A | TASK2
What I'm looking for is:
JohnDoe | Site A | TASK1, TASK2, TASK3
JohnDoe | Site B | Task1, Task2
RogerRabbit | Site A | tASk1, Task2
Hope this clear it up a bit. Sorry folks for the confusion.
FULLNAME, i.e.$currentFULLNAME. If the value row matches the$currentFULLNAMEvalue you don't echo out the rowFULLNAME. If the row value doesn't match, you set$currentFULLNAMEto the new name and echo out the row value