我一直在努力争取这个。我有一个下面的代码(它让你有点麻烦)但我总是得到每个名字的重复数据,而不是每个名字对应的数据。
而不是:
nombre | date | start | end | posicion
---------------------------------------
Peter |23/4/13| 12:00| 13:00 | kitchen
Ann
Marko
我明白了:
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
无论如何都有代码:
$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>';
}
?>
答案 0 :(得分:1)
Iznogud,我相信你只需要链接两个表的记录。将外键(例如 empleados_id )添加到表 dias ,您可以执行以下操作:
SELECT `date`, GROUP_CONCAT( CONCAT('|', nombre, `start`, `end`)) schedule
FROM dias INNER JOIN empleados ON dias.`empleados_id` = `empleados`.`id`
GROUP BY empleados.id
ORDER BY date ASC
答案 1 :(得分:0)
乍看之下,您需要在查询中加入JOIN。我会评论,但我距离能够10分。
添加FROM dias JOIN empleados ON blank = blank
这是准系统版本,但如果您知道任何SQL,您应该能够填写其余的。