php - 在 Drupal 中使用 MYSQL 查询可排序 HTML 表

标签 php jquery mysql drupal

好的,我会尽力把这个问题说清楚。我们最近刚刚开始使用 Drupal,第三方为我们构建了我们网站的一部分。我们有一个 View 文件,它查询 Mysql 数据库并从表中提取与特定搜索条件匹配的类(class)。例如 2014 年秋季的所有类(class)。他们通过 CID 或类(class) ID 订购。 View 页面包括时间表结果 PHP 页面,该页面随后将信息呈现到表格中。我们希望能够在表中具有可排序的列。我尝试添加一个 jscript 文件,该文件将在不重新加载表信息的情况下执行此操作,但它没有工作,我不知道为什么。我尝试过使用 Drupal_add_js 以及仅插入 JS 内联。我也尝试过执行 href='/shedules?orderby=cname"但这似乎并没有保留 View 页面查询中的数据。我对此感到非常沮丧,我不知道还能去哪里看看。如果您能给我任何指导,我将不胜感激!

这里是 View 页面

 <!--
    <pre>
        <? print_r($_POST); ?>
    </pre>
 -->

    <?php
        $query = " SELECT * FROM ";

        $semester = $_POST['semester'];

        if(empty($_POST['text'])){
            if($_POST['currentcourse'] == "1"){
                $query .= " $semester WHERE (sdate <= NOW() OR sdate is NULL)";
            } else {
                $query .= " $semester WHERE (1=1)";
            }
        } else {
            //$query .= " $semester WHERE (1=1)";
            $query .= " $semester WHERE (sdate >= NOW() OR sdate is NULL)";
        }

        if( isset( $_POST['online'] ) && 'ON' == $_POST['online'] )
            $query .=  " AND ( `method`='WEB' OR `method`='Online' ) ";


        if( isset( $_POST['text'] ) && !empty( $_POST['text'] ) ){
            $words = explode(' ', urldecode( $_POST['text'] ) );
            $fields = array( 'division', 'cid', 'cname', 'instructor' );
            if( count($words) > 1){
                // multi word search
                $q = array();
                foreach( $words as $w ){
                    $part = array();
                    foreach( $fields as $f ){
                        $part[] = " $f LIKE '%$w%' ";
                    }
                    $q[] = $part;
                }
                $final_parts = array();
                foreach( $q as $query_part )
                    $final_parts[] = implode( ' OR ', $query_part);

                $final_query = ' AND (' . implode(') AND (', $final_parts) . ')';

            }else{
                // single word search
                $part = array();
                foreach( $fields as $f ){
                    $part[] = " $f LIKE '%$_POST[text]%' ";
                }
                $final_query = ' AND ('.implode( ' OR ', $part).')';
            }

            $query .= " " . $final_query . " ";
        }

        if( isset( $_POST['area_id'] ) && '-1' != $_POST['area_id'] )
            $query .= " AND cid LIKE '" . $_POST['area_id'] . "%'";

        if( isset( $_POST['division_id'] ) && '-1' != $_POST['division_id'] )
            $query .= " AND division = '" . $_POST['division_id'] . "'";

        if( isset( $_POST['duration'] ) && '-1' != $_POST['duration'] )
            $query .= " AND duration_min " . ( ( $_POST['durcomp'] == 'more' ) ? ' >= ' : ' <= ' )  .     $_POST['duration'] . " ";


        $query .= " AND ( 1=1 ";

        if( isset($_POST['time']) && isset($_POST['time'][0]) && !empty($_POST['time'][0]) ){

            $start_parts = explode(' ', $_POST['time'][0]);
            $start_time = ((int)$start_parts[0] * 60) + (int)$start_parts[1];

            $query .= " AND stime_min >= $start_time ";

        }

        if( isset($_POST['time']) && isset($_POST['time'][1]) && !empty($_POST['time'][1]) ){

            $end_parts = explode(' ', $_POST['time'][1]);
            $end_time = ((int)$end_parts[0] * 60) + (int)$end_parts[1];

            $query .= "AND etime_min <= $end_time ";
        }

        $query .= " ) ";


        $query .= " AND ( 1=1 ";


                if( isset( $_POST['T'] ) && 'ON' == $_POST['T'] )
                    $query .=  ( $_POST['dayop'] == 'AND' ) ? ' AND ' : ' OR ' . "  day_T = 1 ";

                if( isset( $_POST['W'] ) && 'ON' == $_POST['W'] )
                    $query .=  ( $_POST['dayop'] == 'AND' ) ? ' AND ' : ' OR ' . "  day_W = 1 ";

                if( isset( $_POST['R'] ) && 'ON' == $_POST['R'] )
                    $query .=  ( $_POST['dayop'] == 'AND' ) ? ' AND ' : ' OR ' . "  day_R = 1 ";

                if( isset( $_POST['F'] ) && 'ON' == $_POST['F'] )
                    $query .=  ( $_POST['dayop'] == 'AND' ) ? ' AND ' : ' OR ' . "  day_F = 1 ";

                if( isset( $_POST['S'] ) && 'ON' == $_POST['S'] )
                    $query .=  ( $_POST['dayop'] == 'AND' ) ? ' AND ' : ' OR ' . "  day_S = 1 ";


        $query .= " ) ORDER BY cid";

 /*             echo $query; */



        $string_query = $query;


        $query = db_query($query);
        $result = $query->fetchAll();

        $in_progress = 0;
        foreach ($result as $r) {
            if (!empty($r->sdate) && strtotime($r->sdate) < time()) {
                $in_progress++;
            }
        }



   endif; ?>


<?php
require( __dir__ . '/course-schedule-headings-table.php');
?>

<?php
require( __dir__ . '/course-schedule-results.php');
?>

这是包含的结果页面

<?php $now = time();?>

<table id="scheduletable" style="width:450px; font-size :10px;">


<thead>
    <tr valign="top">
        <th scope="col" bgcolor="#FFFFCC"><a href="/schedules?sort=cid">Course ID</a><br></th>
        <th scope="col" bgcolor="#E0E0E0" style="width: 12px"><a href="/schedules?sort=cname">Course name</a>    </th>
        <th scope="col" bgcolor="#E0E0E0"><a href="/schedules?sort=credits">Credits</a></th>
        <th scope="col" bgcolor="#E0E0E0"><a href="/schedules?sort=method">Type</a></th>
        <th scope="col" bgcolor="#E0E0E0"><a href="/schedules?sort=sdate">Start date</a></th>
        <th scope="col" bgcolor="#E0E0E0"><a href="/schedules?sort=edate">End date</a></th>
        <th scope="col" bgcolor="#E0E0E0"><a href="/schedules?sort=days">Days</a></th>
        <th scope="col" bgcolor="#E0E0E0"><a href="/schedules?sort=stime">Start time</a></th>
        <th scope="col" bgcolor="#E0E0E0"><a href="/schedules?sort=etime">End time</a></th>
        <th scope="col" bgcolor="#E0E0E0"><a href="/schedules?sort=instructor">Instructor</a></th>
    </tr>
</thead>


<?php foreach($result as $course): ?>


    <?php
        $color="#FFFFFF";

        if( !empty($course->sdate) && strtotime($course->sdate) < $now )
            $color = $color_in_progress;

        if( "Online" == trim( $course->method ) )
            $color = $color_online;

        if( "Hybrid" == trim( $course->method ) )
            $color = $color_hybrid;

        if( !empty($course->stime_min) && $course->stime_min >= 960 )
            $color = $color_evening;
    ?>

    <tbody>
        <tr>
        <tr bgcolor="<?=$color?>">
            <td align="center">
                <a href="/courses/?id=<?= $course->cid ?>">
                    <?= $course->cid ?>
                </a>
            </td>
            <td class="name" style="width: 1.5">
                <?= $course->cname ?>
            </td>
            <td class="small" align="center">
                <?= $course->credits ?>
                <?= ("COMP100 CE0" == $course->cid) ? '.50' : null?>
            </td>
            <td align="center">
                <?= $course->type?>
            </td>
            <td align="center">
                <?= date( "m/d", strtotime( $course->sdate ) )?>
            </td>
            <td align="center">
                <?= date( "m/d", strtotime( $course->edate ) )?>
            </td>
            <td align="center">
                <?= $course->days ?>
            </td>
            <td align="center">
                <?= date( "h:i", strtotime( $course->stime ) )?>
            </td>
            <td align="center">
                <?= date( "h:i", strtotime( $course->etime ) )?>
            </td>
            <td align="center">
                <?= $course->instructor ?>
            </td>
        </tr>
    </tbody>
<? endforeach; ?>
 </table>

以下是表输出示例:样式是根据查询结果动态完成的,该结果来自执行搜索后的 View 源。

<table id="scheduletable" style="width:450px; font-size :10px;">


<thead>
    <tr valign="top">
        <th scope="col" bgcolor="#FFFFCC"><a href="/schedules?sort=cid">Course ID</a><br></th>
        <th scope="col" bgcolor="#E0E0E0" style="width: 12px"><a href="/schedules?sort=cname">Course name</a>    </th>
        <th scope="col" bgcolor="#E0E0E0"><a href="/schedules?sort=credits">Credits</a></th>
        <th scope="col" bgcolor="#E0E0E0"><a href="/schedules?sort=method">Type</a></th>
        <th scope="col" bgcolor="#E0E0E0"><a href="/schedules?sort=sdate">Start date</a></th>
        <th scope="col" bgcolor="#E0E0E0"><a href="/schedules?sort=edate">End date</a></th>
        <th scope="col" bgcolor="#E0E0E0"><a href="/schedules?sort=days">Days</a></th>
        <th scope="col" bgcolor="#E0E0E0"><a href="/schedules?sort=stime">Start time</a></th>
        <th scope="col" bgcolor="#E0E0E0"><a href="/schedules?sort=etime">End time</a></th>
        <th scope="col" bgcolor="#E0E0E0"><a href="/schedules?sort=instructor">Instructor</a></th>
    </tr>
</thead>





    <tbody>
        <tr>
        <tr bgcolor="#FFCCCC">
            <td align="center">
                <a href="/courses/?id=ABED003 11">
                    ABED003 11                  </a>
            </td>
            <td class="name" style="width: 1.5">
                Basic Education             </td>
            <td class="small" align="center">
                6                                   </td>
            <td align="center">
                                </td>
            <td align="center">
                08/18               </td>
            <td align="center">
                10/10               </td>
            <td align="center">
                MTWTHF              </td>
            <td align="center">
                08:30               </td>
            <td align="center">
                10:50               </td>
            <td align="center">
                Melissa Escamilla               </td>
        </tr>
    </tbody>

最佳答案

好吧,我可以使用我之前提到的 sortable.js 对您的表进行排序。只需加载脚本并将表类设置为“可排序”即可。

即:

<table id="scheduletable" class="sortable">

这是一个 JSFiddle 示例:http://jsfiddle.net/yfbedzt4/

此外,如果您开始使用 CSS 样式表来设计表格而不是手动向每个元素添加样式,您的生活将会轻松得多。

编辑:我稍微整理了一下表格,并在此处添加了一些 CSS 样式:http://jsfiddle.net/yfbedzt4/1/

关于php - 在 Drupal 中使用 MYSQL 查询可排序 HTML 表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25367437/

相关文章:

PHP:递归修改数组?

javascript - 使用 Javascript 将值传递给模态弹出窗口

javascript - JQuery 没有运行

php - 如何制作具有相同角色的多用户网站

mysql - 使用 NoSql 功能装饰现有的关系型 SQL 数据库

python - Django 上的属性错误

php - MySQL 查询Where Column Like Column

php - 我应该如何在我的网站上实现 "like"计数器? (不是 Facebook )

javascript - 我的 jquery 切换出了什么问题

javascript - Tab 在 Jquery 中消失了