Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Page 1 of 2 1 2 LastLast
Results 1 to 10 of 12
  1. #1
    Senior Member ericbusch's Avatar
    Join Date
    Aug 2003
    Location
    Daejeon, South Korea
    Posts
    241
    Member #
    2779
    I am stumped on how to order the data of a column by clicking the column header and having the same data presented in a different order.

    Right now, my table loads and it is ordered by date, but i would like for the user to have the option of clicking the column headers like "Name", "Score", and "Book" and have the results sorted by those criteria.

    Any suggestions?
    Eric
    Blogs For Sale - Each blog is packed with 1000s of signatures and ads are highly integrated into the site. Great CTR! Completely customizable from Admin CP. On Sale!
    Complete Websites For Sale - Fantastic looking sites rich with content. Come complete with domain names. I only have a couple of these.

  2.  

  3. #2
    Senior Member Brak's Avatar
    Join Date
    Apr 2003
    Location
    San Francisco, CA
    Posts
    3,413
    Member #
    1217
    Liked
    2 times
    add on to the end of your sql statement "ORDER BY colomnName [ASC|DESC]

    For example, to order by name do a sql like the following:

    "SELECT colomn FROM table ORDER BY Name ASC"
    Kyle Neath: Rockstar extraordinare
    The blog | The poetry site | The Spore site

  4. #3
    Senior Member ericbusch's Avatar
    Join Date
    Aug 2003
    Location
    Daejeon, South Korea
    Posts
    241
    Member #
    2779
    I've got that...

    I have done that already, but i want to make my column headers links so that the user has control over how the data is sorted. For example, the user can view the data by ASC date OR ASC name by clicking the column title. Do i have to create a new php page for each type of sorting option there is?

    Eric
    Blogs For Sale - Each blog is packed with 1000s of signatures and ads are highly integrated into the site. Great CTR! Completely customizable from Admin CP. On Sale!
    Complete Websites For Sale - Fantastic looking sites rich with content. Come complete with domain names. I only have a couple of these.

  5. #4
    Senior Member Brak's Avatar
    Join Date
    Apr 2003
    Location
    San Francisco, CA
    Posts
    3,413
    Member #
    1217
    Liked
    2 times
    Do something like this:

    PHP Code:
    Link...
    <
    a href="mypage.php?sort=name&order=asc">Sort by Name Ascending</a>

    PHP...

    switch (
    $_GET['sort']){
      case 
    "name":
        
    $sql "SELECT colomnName FROM tableName ORDER BY Name ";
        if(
    $_GET['order']) $sql .= "ASC";else $sql .= "DESC";
      break;

    And change for each option accordingly...
    Kyle Neath: Rockstar extraordinare
    The blog | The poetry site | The Spore site

  6. #5
    Senior Member ericbusch's Avatar
    Join Date
    Aug 2003
    Location
    Daejeon, South Korea
    Posts
    241
    Member #
    2779
    a little confused

    i think this should work though i am pretty new to the PHP world. Could you give me a hand with my code. i'm having a hard time changing each piece of code accordingly.

    Here's how i am connecting to my database and finding and ordering my code:

    PHP Code:
    <?php
    $connection 
    mysql_connect("me.com","root","****");

    mysql_select_db("wli"$connection);


    $result mysql_query ("SELECT * FROM students 
                            ORDER BY student_name LIMIT 250"
    $connection);?>
    How can i modify my code so the code you provided will work. i have played around with this a lot but there seems to be too many variables for me to stumble on to the RIGHT code.

    Thanks for your help!
    Eric
    Blogs For Sale - Each blog is packed with 1000s of signatures and ads are highly integrated into the site. Great CTR! Completely customizable from Admin CP. On Sale!
    Complete Websites For Sale - Fantastic looking sites rich with content. Come complete with domain names. I only have a couple of these.

  7. #6
    Senior Member rosland's Avatar
    Join Date
    Jul 2003
    Location
    Norway
    Posts
    1,944
    Member #
    2096
    Hi.

    I haven't tried to implement this myself, as I've had no use for client sortable tables so far.
    However, its possible to sort displayed tables on the client side by embedding JavaScript. If you can make PHP echo the neccessary JavaScript to the page you're displaying the table on, it should work.
    (I've done something similar on a page where I echo the neccessary JS together with PHP output)

    I found the following script from a Google search. I'm not too hot on JavaScript, so don't ask me how it works, but it does.

    Code:
    <html>
    <head>
    <script>
    function colSort(theCol){
      dir = 1;
      if(theCol.sortOrder == 1) dir = -1;
      theHead = theCol.parentNode;
      theTab = theHead.parentNode;
      for(i=0;i<theHead.cells.length;i++){
        if(theHead.cells[i]==theCol){
          colNum = i;
          theCol.sortOrder=dir;
        } else
          theHead.cells[i].sortOrder=0;
      }
      do {
        sorted = true;
        for(i=1;i<theTab.rows.length-1;i++){
          swap = false;
          fVal = theTab.rows[i].cells[colNum].innerHTML;
          sVal = theTab.rows[i+1].cells[colNum].innerHTML;
          if(!isNaN(fVal * 1)){
            fVal *=1; 
            sVal *=1;
          }
          if(dir==1){
            if(fVal < sVal) swap = true;
          } else {
            if(fVal > sVal) swap = true;
          }
          if(swap == true){ 
            theTab.rows[i].swapNode(theTab.rows[i+1]);
            sorted = false 
          }
        }
      } while (sorted == false);
    }
    </script>
    </head>
    <body>
    <table border="1" align="center">
      <tr> 
        <th onClick="colSort(this)">Name</th>
        <th onClick="colSort(this)">points</th>
        <th onClick="colSort(this)">zip</th>
        <th onClick="colSort(this)">number</th>
        <th onClick="colSort(this)">color</th>
      
      <tr> 
        <td>James</td>
        <td>12</td>
        <td>1352</td>
        <td>1</td>
        <td>red</td>
      </tr>
      <tr> 
        <td>Eric</td>
        <td>42</td>
        <td>5254</td>
        <td>2</td>
        <td>blue</td>
      </tr>
      <tr> 
        <td>Daniel</td>
        <td>64</td>
        <td>2434</td>
        <td>3</td>
        <td>yellow</td>
      </tr>
      <tr> 
        <td>Josh</td>
        <td>72</td>
        <td>2554</td>
        <td>4</td>
        <td>white</td>
      </tr>
      <tr> 
        <td>George</td>
        <td>54</td>
        <td>4521</td>
        <td>5</td>
        <td>green</td>
      </tr>
    </table>
    </body>
    </html>
    S. Rosland

  8. #7
    WDF Staff smoseley's Avatar
    Join Date
    Mar 2003
    Location
    Boston, MA
    Posts
    9,729
    Member #
    819
    Liked
    205 times
    Easier to implement on server side.

  9. #8
    Senior Member rosland's Avatar
    Join Date
    Jul 2003
    Location
    Norway
    Posts
    1,944
    Member #
    2096
    Guess you're right.

    However, wouldn't JS be speedier as all the table data would be stored in cache and wouldn't require the whole page to reload? Especially if the table output contained lets say thumbnail pictures or other bulky data.
    S. Rosland

  10. #9
    Senior Member toadeny's Avatar
    Join Date
    May 2003
    Location
    Wellington, New Zealand
    Posts
    188
    Member #
    1426
    Very simply this will give your users access to sort the data, now i dont know what fields you have, but student_id can be anything you want for that matter.

    I use something similar except, the html links i replace with a drop down. eg. this site here. Site here

    PHP Code:
    Sort by: <br>
    <a href="?sort=student_name&order=asc">Student name Ascending.</a><br>
    <a href="?sort=student_name&order=desc">Student name Descending.</a><br>
    <a href="?sort=student_id&order=Asc">Student id Ascending.</a><br>
    <a href="?sort=student_id&order=desc">Student id Descending.</a><br>
    <? 
    //define values for and connect to database
    $dbconn "localhost";
    $dcuser "root";
    $dbpass "mypass";
    $db     "mydb";

    mysql_connect($dbconn$dbuser$dbpass)
    or die(
    "Could Not Connect To Database");

    mysql_select_db($db)
    or die(
    "Could Not Select Database");

            
    //get sort and order values        
            
    $sort = ($_GET['sort']); 
            
    $order = ($_GET['order']);
            
            
    //check to see if sort and order are defined, else default them.
            
    if (!$sort){
            
    $sort "student_name";
            }
            if (!
    $order){
            
    $order "ASC";
            }
            
            
    //generate a query
            
    $sql "SELECT * students ORDER BY $sort $order LIMIT 250";
            
            
    //execute query
            
    $query1 mysql_query($sql)
            
            
    //loop thru results
            
    while ($result mysql_fetch_array($query1))
            {
            echo 
    $result['studentname'];
            echo 
    "<br>";

  11. #10
    Senior Member ericbusch's Avatar
    Join Date
    Aug 2003
    Location
    Daejeon, South Korea
    Posts
    241
    Member #
    2779
    If you used this method, would the $_GET[variables] be passed on to other pages if there were multiple pages?

    Eric
    Blogs For Sale - Each blog is packed with 1000s of signatures and ads are highly integrated into the site. Great CTR! Completely customizable from Admin CP. On Sale!
    Complete Websites For Sale - Fantastic looking sites rich with content. Come complete with domain names. I only have a couple of these.


Page 1 of 2 1 2 LastLast

Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -6. The time now is 05:06 PM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com