Hello i want to sort the returning data from my MySql database.

i want to sort them by their type (1,2,3)

This is what i ve done so far.

PHP works fine returns all records $somevariable represents the AND to sort the records

 function all() {
    $type = $data->type;
if($type && $type != "")
    {
        $somevariable=" AND type='$type' ";
    }

try {
         $db = getDB();
        $sql = "SELECT * FROM table WHERE somefield='true' $somevariable ";
        $stmt = $db->query($sql);
        $data = $stmt->fetchAll(PDO::FETCH_OBJ);
        $db = null;
        echo json_encode( $data);       
    }

Javascript

var globalFilter = {};
$(document).ready(function()
{

    $('a:not([data-filter=""])').click(function() {   
       $("#all").html("");
       $.extend( globalFilter, $(this).data("filter") );
       newsFeed(uid,token,apiBaseUrl,baseUrl,lastid,newsfeedPerPage,feedType,public_username,groupID,messageID);
    });

    var obj = {"uid": uid,"token": token,"lastid":lastid,"perpage":perpage};
        $.extend( obj, globalFilter);
        var encodedata=JSON.stringify(obj);
        var url=apiBaseUrl+'api/all'; this calls the php script. Works Fine
});

html

<a href="#" data-filter='{"type":""}'>All</a> <a href="#type1" data-filter='{"type":"1"}'>Type 1</a> <a href="#type2" data-filter='{"type":"2"}'>Type 2</a> <a href="#type3" data-filter='{"type":"3"}'>Type 3</a>

On the table in query there is a field named type that thas 1,2,3 as values
and it returns all the data but when i press the link with the data-filter='{"type":"1"}' i want it to return only the records that have 1 as type

Member Avatar for diafol

Bit confused. The title talks of sorting - do you mean filtering?

yes i want to filter by their type

Check this link
Click Here

You will get some idea ...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.