是否有更适合从SQL过滤的方法?

时间:2015-02-23 16:02:11

标签: javascript php html mysql

我有一个下拉数月的代码,当选择时,该特定月份的数据会显示。例如:

<div style="width:700px;margin-left:auto;margin-right:auto;text-align:center;">
<h2>List of sessions</h2>
    <select name = "Select">
        <option value="jan" onclick="showDiv('j')">January</option>
        <option value="feb" onclick="showDiv('f')">February</option>
        <option value="mar" onclick="showDiv('m')">March</option>
        <option value="apr" onclick="showDiv('a')">April</option>
        <option value="may" onclick="showDiv('m')">May</option>
    </select>  &nbsp;



   </div>
<div class="content" id="f"> 

        <?php $sql = DB::getInstance()->get('sessions', array('MONTH(date)','=' ,'2'));
 //mysql_db_query('nio', "SELECT DATE");
        echo "<table class='table table-hover'style='color: orange' > "
                            . "<thead>
      <tr>
        <th>Title</th>
        <th>Date</th>
        <th>Start Time</th>
        <th>End Time</th>
      </tr> 
      </thead>
    <tbody>";
                if(!$sql->count()){
                    echo 'No data';
                }else {
                    foreach ($sql->results() as $sql){ 
                        echo "<tr style='background-color: black'>";
                        echo "<td>" .  $sql->title . "</td>";
                        echo "<td>" . $sql->date . "</td>";
                        echo "<td>" . $sql->start_time . "</td>";
                        echo "<td>" . $sql->end_time . "</td>";
                        echo "</tr>";
                        ?>
                <?php }}echo"   </tbody>";
 echo" </table>";?>
 </div>

上面的div类仅用于显示二月份的数据。我必须每个月都有,这意味着如果我按照这种方式,我将重复上述div约11次。 有没有更好的方法来做到这一点?

1 个答案:

答案 0 :(得分:0)

看看它是否对您有所帮助:

<div style="width:700px;margin-left:auto;margin-right:auto;text-align:center;">
    <h2>List of sessions</h2>

    <form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" method="post">
        <select name="Select" onchange="this.form.submit()">
            <option value="">-- SELECT --</option>
            <option value="1">January</option>
            <option value="2">February</option>
            <option value="3">March</option>
            <option value="4">April</option>
            <option value="5">May</option>
        </select>
    </form>
</div>

<?php

// Here you get user selection
// Eg.
if(isset($_POST['Select']) && !empty($_POST['Select'])){

    $userSelection = (int) $_POST['Select'];

    // Imagine user selection is 1
    // Make query and cosntruct table here 
}     

?>