如何在php中设置获取数据的值

时间:2017-01-05 06:05:16

标签: php mysql

我在SQL表中有以下数据:

id      code     day     time    year
1     PRC-001    0       t-1     2017
2     PRC-001    1       t-2     2017
3     PRC-002    0       t-3     2017
4     PRC-002    1       t-4     2017
5     PRC-003    0       t-5     2017
6     PRC-003    1       t-6     2017

输出应该是这样的:

  Friday        Saturday         code      
09:30-10:30    03:30-04:30      PRC-001
10:40-11:40    04:45-06:00      PRC-002    
11:50-12:50    06:10-07:10      PRC-003 

如何根据t-1 =>设置值? 09:30-10:30,t-2 => 03:30-04:30,第0天=>星期五,第1天=>星期六。我已经为此编写了一个函数。但无法获得价值。这是我的代码:

function getTime(){
    $time = array(
                    t-1 => "09:30 A.M. - 10:30 A.M.",
                    t-2 => "10:40 A.M. - 11:40 A.M.",
                    t-3 => "11:50 A.M. - 12:50 P.M.",
                    t-4 => "03:30 P.M. - 04:30 P.M.",
                    t-5 => "04:45 P.M. - 06:00 P.M.",
                    t-6 => "06:10 P.M. - 07:10 P.M.",
                );

    return $time;
}
function getDay(){
    $days = array(
                    0 => "Friday",
                    1 => "Saturday",
                    2 => "Sunday",
                    3 => "Tuesday",
                    4 => "Thursday",
                );

    return $days;
 }

$rs = mysql_query("SELECT * FROM routine");

$results = [];

while ($row = mysql_fetch_assoc($rs)) {

$code = $row['code'];

if (!isset($results[$code])) {
    $results[$code] = [
        'day0' => '-',
        'day1' => '-',
    ];
}

$results[$code]['day' . $row['day']] = $row['time'];

}

?>

<table>
<thead>
<tr id="grey">
    <th rowspan="2">Day0</th>
    <th rowspan="2">Day1(s)</th>
    <th rowspan="2">code</th>
</tr>
</thead>

<tbody>
<?php foreach ($results as $code => $result) : ?>
    <!--You shouldn't have multiple elements using the same ids-->
    <tr>
        <td id='clist'><?php echo $result['day0'] ?></td>
        <td id='clist'><?php echo $result['day1'] ?></td>
        <td id='clist'><?php echo $code ?></td>
    </tr>
<?php endforeach ?>
</tbody>

1 个答案:

答案 0 :(得分:0)

我已经解决了这个问题。这是我的代码:

$rs = mysql_query("SELECT * FROM test_work");

    $results = [];
    $days = [];

    while ($row = mysql_fetch_assoc($rs)) {

    $code = $row['code'];

    if (!isset($results[$code])) {
        $results[$code] = [
            'day0' => '-',
            'day1' => '-',
        ];
    }

    $results[$code]['day' . $row['day']] = $row['time'];
    $results[$code]['' . $row['time']] = $row['day'];

}
    $time = array(
                    't-1' => "09:30 A.M. - 10:30 A.M.",
                    't-2' => "10:40 A.M. - 11:40 A.M.",
                    't-3' => "11:50 A.M. - 12:50 P.M.",
                    't-4' => "03:30 P.M. - 04:30 P.M.",
                    't-5' => "04:45 P.M. - 06:00 P.M.",
                    't-6' => "06:10 P.M. - 07:10 P.M.",
                    't-7' => "05:30 P.M. - 06:30 P.M.",
                    't-8' => "06:50 P.M. - 07:50 P.M.",
                );
 ?>


<table>
 <thead>
<tr id="grey">
    <th rowspan="2">Day0</th>
    <th rowspan="2">Day1(s)</th>
    <th rowspan="2">code</th>
</tr>
</thead>

<tbody>
<?php foreach ($results as $code => $result) : ?>

    <!--You shouldn't have multiple elements using the same ids-->
    <tr>
    <?php   $d0 = $result['day0'];
            $d1 = $result['day1'];
    ?>
        <td id='clist'><?php echo $time[$d0];  ?></td>
        <td id='clist'><?php echo $time[$d1]; ?></td>
        <td id='clist'><?php echo $code ?></td>
    </tr>
<?php endforeach ?>
</tbody>
</table>