I've been struggling for some time with this one. I have a following code (its bit messy mind you) but I always get the duplicated data for the each name, instead of data corresponding to each name.
Instead of:
nombre | date | start | end | posicion
---------------------------------------
Peter |23/4/13| 12:00| 13:00 | kitchen
Ann
Marko
I get:
nombre | date | start | end | posicion
---------------------------------------
Peter |23/4/13| 12:00| 13:00 | kitchen
Ann |23/4/13| 12:00| 13:00 | kitchen
Marko |23/4/13| 12:00| 13:00 | kitchen
Anyway there is the code:
$db = new PDO('mysql:host=localhost;...');
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$sql = "SELECT
date, GROUP_CONCAT(CONCAT_WS('|', nombre, start, end, totalHx)) schedule
FROM dias, empleados
GROUP BY date
ORDER BY date ASC
LIMIT $startrow, 7 ";
$query = $db->prepare($sql);
$query->execute();
$rows = $query->fetchall(PDO::FETCH_ASSOC);
$query = null;
$db = null;
?>
<h2>CALENDARIO</h2>
<table width=60%' border='1' cborder='1' cellpadding='2' cellspacing='0' bordercolor='#666666' class='boldtable' bgcolor="#F2F2F2" >
<thead>
<tr style >
<th></th>
<?php
$schedules = array();
$day = 1;
foreach($rows as $row)
{
$schedule = explode(',', $row['schedule']);
foreach($schedule as $details) {
list($nameX, $start_hourX, $end_hourX, $posicion) = explode('|', $details);
if (!isset($schedules[$nameX]))
{
$employee = array();
$sacarIDa =mysql_query( "SELECT `id` FROM empleados WHERE `nombre`= '$nameX';");
$sacarIDR = mysql_fetch_array ($sacarIDa);
$restofLine = $sacarIDR[0];
$nameLink = 'update_empleado.php' . '?id=' . $restofLine;
$name = "<a href='$nameLink' style=' color: #000; background: #ffc; font-family: Verdana,; font-size: 13px; font-weight: bold;'>" . $nameX . "</a>";
$employee[0] = $name;
$schedules[$nameX] = $employee;
}
$start_hour = $start_hourX;
$end_hour = $end_hourX;
$schedules[$nameX][$day] .= '<span style="color:#000000">' . "<p><b> $start_hour-$end_hour</b> | $posicion </p> </span>";
}
$day++;
}
?>
</tr>
</thead>
<tbody>
<?php
foreach($schedules as $schedule)
{
echo "<tr><td>".$schedule[0]."</td>";
for ($i = 1; $i < $day; $i++)
{
echo "<td>".$schedule[$i]."</td>";
}
echo '</tr>';
}
?>