GIDForums  

Go Back   GIDForums > Computer Programming Forums > MySQL / PHP Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 03-Oct-2003, 05:53
dean dean is offline
New Member
 
Join Date: Oct 2003
Posts: 1
dean is an unknown quantity at this point

Sorting columns by selection


Hi,

I'm new to PHP so need a bit of help. I am setting up a simple site which lists job numbers, client names, date for completion, etc.

I have set up the database, login, and table repeats. However, I would like to be able to allow the users to sort the columns by clicking on the column text/headers - for example if you click client name it will sort the whole table listing the client names from A-Z.

I've tried loads of different ways, but like I say I'm new to PHP so just guessing really. Does anyone have some sample code I could use or any suggestions?

Thanks,


Dean


PS: I tried to sort the table using ORDER BY xyz command - where I wanted the xyz to be a URL variable. I thought I could then use links such as .php?sort=client, .php?sort=date, etc. Is this possible in PHP as I could only receive errors - due to my inexperience in php!!

Help!!
  #2  
Old 03-Oct-2003, 06:29
Allowee's Avatar
Allowee Allowee is offline
Regular Member
 
Join Date: May 2003
Location: The Netherlands
Posts: 339
Allowee has a spectacular aura about
Here's a part of a script I use

Since you are new to PHP I've removed some special security stuff, just to make it simple

PHP Code:

<?php
Define('USERS_TABLE', 'users');
if (!isset($_GET['how'])) {
    $_GET['how'] = "ASC";
}

if ($_GET['how'] == "ASC") {
    $how="DESC";
} elseif ($_GET['how'] == "DESC") {
    $how="ASC";
}

?>
</div>
<table>
<tr>
    <td width='150'><a href="<?php echo $_SERVER["PHP_SELF"]."?page=members.php&amp;sort=user&amp;how=".$how; ?>">Username</a></td>
    <td width='190'><a href="<?php echo $_SERVER["PHP_SELF"]."?page=members.php&amp;sort=name&amp;how=".$how; ?>">Name</a></td>
    <td width='50'>E-Mail</td>
    <td width='70'><a href="<?php echo $_SERVER["PHP_SELF"]."?page=members.php&amp;sort=count&amp;how=".$how; ?>">PM Count</a></td>
</tr>
<?php
if (isset($_GET['sort'])) {

        $Query = "SELECT * FROM ".USERS_TABLE." WHERE inmemlist = 'TRUE'
                    ORDER BY ".$sort." ".$_GET['how']." LIMIT ".$lpage.", 15";
        $Result = $database->query($Query);


} elseif (!isset($_GET['sort'])) {
    $Query = "SELECT * FROM ".USERS_TABLE." WHERE inmemlist = 'TRUE'
                ORDER BY id ASC LIMIT ".$lpage.", 15";
    $Result = $database->query($Query);
}

while ($mem = mysql_fetch_assoc($Result)) {


    if ($mem['showmail'] == "TRUE") {
        $mem['email'] = "<a href=\"mailto:".$mem['email']."\">Mail</a>";
    } else {
        $mem['email'] = "<a href=\"index.php?page=email.php&amp;recipient=".$mem['user']."\">Hidden</a>";
    }
?>
<tr>
    <td><?php echo $mem['user']; ?></td>
    <td><?php echo $mem['name']; ?></td>
    <td><?php echo $mem['email']; ?></td>
    <td><?php echo $mem['count']; ?></td>
</tr>

<?php

}


you can use it like:
.php?sort=name&how=ASC or:
.php?sort=name&how=DESC

you can also change 'sort=' value
__________________
Pastebin
PHP Documentation Site
Allowee's Blog http://allowee.net
 
 

Recent GIDBlogFirst week of IA training by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Sorting a 2D array c++ mike3340 CPP / C++ Forum 4 15-Dec-2003 13:35
vector sorting gotnospoon CPP / C++ Forum 0 10-Nov-2003 15:26
Sorting A Function misunderstood MySQL / PHP Forum 1 02-Oct-2003 08:44
SORT / ORDER BY multi columns in MySQL misunderstood MySQL / PHP Forum 3 01-Oct-2003 09:01
Opinions Wanted: do 2 columns or 3 look best for this site layout? jps0 Websites Reviewed Forum 10 20-Aug-2003 15:30

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 08:46.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.