Jump to content

[SOLVED] Is it possible to reorder data without running another query?


matthewst

Recommended Posts

I have a page that looks like an excel spread sheet. I need to be able to sort this page by different columns. I could make the coulmn headers links to different pages where the query is "ordered by" the appropriate column. The problem is the data is quit large and it takes a while to load.

 

$Cust_Tables = mysql_query("Select table_id, set_no, dist_id, contact_fname, contact_lname, rest_name, rest_city, rest_state_pr FROM abc_tables where status_2<'6' order by rest_name", $db_link);
	$tables = mysql_num_rows($Cust_Tables);

 

Has anyone done this?

Link to comment
Share on other sites

I don't think it's possible.

 

What you could do however, is to make the section of the page where the table is, refresh using AJAX, so that only the table would refresh instead of having the whole page refreshed.

 

I'm not too good with Javascript so you'll have to ask someone else on this.

Link to comment
Share on other sites

Make each heading and link and format it like this:

index.php?sort=date

 

$defualt = "user";

$sort = $_GET["sort"]

if (!empty($sort)) {

SELECT * FROM table ORDER BY $sort

} else {

SELECT * FROM table ORDER BY $default

}

 

That will only sort one direction though. IF you want it to switch between ASC and DESC, you'll have to add another variable in the URL and then test to see how you're sorting ATM and change the links accordingly.

 

Hope this helps.

Link to comment
Share on other sites

I don't think it's possible.

 

What you could do however, is to make the section of the page where the table is, refresh using AJAX, so that only the table would refresh instead of having the whole page refreshed.

 

I'm not too good with Javascript so you'll have to ask someone else on this.

 

It is very possible with PHP. Inefficient but possible.

 

The basic gist is you have all that data in an array. You create your own function to run on that array and sort it using http://us2.php.net/array_walk

 

Not too hard, and I am not sure if it will run fast or not, should be faster than javascript at anyrate.

Link to comment
Share on other sites

Thanks everyone for all the help! I ended up using sorttable.js to sort my data. The only issue I had with it was that it wouldn't sort if the data was created in the table. I had to run all my queries and do all my functions and save all the results to variables. Then echo the variables in the table. The sorttable.js is surprisingly fast at sorting data.

Link to comment
Share on other sites

The only issue I had with it was that it wouldn't sort if the data was created in the table. I had to run all my queries and do all my functions and save all the results to variables. Then echo the variables in the table.

 

No offense, but there must have been an erro in your code. The browser doesn't care one bit if the HTML was constructed by PHP in-line, using variable, whatever. It only cares about the final output. But, glad you got it working. It's a good solution to use when you don't want to put that extra load on your server. But, using JavaScript does have it's drawbacks - i.e. user could have it turned off.

Link to comment
Share on other sites

your query is a bit bloated

<?php
$Cust_Tables = mysql_query("Select 
table_id, set_no, dist_id, contact_fname, contact_lname, rest_name, rest_city, rest_state_pr
FROM abc_tables where 
status_2<'6' order by rest_name", $db_link);?>

Are all those fields properly made in the table and is your table properly optimized and indexed?

Link to comment
Share on other sites

What you could do is actually use the while(fetch) idea to write out a javascript array for them and then sort off those arrays (it be an echo command in the loop) however this is a php forum not js so i guess you have to go somewhere to get help on that

The sorttable.js function will automatically resort a properly formatted HTML table on the fly. No data arrays needed. Very simple, slick tool.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.