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

时间:2014-08-18 16:00:37

标签: php jquery mysql drupal

好的,我将尝试尽可能明确地提出这个问题。我们刚刚开始使用Drupal和第三方为我们构建了我们网站的一部分。我们有一个查看我们的Mysql数据库的视图文件,并从表中提取符合特定搜索条件的课程。例如所有2014年秋季课程。他们按照CID或课程ID排序。 “视图”页面包含“计划结果PHP”页面,然后将该信息呈现到表中。我们希望能够在表格中包含可排序的列。我已经尝试添加一个jscript文件,这将执行此操作而不重新加载表信息,但它没有工作,我不知道为什么。我尝试过使用Drupal_add_js以及插入JS内联。我也尝试过做href =' / shedules?orderby = cname"但这似乎并不能保留来自视图页面的查询数据。我对此感到非常沮丧,我不知道还有什么地方可以看。非常感谢您给我的任何指导!

这是视图页面      

 <!--
    <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>

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

<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>

4 个答案:

答案 0 :(得分:1)

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

即:

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

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

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

编辑:我稍微整理了一下桌面,并在这里添加了一些CSS样式:http://jsfiddle.net/yfbedzt4/1/

答案 1 :(得分:0)

将数据输出到表的drupal方法是使用函数theme_table

手动渲染表格不是标准的drupal技术。

这是关于其工作原理的另一篇文章: http://zgadzaj.com/drupal-for-beginners-how-to-create-a-table-using-themetable

答案 2 :(得分:0)

如果您正在寻找合适的“Drupal方式”,请查看theme_table()。此功能允许您指定要排序的列。

此外,您应该使用db_query()db_select()来构建SQL查询。

这是一个很好的示例,说明如何正确构建查询并在HTML表格上呈现其结果: http://www.rahulsingla.com/blog/2011/05/drupal-7-creating-drupal-style-tables-with-paging-sorting-and-sticky-headers

答案 3 :(得分:0)

好的,谢谢@Kez的帮助,我终于找到了解决方案。 @Kez帮助我解决了第三方在那里遇到的一些问题,但最终为了排序表我必须在表后添加以下代码       <script type="text/javascript"> var st1 = new SortableTable(document.getElementById("scheduletable")); </script>