Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7770211
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T16:21:17+00:00 2026-06-01T16:21:17+00:00

I have a object i that i list in HTML tables. But i want

  • 0

I have a object i that i list in HTML tables. But i want to able to click different columns in this table to “sort” the the data based on that field in question.

As I’m aware of objects are not sorted, so i need to convert it to an array some how ? And then i use href links to call a sort function to sort the array before repopulating the table.

Was hopeing some one may be able to shed light to explain how i do it ?

This is basically how far i got:

$get = mysql_query("SELECT * FROM game_buildings") or die(mysql_error());
        $i = 0;
        while($row = mysql_fetch_assoc($get)){
            $data[$i][0] = $row['name'];
            $data[$i][1] = $row['id'];
            $i ++;
            }
$data = json_encode($data);
?>
<script>var data = <?echo $data; ?>; </script>

Then the table:

<table id="list" style="width:70%;margin: 0 auto;" border="1">
    <tr>
  <td align="center">
        <a href="#" onclick='javascript:sort('name')'>Name</a>
      </td>
  <td align="center">
        <a href="#" onclick='javascript:sort('type')'>Type</a>
      </td>
    </tr>

<script>
  for(var key in data){
    result += '<tr><td>'+data[key][0]+'</td><td>'+data[key][1]+'</td></tr>';
  }
    document.getElementById('list').innerHTML += result;
</script>


</table>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T16:21:19+00:00Added an answer on June 1, 2026 at 4:21 pm

    data should already be an array. You can verify this by checking the output from json_encode($data):

    <script>var data = [["foo",1],["bar",2],...];</script>
    

    But, "name" and "type" don’t really have any relevance with these, which you seem to want:

    onclick="javascript:sort('name')"
    onclick="javascript:sort('type')"
    

    You can change that by keeping the mysql_fetch_assoc structure:

    while($row = mysql_fetch_assoc($get)){
        $data[] = $row;
    }
    

    In theory, at least, the output to JavaScript would then be:

    <script>var data = [{"name":"foo","id":1,"type":...},...];</script>
    

    With an array of objects, you can use the sort method to sort data by the values of the inner objects:

    function sort(key) {
        data = data.sort(function (a, b) {
            var A = a[key], B = b[key];
    
            if (A < B)
                return -1;
            if (A > B)
                return 1;
    
            return 0;
        });
    
        // ...
    }
    

    One way to update the table is to remove the old rows and append new (in place of the // ... above):

    var table = document.getElementById('list');
    var oldRows = [].slice.call(table.rows, 1); // 1 = skip the "header" row(s)
    
    for (var r = 0, l = oldRows.length; r < l; r++) {
        oldRows[r].parentNode.removeChild(oldRows[r]);
    }
    
    for (var i = 0, l = data.length; i < l; i++) {
        table.innerHTML += '<tr><td>' + data[i].name + '</td><td>' + data[i].id + '</td></tr>';
    }
    

    Example: http://jsfiddle.net/coiscir/GB2v3/1/ (albeit, using “id” rather than “type”)

    With this, you can take the initial loop out of the markup as well:

    window.onload = function () {
        sort('name');
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an object in a list that I need to rank several different
I have a Result object that lets me pass around a List of event
I have a list of class items List<MyClass> I have a seperate object that
I have a list of object that is the data source. Something like that:
I have a list of object that need to be displayed and moveable on
I have a list of Object arrays (List) That I am going to pass
I have a parent object called Page that has a List of objects called
I have a list object List<Documents> , and inside that I have another list
I have an ASP.NET WebService that returns an object of List public class Students
I have an array of object names. These objects are HTML elements. I want

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.