根据PHP

时间:2015-10-18 05:04:33

标签: php sorting

我在表中显示带有分页的文本文件记录以及他们想要查看功能的记录数。 不,我正在尝试添加新功能,用户可以根据表列对表数据进行排序。所以我在表格标题的右侧添加了排序选项。

现在我的问题是,Sorting工作正常但它不是基于列。 请告诉我,我做错了什么?

这是我的PHP脚本

                <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>


    records per page: <select name="forma" id="dropdownid" onchange="changeView(this.options[this.selectedIndex].value);">
     <option value="5">5</option>
     <option value="2">2</option>
     <option value="10">10</option>
     <option value="all">View All</option>

    </select>
    <script>
        function changeView(perPageRecord){
            var pageNumber="1";
            var pageSort="<?php
    echo $_GET['sort'];
    ?>";
            var pageSortColumn="<?php
    echo $_GET['sortColumn'];
    ?>";
            location="data.php?p="+pageNumber+"&sort="+pageSort+"&sortColumn="+pageSortColumn+"&perPage="+perPageRecord;
        }
    $('#dropdownid').val('<?php
    echo $_GET['perPage'];
    ?>');

    </script>
    <?php
    error_reporting(0);

    $lines = file_get_contents('ids.txt');
    //arsort($lines);

    $perPageRecord = $_GET['perPage'];
    if ($perPageRecord != "")
        $perpage = $perPageRecord; //Number of lines per page
    else
        $perpage = 5;

    $searchString = ""; //Insert word(s) you're searching for

    $pattern = preg_quote($searchString, '/');
    $pattern = "/^.*$pattern.*/m";

    if (preg_match_all($pattern, $lines, $matches)) {
        $line_amount = count($matches[0]);
    } else {
        echo "No matches found";
    }
    //print_r( $line_amount );exit;
    if ($perpage === "all")
        $perpage = $line_amount;
    if ($perpage > $line_amount)
        $perpage = $line_amount;

    //echo $perpage;exit;
    $p     = isset($_GET['p']) ? $_GET['p'] : 1;
    $talle = "<table border='1'><tr><td>Address<div style='float:right;' >";

    if (($_GET['sort'] == "") || ($_GET['sort'] == "desc")) {
         if(($_GET['sort']=="desc") && ($_GET['sortColumn']=="address"))
                $sort="asc";
           else
                $sort="desc";

            $sortColumn = "address";
        $talle = $talle . "<a href='?sort=$sort&sortColumn=$sortColumn'>sort</a>";

        rsort($matches[0]);
    } else if (($_GET['sort'] == "asc") ) {
            if(($_GET['sort']=="asc") && ($_GET['sortColumn']=="address"))
                $sort="desc";
           else
                $sort="asc";
            $sortColumn = "address";
        $talle = $talle . "<a href='?sort=$sort&sortColumn=$sortColumn'>sort</a>";
        sort($matches[0]);
    }
    $talle = $talle .="</div></td><td><div style='float:right;' >";

    //print_r($matches[0]);

    if ((($_GET['sort'] == "") || ($_GET['sort'] == "desc")) ) {
         if(($_GET['sort']=="desc") && ($_GET['sortColumn']=="location"))
                $sort="asc";
           else
                $sort="desc";

            $sortColumn = "location";
        $talle = $talle . "<a href='?sort=$sort&sortColumn=$sortColumn'>sort</a>";

        rsort($matches[0]);
    } else if (($_GET['sort'] == "asc") ) {
          if(($_GET['sort']=="asc") && ($_GET['sortColumn']=="location"))
                $sort="desc";
           else
                $sort="asc";

            $sortColumn = "location";
        $talle = $talle . "<a href='?sort=$sort&sortColumn=$sortColumn'>sort</a>";
        sort($matches[0]);
    }
    $talle = $talle . "</div>Latitude &nbsp;&nbsp;&nbsp;</td><td><div style='float:right;' ></div>Longitude</td></tr>";
    echo $talle;
    for ($i = (($p * $perpage) - $perpage); $i <= (($perpage * $p) - 1); $i++) {
        if ($i >= $line_amount) {
            break;
        } else {
            $rowData = explode(";", $matches[0][$i]);
            //checking if coordinates is not empty
            if (isset($rowData[1]) && (trim($rowData[1]) != "")) {
                echo "<tr>";
                //print_r(rsort($rowData));
                foreach ($rowData as $value) {
                    echo "<td>" . $value . "</td>";
                }
                echo "</tr>";

            }
            // print_r( $matches[0][$i]).'<br />';
        }
    }
    echo "</table>";

    ?>

    <table summary="" cellpadding="10" cellspacing="0"  border="0" class="global-links-menu">
        <tr>

            <?php

    $total_pages = $line_amount / $perpage;
    if ($line_amount % $perpage != 0) {
        $total_pages = $total_pages + 1;
    }
    $sort       = $_GET['sort'];
    $sortColumn = $_GET['sortColumn'];
    if ($sort == "")
        $sort = "asc";
    if ($sortColumn == "")
        $sortColumn = "location";
    if ($p != 1) {
        $back_page = $p - 1;

        echo "<td ><a href='?p=$back_page&sort=$sort&sortColumn=$sortColumn&perPage=$perpage'>Back</a></td>";
    } else {
        $back_page = $p - 1;
        echo "<td >Back</td>";
    }

    for ($j = 1; $j <= $total_pages; $j++) {
        if ($j == $p) {
            echo "<td>$p</td>";
        } else {
            echo "<td><a href='?p=$j&sort=$sort&sortColumn=$sortColumn&perPage=$perpage'>$j</a></td>";
        }
    }

    if ($p <= $total_pages - 1) {
        $next_page = $p + 1;
        echo "<td ><a href='?p=$next_page&sort=$sort&sortColumn=$sortColumn&perPage=$perpage'>Next</a></td>";
    } else {
        echo "<td >Next</td>";
    }

    ?>
        </tr>
    </table>

这是我正在阅读的文本文件数据

http://pastebin.com/pGDymVE0

注意:代码更新

更新1:

我创建了基于列进行排序的函数,但它无法正常工作。

我正在接受这个问题的帮助

Sort PHP multi-dimensional array based on key?

它适用于地址,但不适用于浮点数。

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>


    records per page: <select name="forma" id="dropdownid" onchange="changeView(this.options[this.selectedIndex].value);">
     <option value="5">5</option>
     <option value="2">2</option>
     <option value="10">10</option>
     <option value="all">View All</option>

    </select>
    <script>
        function changeView(perPageRecord){
            var pageNumber="1";
            var pageSort="<?php
    echo $_GET['sort'];
    ?>";
            var pageSortColumn="<?php
    echo $_GET['sortColumn'];
    ?>";
            location="data.php?p="+pageNumber+"&sort="+pageSort+"&sortColumn="+pageSortColumn+"&perPage="+perPageRecord;
        }
    $('#dropdownid').val('<?php
    echo $_GET['perPage'];
    ?>');

    </script>
    <?php
    error_reporting(0);

    $lines = file_get_contents('ids.txt');
    //arsort($lines);

    $perPageRecord = $_GET['perPage'];
    if ($perPageRecord != "")
        $perpage = $perPageRecord; //Number of lines per page
    else
        $perpage = 5;

    $searchString = ""; //Insert word(s) you're searching for

    $pattern = preg_quote($searchString, '/');
    $pattern = "/^.*$pattern.*/m";

    if (preg_match_all($pattern, $lines, $matches)) {
        $line_amount = count($matches[0]);
    } else {
        echo "No matches found";
    }
    //print_r( $line_amount );exit;
    if ($perpage === "all")
        $perpage = $line_amount;
    if ($perpage > $line_amount)
        $perpage = $line_amount;

    //echo $perpage;exit;
    $p     = isset($_GET['p']) ? $_GET['p'] : 1;
    $talle = "<table border='1'><tr><td>Address<div style='float:right;' >";

    if (($_GET['sort'] == "") || ($_GET['sort'] == "desc")) {
        if (($_GET['sort'] == "desc") && ($_GET['sortColumn'] == "address")) {
            $matches[0] = (address_sort($matches[0], $_GET['sort'], '0'));
            $sort       = "asc";
        } else
            $sort = "desc";

        $sortColumn = "address";
        $talle      = $talle . "<a href='?sort=$sort&sortColumn=$sortColumn'>sort</a>";

        // rsort($matches[0]);
    } else if (($_GET['sort'] == "asc")) {
        if (($_GET['sort'] == "asc") && ($_GET['sortColumn'] == "address")) {
            $matches[0] = (address_sort($matches[0], $_GET['sort'], '0'));
            $sort       = "desc";
        } else
            $sort = "asc";
        $sortColumn = "address";

        //print_r(address_sort($matches[0],$_GET['sort'] ,'0'));
        //  print_r($matches[0]);
        $talle = $talle . "<a href='?sort=$sort&sortColumn=$sortColumn'>sort</a>";
        // sort($matches[0]);
    }
    $talle = $talle .= "</div></td><td><div style='float:right;' >";



    if ((($_GET['sort'] == "") || ($_GET['sort'] == "desc"))) {
        if (($_GET['sort'] == "desc") && ($_GET['sortColumn'] == "location")) {
            $matches[0] = (address_sort($matches[0], $_GET['sort'], '1'));
            $sort       = "asc";
        } else
            $sort = "desc";

        $sortColumn = "location";
        $talle      = $talle . "<a href='?sort=$sort&sortColumn=$sortColumn'>sort</a>";

        //  rsort($matches[0]);
    } else if (($_GET['sort'] == "asc")) {
        if (($_GET['sort'] == "asc") && ($_GET['sortColumn'] == "location")) {
            // echo $_GET['sort'];
            $matches[0] = (address_sort($matches[0], $_GET['sort'], '1'));
            $sort       = "desc";
        } else
            $sort = "asc";

        $sortColumn = "location";
        $talle      = $talle . "<a href='?sort=$sort&sortColumn=$sortColumn'>sort</a>";
        // sort($matches[0]);
    }
    $talle = $talle . "</div>Latitude &nbsp;&nbsp;&nbsp;</td><td><div style='float:right;' ></div>Longitude</td></tr>";
    echo $talle;
    for ($i = (($p * $perpage) - $perpage); $i <= (($perpage * $p) - 1); $i++) {
        if ($i >= $line_amount) {
            break;
        } else {
            $rowData = explode(";", $matches[0][$i]);
            //checking if coordinates is not empty
            if (isset($rowData[1]) && (trim($rowData[1]) != "")) {
                echo "<tr>";
                //print_r(rsort($rowData));
                foreach ($rowData as $value) {
                    echo "<td>" . $value . "</td>";
                }
                echo "</tr>";

            }
            // print_r( $matches[0][$i]).'<br />';
        }
    }
    echo "</table>";

    ?>

    <table summary="" cellpadding="10" cellspacing="0"  border="0" class="global-links-menu">
        <tr>

            <?php

    $total_pages = $line_amount / $perpage;
    if ($line_amount % $perpage != 0) {
        $total_pages = $total_pages + 1;
    }
    $sort       = $_GET['sort'];
    $sortColumn = $_GET['sortColumn'];
    if ($sort == "")
        $sort = "asc";
    if ($sortColumn == "")
        $sortColumn = "location";
    if ($p != 1) {
        $back_page = $p - 1;

        echo "<td ><a href='?p=$back_page&sort=$sort&sortColumn=$sortColumn&perPage=$perpage'>Back</a></td>";
    } else {
        $back_page = $p - 1;
        echo "<td >Back</td>";
    }

    for ($j = 1; $j <= $total_pages; $j++) {
        if ($j == $p) {
            echo "<td>$p</td>";
        } else {
            echo "<td><a href='?p=$j&sort=$sort&sortColumn=$sortColumn&perPage=$perpage'>$j</a></td>";
        }
    }

    if ($p <= $total_pages - 1) {
        $next_page = $p + 1;
        echo "<td ><a href='?p=$next_page&sort=$sort&sortColumn=$sortColumn&perPage=$perpage'>Next</a></td>";
    } else {
        echo "<td >Next</td>";
    }





    function address_sort($myarray, $sortingType, $indexKey)
    {
        foreach ($myarray as $i => $value) {
            $myarray[$i] = explode(";", $value);
        }
        if (trim($sortingType) == "asc")
            $sortType = SORT_ASC;
        else if (trim($sortingType) == "desc")
            $sortType = SORT_DESC;
        else
            $sortType = "nothing";
        $myarray = array_sort($myarray, "'" . $indexKey . "'", $sortType);
        print_r($myarray);
        foreach ($myarray as $i => $value) {
            $myarray[$i] = implode(";", $value);
        }
        return $myarray;
    }

    function array_sort($array, $on, $order = SORT_ASC)
    {

        $new_array      = array();
        $sortable_array = array();

        if (count($array) > 0) {
            foreach ($array as $k => $v) {
                if (is_array($v)) {
                    foreach ($v as $k2 => $v2) {
                        if ($k2 == $on) {
                            $sortable_array[$k] = $v2;
                        }
                    }
                } else {
                    $sortable_array[$k] = $v;
                }
            }

            switch ($order) {
                case SORT_ASC:
                    asort($sortable_array);
                    break;
                case SORT_DESC:
                    arsort($sortable_array);
                    break;
            }

            foreach ($sortable_array as $k => $v) {
                $new_array[$k] = $array[$k];
            }
        }

        return $new_array;
    }
    ?>
        </tr>
    </table>

1 个答案:

答案 0 :(得分:0)

我修改了我的脚本并尽可能地优化它。 现在它按照我的要求正常工作。每个列都有排序选项,当我们进行排序时,则使用分页和每页记录选项对列进行排序。

如果有人在寻找类似的脚本,那么这可能会有所帮助

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>


    records per page: <select name="forma" id="dropdownid" onchange="changeView(this.options[this.selectedIndex].value);">
     <option value="5">5</option>
     <option value="2">2</option>
     <option value="10">10</option>
     <option value="all">View All</option>

    </select>
    <script>
        function changeView(perPageRecord){
            var pageNumber="1";
            var pageSort="<?php
    echo $_GET['sort'];
    ?>";
            var pageSortColumn="<?php
    echo $_GET['sortColumn'];
    ?>";
            location="data.php?p="+pageNumber+"&sort="+pageSort+"&sortColumn="+pageSortColumn+"&perPage="+perPageRecord;
        }
    $('#dropdownid').val('<?php
    echo $_GET['perPage'];
    ?>');

    </script>
    <?php
    error_reporting(0);

    $lines = file_get_contents('ids.txt');
    //arsort($lines);

    $perPageRecord = $_GET['perPage'];
    if ($perPageRecord != "")
        $perpage = $perPageRecord; //Number of lines per page
    else
        $perpage = 5;

    $searchString = ""; //Insert word(s) you're searching for

    $pattern = preg_quote($searchString, '/');
    $pattern = "/^.*$pattern.*/m";

    if (preg_match_all($pattern, $lines, $matches)) {
        $line_amount = count($matches[0]);
    } else {
        echo "No matches found";
    }
    //print_r( $line_amount );exit;
    if ($perpage === "all")
        $perpage = $line_amount;
    if ($perpage > $line_amount)
        $perpage = $line_amount;



    $p = isset($_GET['p']) ? $_GET['p'] : 1;

    $talle         = "<table border='1'><tr>";
    $heading_array = array(
        "Country",
        "State",
        "City",
        "Zip",
        "Location",
        "",
        "Latitude",
        "Longitude"
    );
    foreach ($heading_array as $k => $v) {
        if (($_GET['sort'] == "") || ($_GET['sort'] == "desc")) {
            if (($_GET['sort'] == "desc") && (strtolower($_GET['sortColumn']) == strtolower($v))) {
                $matches[0] = (address_sort($matches[0], $_GET['sort'], $k));
                $sort       = "asc";
            } else
                $sort = "desc";
            $sortColumn = $v;
            $sortString = "<a href='?sort=$sort&sortColumn=$sortColumn&perPage=$perpage'>sort</a>";
            // rsort($matches[0]);
        } else if (($_GET['sort'] == "asc")) {
            if (($_GET['sort'] == "asc") && (strtolower($_GET['sortColumn']) == strtolower($v))) {
                $matches[0] = (address_sort($matches[0], $_GET['sort'], $k));
                $sort       = "desc";
            } else
                $sort = "asc";
            $sortColumn = $v;
            $sortString = "<a href='?sort=$sort&sortColumn=$sortColumn&perPage=$perpage'>sort</a>";
        }
        if ($v != "")
            $talle = $talle . "<td><div style='float:right;' >&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" . $sortString . "</div>" . $v . "</td>";
        else
            $talle = $talle . "<td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp</td>";
    }
    $talle = $talle . "</tr>";

    echo $talle;
    for ($i = (($p * $perpage) - $perpage); $i <= (($perpage * $p) - 1); $i++) {
        if ($i >= $line_amount) {
            break;
        } else {
            $rowData = explode(";", $matches[0][$i]);
            //checking if coordinates is not empty
            if (isset($rowData[1]) && (trim($rowData[1]) != "")) {
                echo "<tr>";
                //print_r(rsort($rowData));
                foreach ($rowData as $value) {
                    echo "<td>" . $value . "</td>";
                }
                echo "</tr>";

            }
            // print_r( $matches[0][$i]).'<br />';
        }
    }
    echo "</table>";

    ?>

    <table summary="" cellpadding="10" cellspacing="0"  border="0" class="global-links-menu">
        <tr>

            <?php

    $total_pages = $line_amount / $perpage;
    if ($line_amount % $perpage != 0) {
        $total_pages = $total_pages + 1;
    }
    $sort       = $_GET['sort'];
    $sortColumn = $_GET['sortColumn'];
    if ($sort == "")
        $sort = "asc";
    if ($sortColumn == "")
        $sortColumn = "location";
    if ($p != 1) {
        $back_page = $p - 1;

        echo "<td ><a href='?p=$back_page&sort=$sort&sortColumn=$sortColumn&perPage=$perpage'>Back</a></td>";
    } else {
        $back_page = $p - 1;
        echo "<td >Back</td>";
    }

    for ($j = 1; $j <= $total_pages; $j++) {
        if ($j == $p) {
            echo "<td>$p</td>";
        } else {
            echo "<td><a href='?p=$j&sort=$sort&sortColumn=$sortColumn&perPage=$perpage'>$j</a></td>";
        }
    }

    if ($p <= $total_pages - 1) {
        $next_page = $p + 1;
        echo "<td ><a href='?p=$next_page&sort=$sort&sortColumn=$sortColumn&perPage=$perpage'>Next</a></td>";
    } else {
        echo "<td >Next</td>";
    }





    function address_sort($myarray, $sortingType, $indexKey)
    {
        foreach ($myarray as $i => $value) {
            $myarray[$i] = explode(";", $value);
        }
        if (trim($sortingType) == "asc")
            $sortType = SORT_ASC;
        else if (trim($sortingType) == "desc")
            $sortType = SORT_DESC;
        else
            $sortType = "nothing";
        $myarray = array_sort($myarray, $indexKey, $sortType);
        $j       = 0;
        foreach ($myarray as $i => $value) {
            $myarray[$j] = $value;
            $j++;
        }

        foreach ($myarray as $i => $value) {
            $myarray[$i] = implode(";", $value);
        }

        return $myarray;
    }

    function array_sort($array, $on, $order = SORT_ASC)
    {

        $new_array      = array();
        $sortable_array = array();

        if (count($array) > 0) {
            foreach ($array as $k => $v) {
                if (is_array($v)) {
                    foreach ($v as $k2 => $v2) {
                        //echo $k2." -----  ".$on;
                        if ($k2 == $on) {
                            //if(is_numeric($v2))
                            $sortable_array[$k] = $v2;
                        }
                    }
                } else {
                    $sortable_array[$k] = $v;
                }
            }

            switch ($order) {
                case SORT_ASC:
                    asort($sortable_array);
                    break;
                case SORT_DESC:
                    arsort($sortable_array);
                    break;
            }
            foreach ($sortable_array as $k => $v) {
                $new_array[$k] = $array[$k];
            }
        }

        return $new_array;
    }
    ?>
        </tr>
    </table>