从数据库中选择下一个事件

时间:2015-09-16 16:13:16

标签: php mysql date

我正在开发一个选择日期列表的功能,并突出显示下一个事件。我设法突出了所有未来的事件,但我想只强调最近的事件。如何才能做到这一点?

<?php
function racesbydate($sql) {
    include 'connect.php';
    $year = $_GET['year'];
    $get = $year.'%';
    $select = $conn->prepare($sql);
    $select->bind_param('s', $get);
    $select->execute();
    $select->store_result();
    if ($select->num_rows > 0) {
        echo "<div class='races' id='$year' style='display:block'>
         <table>\n<th colspan='5'>Cycling Season $year </th>\n
                <tr id='information'>\n
                    <th id='date'>Date</th>\n
                    <th id='race'>Race</th>\n
                    <th id='route'>Route</th>\n
                    <th id='info'>Entry</th>\n
                    <th id='rizultz'>Results</th>";
        $meta = $select->result_metadata();
        while ($field = $meta->fetch_field()) {
            $params[] = &$row[$field->name];            
        }
        call_user_func_array(array($select, 'bind_result'), $params);
        while ($select->fetch()) {
            $today = date("Y-m-d");
            $date = new DateTime($row['date']);
            $formatted_date = $date->format('d/m/Y');
            if ($row['date'] > $today) {  
                if ($row['infoID'] != "0") {
                    echo "<tr class='racedetails' id='nextrace' >\n
                    <td class='dark' id='date'>".$formatted_date."</td>\n
                    <td class='light' id='race'>".$row["title"]."</td>\n
                    <td class='dark' id='route'>".$row["route"]."</td>\n
                    <td class='light' id='info'><a href='".$row["infoID"]."'>Info</a></td>\n
                    <td class='dark' id='rizults'>N/A</td>\n</tr>\n";
                }
            } else {
                if (($row['infoID'] != "0") && ($row['resultID'] !="0" )) {
                    echo "<tr class='racedetails' id='race' >\n
                    <td class='dark' id='date'>".$formatted_date."</td>\n
                    <td class='light' id='race'>".$row["title"]."</td>\n
                    <td class='dark' id='route'>".$row["route"]."</td>\n
                    <td class='light' id='info'><a href='".$row["infoID"]."'>Info</a></td>\n
                    <td class='dark' id='rizults'><a href='".$row["resultID"]."'>Results</a></td>\n</tr>\n";
                }
            }

        }
    }
    echo "</table>\n</div>";
    $select->close();
}

当前结果

<tr class='racedetails' id='nextrace' ><td class='dark' id='date'>06/01/2016</td></tr>
<tr class='racedetails' id='nextrace' ><td class='dark' id='date'>08/02/2016</td></tr>

目标结果

<tr class='racedetails' id='nextrace' ><td class='dark' id='date'>06/01/2016</td></tr>
<tr class='racedetails' id='race' ><td class='dark' id='date'>08/02/2016</td></tr>

1 个答案:

答案 0 :(得分:0)

如果我理解正确,你只需要一个标志来存储下一场比赛是否已被看见的信息:

    :
    call_user_func_array(array($select, 'bind_result'), $params);
    $nextRace = true;
    while ($select->fetch()) {
        $today = date("Y-m-d");
        $date = new DateTime($row['date']);
        $formatted_date = $date->format('d/m/Y');
        if ($row['date'] > $today) {  
            if ($row['infoID'] != "0") {
                $cssId = $nextRace ? 'nextrace' : 'race';
                $nextRace = false;
                echo "<tr class='racedetails' id='".$cssId."' >\n
                <td class='dark' id='date'>".$formatted_date."</td>\n
                <td class='light' id='race'>".$row["title"]."</td>\n
                <td class='dark' id='route'>".$row["route"]."</td>\n
                <td class='light' id='info'><a href='".$row["infoID"]."'>Info</a></td>\n
                <td class='dark' id='rizults'>N/A</td>\n</tr>\n";
            }
        } else {
        :

这假定比赛的顺序按日期递增(您的示例建议)。