0

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>';
    }
?>     
4

2 回答 2

1

Iznogud,我相信你只需要链接两个表的记录。向表dias添加外键(例如empleados_id ) ,您可以执行以下操作:

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
于 2013-08-15T14:31:32.767 回答
0

乍一看,您的查询中需要 JOIN。我会发表评论,但我距离能够发表评论还有 10 分。

添加 FROM dias JOIN empleados ON 空白 = 空白

这是准系统版本,但如果您知道任何 SQL,您应该能够填写其余部分。

于 2013-08-15T14:16:41.480 回答