473,805 Members | 2,119 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Making dynamic table sortable

I'm doing a small project right now where I create a table from a
database. This is the code, which Dreamweaver, for the most part,
has written:

***

<?php
mysql_select_db ($database_tdre am, $tdream);
$query_rstdream = "SELECT * FROM albums ORDER BY `year` ASC";
$rstdream = mysql_query($qu ery_rstdream, $tdream) or die
(mysql_error()) ;
$row_rstdream = mysql_fetch_ass oc($rstdream);
$totalRows_rstd ream = mysql_num_rows( $rstdream);
?>

***

<table cellspacing="0" >
<thead>
<tr>
<td>Album</td>
<td>Year</td>
<td>Era</td>
<td>Type</td>
</tr>
</thead>
<tbody>
<?php do { ?>
<?php
$class = ($class == 'odd') ? 'even' : 'odd';
?>
<tr class="<?php echo $class ?>">
<td><?php echo $row_rstdream['Name']; ?></td>
<td><?php echo $row_rstdream['Year']; ?></td>
<td><?php echo $row_rstdream['Era']; ?></td>
<td><?php echo $row_rstdream['Type']; ?></td>
</tr>
<?php } while ($row_rstdream = mysql_fetch_ass oc($rstdream));
?>
</tbody>
</table>

***

Sorry for the length of the examples. I have a basic
understanding of what the code is doing, so I should be able to
modify it if I knew I was heading in the right direction. My
object is to make this table sortable, by clicking on the column
headers. I have an additional column in my database table which
is an auto_increment counter. Would anyone have some ideas or
code that could help me figure this out? I'm not sure how to
approach it, and was hoping that there was a simple answer to the
problem.

TIA
Ian
--
http://www.bookstacks.org/
Jul 17 '05
34 7660
I noticed that Message-ID: <Xn************ *************** @130.133.1.4>
from Ian Rastall contained the following:
Thanks. I just tried it, but I ended up getting an error:

Unknown column 'Year,ID' in 'order clause'


Yes, because it would have to be either `Year`,`ID` or just Year, ID

Delete the back ticks from the ORDER BY bit and it will work.

Sheesh, you give up quick..LOL.

--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Jul 17 '05 #11
In comp.lang.php Geoff Berrow wrote:
it would have to be either `Year`,`ID` or just Year, ID
I tried the second already, but hadn't given the first one a
shot. When I did, I got this error:

You have an error in your SQL syntax near 'Year`,`ID`` ASC' at
line 1

It's written in my document like:

"?order=`Year`, `ID`"
Delete the back ticks from the ORDER BY bit and it will work.


When I write it like:

"?order=Yea r, ID"

The error is:

Unknown column 'Year, ID' in 'order clause'

Ian
--
http://www.bookstacks.org/
Jul 17 '05 #12
In comp.lang.php Geoff Berrow wrote:
Delete the back ticks from the ORDER BY bit and it will work.
Oh, um, I did what this actually says, deleted the back ticks
from around the variable at the ORDER BY bit, and it works. Thank
you!
Sheesh, you give up quick..LOL.


Well, I'm trying not to take up too much of everyone's time.
There's another question, actually ... which is how to alternate
between ascending and descending sorts. I think possibly putting
an if/then statement at the top of the file, and a variable in
the ORDER BY section to take the place of ASC. I'm not sure,
though, what to do at the top to tell the browser to switch to
descending and back again.

The code is:

***

<?php
$order="Year";//declare the default value once, at the top.
if (isset($_GET['order'])){
$order=$_GET['order'];
}
mysql_select_db ($database_tdre am, $tdream);
$query_rstdream = "SELECT * FROM albums ORDER BY $order ASC";
$rstdream = mysql_query($qu ery_rstdream, $tdream) or die
(mysql_error()) ;
$row_rstdream = mysql_fetch_ass oc($rstdream);
$totalRows_rstd ream = mysql_num_rows( $rstdream);
?>

***

<table class="sort-table" id="table-1" cellspacing="0" >
<thead>
<tr>
<td><a href="<?php print $_SERVER['PHP_SELF']."?order=Nam e";
?>">Name</a></td>
<td><a href="<?php print $_SERVER['PHP_SELF']."?order=Year,I D";
?>">Year</a></td>
<td><a href="<?php print $_SERVER['PHP_SELF']."?order=Era ";
?>">Era</a></td>
<td><a href="<?php print $_SERVER['PHP_SELF']."?order=Typ e";
?>">Type</a></td>
</tr>
</thead>
<tbody>
<?php do { ?>
<?php
$class = ($class == 'odd') ? 'even' : 'odd';
?>
<tr class="<?php echo $class ?>">
<td><?php echo $row_rstdream['Name']; ?></td>
<td><?php echo $row_rstdream['Year']; ?></td>
<td><?php echo $row_rstdream['Era']; ?></td>
<td><?php echo $row_rstdream['Type']; ?></td>
</tr>
<?php } while ($row_rstdream = mysql_fetch_ass oc($rstdream));
?>
</tbody>
</table>

***

TIA
Ian
--
http://www.bookstacks.org/
Jul 17 '05 #13
I noticed that Message-ID: <Xn************ *************** @130.133.1.4>
from Ian Rastall contained the following:
I think possibly putting
an if/then statement at the top of the file, and a variable in
the ORDER BY section to take the place of ASC. I'm not sure,
though, what to do at the top to tell the browser to switch to
descending and back again.


Well, to toggle it, ie to change the order each time you press the
column header will require the page to remember its state and as html is
stateless, this means passing the state back to the script somehow
possibly as a session variable.

Alternatively, why not have a pair of radio buttons and get the sort
order from them?

If you always want the same order each column you can pass the variable
in the link just like you did with the order.

<td><a href="<?php print
$_SERVER['PHP_SELF']."?order=Type&a mp;direction=DE SC";
?>">Type</a></td>

The direction of sort will then be contained in $_GET['direction']

--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Jul 17 '05 #14
I noticed that Message-ID: <Xn************ *************** @130.133.1.4>
from Ian Rastall contained the following:
Sheesh, you give up quick..LOL.


Well, I'm trying not to take up too much of everyone's time.


Just kidding.
--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Jul 17 '05 #15
In comp.lang.php Geoff Berrow wrote:
Alternatively, why not have a pair of radio buttons and get
the sort order from them?


Thanks for the advice, Geoff. I'll try using radio buttons, since
it would be a breeze to set the variable.

Ian
--
http://www.bookstacks.org/
Jul 17 '05 #16
Try:

a href="<?=$PHP_S ELF;?>?order=<? =urlencode("Yea r DESC, ID ASC");?>"

By the way, the sorting problem might be the table field name, because
for mysql, the name of the field is case sensitive, let's say that the
name of your field is: Joe, if you use a SQL command like this: ORDER
BY joe ASC it gives you an error, because it should be like this: ORDER
BY Joe ASC.
Regards,
Ramiro Varandas Jr

Jul 17 '05 #17
On Sat, 29 Jan 2005 08:20:28 +0000, Geoff Berrow
<bl******@ckdog .co.uk> reverently intoned upon the aether:

<td>< a href="<?php print $_SERVER['PHP_SELF']."?order=Alb um ";
?>">Album</a></td>

I assume the column header is the same as your field header. Remember
it is case sensitive.

Then at the top of your script you do

$order="year";//declare the default value once, at the top.

if (isset($_GET['order'])){
$order=$_GET['order'];
}

finally include that variable in the SELECT

$query_rstdream = "SELECT * FROM albums ORDER BY `$order` ASC";


I would suggest passing $order to a set of if-else if statements.

$sort_order = 'Album';
if($order == 'Year'){
$sort_order = 'Year';
}else if($order == 'Era'){
$sort_order = 'Era';
}else if($order == 'Type'){
$sort_order = 'Type';
}

$query_rstdream = "SELECT * FROM albums ORDER BY $sort_order ASC";

or else a client can enter

http://wherever.net/somefile.php?order=Year; DROP TABLE albums;

or something similar.

This is called a "SQL Injection". Drop that into google and you will
find lots of info.

Also, other data validation techniques exist, you could use
preg_match(..) and log an error if the data is wrong. What the above
if-else does though is guarantee that you are never passing client
input to the database which is much simpler than validating the data.

Whatever you do, just getting it to work on good input, does not imply
it will do what you expect on bad input. You should also note that a
website on the net is wide open to the world.

enjoy,

Sean
"In the End, we will remember not the words of our enemies,
but the silence of our friends."

- Martin Luther King Jr. (1929-1968)

Photo Archive @ http://www.tearnet.com/Sean
Last Updated 29 Sept. 2004
Jul 17 '05 #18
Sean wrote:
On Sat, 29 Jan 2005 08:20:28 +0000, Geoff Berrow
<bl******@ckdog .co.uk> reverently intoned upon the aether:

<td>< a href="<?php print $_SERVER['PHP_SELF']."?order=Alb um ";
?>">Album</a></td>

I assume the column header is the same as your field header. Remember
it is case sensitive.
<snip>

I would suggest passing $order to a set of if-else if statements.

$sort_order = 'Album';
if($order == 'Year'){
$sort_order = 'Year';
}else if($order == 'Era'){
$sort_order = 'Era';
}else if($order == 'Type'){
$sort_order = 'Type';
}

I like to write it like this:

switch ($order) {
case "Year":
case "Era":
case "Type":
$sort_order = $order;
break;

default:
$sort_order = "Year"; // the preferred sort order
break;
}

This way one can see all the valid choices at a glance, and it's plain
easy to add a new valid choice.

$query_rstdream = "SELECT * FROM albums ORDER BY $sort_order ASC";

or else a client can enter

http://wherever.net/somefile.php?order=Year; DROP TABLE albums;

or something similar.

This is called a "SQL Injection". Drop that into google and you will
find lots of info.


Good advice.
Jul 17 '05 #19
I noticed that Message-ID: <29************ *************** *****@4ax.com>
from Sean contained the following:
or else a client can enter

http://wherever.net/somefile.php?order=Year; DROP TABLE albums;

or something similar.

This is called a "SQL Injection". Drop that into google and you will
find lots of info.


I may be wrong but I thought that MySql would not allow this. Must do
some tests.
--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Jul 17 '05 #20

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
3311
by: jeffsal | last post by:
I am using sorttable.js to sort a table which works fine which allows a user to sort the table by clicking on the column header. Is there some code I could add to the page (onload or something) to auto sort by the first column without user clicking on it. Here is the sorttable.js code. addEvent(window, "load", sortables_init); var SORT_COLUMN_INDEX;
1
1654
by: Ross | last post by:
The following codes originally provided by a kind responder Toby A Inkster in another newsgroup for working on displaying a table on web capable of sorting are modified by me (a newbie). How to disable sorting by say, length and chart (last few codes at the end)? I find the order is important to display so cannot just single them out. <?php function insert_datatable_cmp ($a, $b) { return ($a]<$b]) ? -1 : 1; }
1
2651
by: cotton_gear | last post by:
Hello, Fiest of all let me thank this group for so quick in responding to any postings. I am using a javascript based utility from a site to sort the columns of the table. But, for some strange reason it is not working on colmuns containg checkboxes, text-boxes (or, any user input fileds). For checkbox column, the values are lost when clicked on the column title to sort. I tried a lot to modify but couldnt achive what I wanted.
1
2070
by: ferraro.joseph | last post by:
Hi, I'm querying Salesforce.com via their AJAX toolkit and outputting query results into a table. Currently, their toolkit does not possess the ability to do table joins via their structured query language, which forces me to do the join manually via arrays. Right now, I'm having trouble getting these query results (which are in
7
4830
by: Kamal | last post by:
Hello all, I have a very simple html table with collapsible rows and sorting capabilities. The collapsible row is hidden with css rule (display:none). When one clicks in the left of the expandable row, the hidden row is made visible with css. The problem is when i sort the rows, the hidden rows get sorted as well which i don't want and want to be moved (while sorting) relative to their parent rows. The following is my complete html code...
1
16983
by: since | last post by:
I figured I would post my solution to the following. Resizable column tables. Search and replace values in a table. (IE only) Scrollable tables. Sortable tables. It is based on a lot examples I found on the web. Works in IE and mozilla. http://www.imaputz.com/cssStuff/bigFourVersion.html
5
4200
by: Stepheno | last post by:
Hi, I am a recently converted Iseries (AS/400) RPG programmer trying to learn HTML/CSS/JavsScript all at the same time (not fun). My problem deals mostly with CSS. I will be reveiving a table, for which number of rows/columns i will not know, and I have to pretty up the table, including a verical scroll bar. This has to work in IE6, IE7, and FireFox 3.5. The CSS I currently have will give me a scrollable table, but only if I hard code...
5
4962
by: jrod11 | last post by:
hi, I found a jquery html table sorting code i have implemented. I am trying to figure out how to edit how many colums there are, but every time i remove code that I think controls how many colums there are, it crashes. There are currently 6 columns, and I only want 4. How do I remove the last two (discount and date)? Here is a link: http://www.jaredmoore.com/tablesorter/docs/salestable.html Here is some jquery js that I think...
0
9596
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10613
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10363
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10368
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10107
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5544
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4327
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3846
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3008
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.