TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 05-13-2009, 07:02 PM   #1 (permalink)
The Contributor
 
Join Date: May 2008
Posts: 36
Thanks: 5
Kay1021 is on a distinguished road
Confused Control the organization of data table by headers

I have a table that contains data from my database. I was wondering if there was a way that I could make the headers (name, date joined, expiry date and fee) control the order of the data in both ascending and descending order. And make the which header is organizing the data currently look different (ie. when data is organized by name...name is bold while other headers are not). I've tried searching for how to do this...but have had no luck with actually accomplishing it.

I'd appreciate any help.

Thanks


Here's my code:
PHP Code:
<?php 
print
<table class="myTable"> 
    <thead> 
      
    <th scope="col"><h2>Name</h2></th> 
    <th scope="col"><h2>Phone Number</h2></th> 
    <th scope="col"><h2>Email</h2></th> 
    <th scope="col"><h2>Date Joined</h2></th> 
    <th scope="col"><h2>Expiry Date</h2></th> 
    <th scope="col"><h2>Fee Paid</h2></th> 
    <th scope="col"><h2>Edit</h2></th> 
    <th scope="col"><h2>Delete</h2></th> 
      
    </thead>'

      
      
    
$sql "SELECT name, phone, email, date_joined, expiry_date, fee FROM customer ORDER BY name";
      
      
    
$qry mysql_query($sql); 
      
    if (
mysql_num_rows($qry) > 0) { 
        while (
$rs mysql_fetch_assoc($qry)) { 
              
              
            print

            <td>' 
$rs['name'] . '</td> 
            <td>' 
$rs['phone'] . '</td> 
            <td>' 
$rs['email'] . '</td> 
            <td>' 
$rs['date_joined'] . '</td> 
            <td>' 
$rs['expiry_date'] . '</td> 
            <td>' 
$rs['fee'] . '</td> 
            <td><a href="edit.php">Edit</a></td> 
            <td><a href="delete.php">Delete</a></td><tr>'

        } 
    } else { 
        print 
'<h4>no result found</h4>'
    } 
print

</table>'

?>
Kay1021 is offline  
Reply With Quote
Old 05-13-2009, 07:29 PM   #2 (permalink)
The Gregarious
 
allworknoplay's Avatar
 
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
allworknoplay is on a distinguished road
Default

There's a lot of javascripts out there that can do this. Of course I don't know of any offhand but I've played with a lot in the past.

If you only want to do it the PHP way, well you would make each a link that references the page. So when a user clicks on the link, it re-posts the page and provides added data to your SQL query like ASC or DESC for example...

Since you clicked on the header, you can now change the color to show that this header is highlighted and clicked on...

The JS way though is a bit smoother because it doesn't have to reload the page, it just takes data that has already been outputted...

I'm not that very good at JS though so you'd have to get a response from the JS experts around here..
allworknoplay is offline  
Reply With Quote
The Following User Says Thank You to allworknoplay For This Useful Post:
Kay1021 (05-13-2009)
Old 05-13-2009, 09:51 PM   #3 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

Take the following as an example. Hopefully it should explain it well, and give you also a working example to build upon

php Code:
/* The original query with blanks to be filled in. */
$szSQL = "SELECT * FROM members ORDER by %s %s";

/* Set the defaults if invalid fields are supplied, or none at all. */
$szDefaultField = 'name';
$szDefaultOrder = 'asc';

/* Set the restrictions for both so users can't enter just anything. */
$aAllowedFields = array('name', 'username');
$aAllowedOrders = array('desc', 'asc');

/* Set to the ones specified, but if we have none, set to the defaults. */
$szField = isset($_GET['field']) ? strtolower($_GET['field']) : $szDefaultField;
$szOrderBy = isset($_GET['order']) ? strtolower($_GET['order']) : $szDefaultOrder;

/* Check if we're allowed this particular field in the query. */
if (!in_array($szField, $aAllowedFields))
{
    $szField = $szDefaultField;
}

/* Check if we're allowed this particular ordering in the query. */
if (!in_array($szOrderBy, $aAllowedOrders))
{
    $szOrderBy = $szDefaultOrder;
}

/* Build the new query with the ordering on field and ASC/DESC. */
$szSQL = sprintf($szSQL, $szField, $szOrderBy);

/* Query to execute... */
printf("Executing Query: %s", $szSQL);

Here is the form to be used in conjunction with this code:

html4strict Code:
<h1>Orders</h1>

<ul>
    <li><a href="?field=name&order=asc">Order by Name Ascending</a></li>
    <li><a href="?field=username&order=asc">Order by Username Ascending</a></li>
    <li><a href="?field=name&order=desc">Order by Name Descending</a></li>
    <li><a href="?field=username&order=desc">Order by Username Descending</a></li>
</ul>
Attached Files
File Type: php Order.php (1.4 KB, 95 views)
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 05-13-2009, 09:59 PM   #4 (permalink)
The Gregarious
 
allworknoplay's Avatar
 
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
allworknoplay is on a distinguished road
Default

BTW, WH:

Did your avatar come from a bigger picture somewhere or did you make it up yourself? It kinda looks like it has a matrix background, it would be a cool wallpaper if you had a higher resolution pic of it...
allworknoplay is offline  
Reply With Quote
Old 05-13-2009, 11:01 PM   #5 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

It's not THAT big a picture, sadly.



Artist: http://fredeinaudi.com/main.html
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 05-13-2009, 11:03 PM   #6 (permalink)
The Gregarious
 
allworknoplay's Avatar
 
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
allworknoplay is on a distinguished road
Default

LOL that is such a freaky picture, I love it!

Last edited by Wildhoney : 05-14-2009 at 02:22 AM. Reason: Removed quote because thread was looking loooooooong! :)
allworknoplay is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
feedback on my class please frostyboy33 Advanced PHP Programming 7 10-22-2012 09:12 AM
Databse structure Village Idiot TalkPHP Developer Team 3 01-16-2009 10:31 PM
This project has begun! Village Idiot TalkPHP Developer Team 40 01-01-2009 04:29 AM
problem getting data on the html table.... jetnet1 General 2 11-24-2008 06:55 PM
Creating a table from loaded data benton General 5 04-20-2008 11:48 AM


All times are GMT. The time now is 05:13 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design