-1

I am showing text file record in table with pagination and with how many records they want to view functionality. No i am trying to add a new functionality, User can sort table data based on table columns. So i added sort option at right side of table heading.

Now my problem is, Sorting is working fine but its not based on column. Please tell me whats i am doing wrong?

Here is my PHP script

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

Here is the text file data, which i am reading

http://pastebin.com/pGDymVE0

NOTE: CODE UPDATED

UPDATE 1:

I created function to sort based on column, But its not working correctly.

I am taking help of this question

Sort PHP multi-dimensional array based on key?

Its working fine for address, but not working for float numbers.

    <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>
Community
  • 1
  • 1
pankaj agarwal
  • 171
  • 2
  • 15

1 Answers1

0

I modified my script and i optimize it as much as i can. Now it's working correctly as per my requirement. Every column has sort option, And when we do sort, then column is sorted with pagination and per page record option.

If anyone looking for similar script then this might be helpful

    <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>
pankaj agarwal
  • 171
  • 2
  • 15