Current location: Hot Scripts Forums » Programming Languages » PHP » sort html data table (up/down arrows)


sort html data table (up/down arrows)

Reply
  #1 (permalink)  
Old 01-10-10, 01:51 PM
cubob cubob is offline
New Member
 
Join Date: Jan 2010
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
sort html data table (up/down arrows)

I have data in a mysql table and I need a system that make it possible to move up and down this data (by means of arrows button (up and down)) rows whenever they are rendered in the browser and the write back or remember this order for the next shows. The submit of the form for every sort operation is not a problem, as the mass of data is small. I just need a good and simple pattern to follow, not the detailed code.
Thank you.
Reply With Quote
  #2 (permalink)  
Old 01-10-10, 02:25 PM
Jcbones Jcbones is offline
Aspiring Coder
 
Join Date: Mar 2009
Location: North Carolina, USA
Posts: 516
Thanks: 5
Thanked 47 Times in 44 Posts
Un-tested.

PHP Code:

<?php

if(isset($_GET['sort'])) {
    
$sort $_GET['sort'];
        
$file fopen('sort.txt','w');
        
fwrite($file,$sort);
        
fclose($file);
}
else {
    
$sort file_get_contents('sort.txt');
}

$sort = (isset($sort)) ? "ORDER BY `$sort`" NULL;

$sql "SELECT * FROM `table` $sort";
?>
Reply With Quote
  #3 (permalink)  
Old 01-10-10, 02:44 PM
Jcbones Jcbones is offline
Aspiring Coder
 
Join Date: Mar 2009
Location: North Carolina, USA
Posts: 516
Thanks: 5
Thanked 47 Times in 44 Posts
Tested.

PHP Code:

<?php
include('config.php');
$table ' ';
if(isset(
$_GET['sort'])) {
    
$sort $_GET['sort'];
        
$file fopen('sort.txt','w');
        
fwrite($file,$sort);
        
fclose($file);
}
else {
    
$sort file_get_contents('sort.txt');
}

$sort = ($sort != NULL) ? "ORDER BY `$sort`" NULL;

$sql "SELECT * FROM `$table$sort";
                                    
$result mysql_query($sql);
if(
mysql_num_rows($result) > 0) {

    echo 
'<table border="1">';
    
$i 0;
    while(
$r mysql_fetch_assoc($result)) {
        if(
$i == 0) {
            foreach(
$r as $k=>$v) {
                echo 
'<th>' $k ' <a href="?sort=' $k '">^</a></th>';
                
$keys[] = $k;
            }
        }
        
$i++;
        echo 
'<tr>';
            foreach(
$keys as $v) {
                echo 
'<td>' $r[$v] . '</td>';
            }
        echo 
'</tr>';
        
    }
    
    echo 
'</table>';
    
}


?>
Reply With Quote
  #4 (permalink)  
Old 01-10-10, 08:41 PM
End User's Avatar
End User End User is offline
Level II Curmudgeon
 
Join Date: Dec 2004
Posts: 3,027
Thanks: 14
Thanked 35 Times in 33 Posts
I'd strongly recommend adding this code to help prevent potential naughtiness:

$sort = preg_replace("/[^0-9a-z]/i",'', $_GET['sort']);
__________________
I don't live on the edge, but sometimes I go there to visit.
-------------------------------------------------------------------------
Sanitize Your Data | Oracle Date & Substring Functions | Code Snippet Library | [url=http://www.codmb.com/Call Of Duty[/url]
Reply With Quote
  #5 (permalink)  
Old 01-10-10, 09:00 PM
wirehopper's Avatar
wirehopper wirehopper is offline
-
 
Join Date: Feb 2006
Posts: 2,515
Thanks: 20
Thanked 109 Times in 106 Posts
You might want to use session variables:

PHP Code:

<?php 

session_start
();  /* This line must be before any output is sent, including headers */
if (isset($_GET['sort']))  /* If a sort value was sent */
  
$sort=preg_replace("/[^0-9a-z]/i",''$_GET['sort']);  /* As noted by End User */
else
  if (isset(
$_SESSION['sort']))  /* If there is an existing sort value */
    
$sort=$_SESSION['sort'];
  else  
/* No sort sent or stored */
    
$sort='';
$_SESSION['sort']=$sort;

/* At this point, $sort will be the sort value, and it will be saved in $_SESSION['sort'] for the next access */
Reply With Quote
  #6 (permalink)  
Old 01-11-10, 04:21 AM
cubob cubob is offline
New Member
 
Join Date: Jan 2010
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks...anyway your response does not seem to help me...read carefully the question
Reply With Quote
  #7 (permalink)  
Old 01-11-10, 05:44 AM
Jcbones Jcbones is offline
Aspiring Coder
 
Join Date: Mar 2009
Location: North Carolina, USA
Posts: 516
Thanks: 5
Thanked 47 Times in 44 Posts
I re-read your question, and it is exactly what you ask for. If you need something different, please specify.

Quote:
I need a system that make it possible to move up and down this data (by means of arrows button (up and down)) rows whenever they are rendered in the browser and the write back or remember this order...not the detailed code.
For sanitation, I would put good strings in an array, then use in_array(). The strings aren't going to change.
Reply With Quote
  #8 (permalink)  
Old 01-11-10, 05:46 AM
Jcbones Jcbones is offline
Aspiring Coder
 
Join Date: Mar 2009
Location: North Carolina, USA
Posts: 516
Thanks: 5
Thanked 47 Times in 44 Posts
Quote:
Originally Posted by wirehopper View Post
You might want to use session variables:

PHP Code:

<?php 

session_start
();  /* This line must be before any output is sent, including headers */
if (isset($_GET['sort']))  /* If a sort value was sent */
  
$sort=preg_replace("/[^0-9a-z]/i",''$_GET['sort']);  /* As noted by End User */
else
  if (isset(
$_SESSION['sort']))  /* If there is an existing sort value */
    
$sort=$_SESSION['sort'];
  else  
/* No sort sent or stored */
    
$sort='';
$_SESSION['sort']=$sort;

/* At this point, $sort will be the sort value, and it will be saved in $_SESSION['sort'] for the next access */
I suggested writing to a file, because I thought he wanted it saved between sessions.
Reply With Quote
  #9 (permalink)  
Old 01-11-10, 06:02 AM
cubob cubob is offline
New Member
 
Join Date: Jan 2010
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
I found some solutions:

PHP ::
Reply With Quote
Reply

Bookmarks

Tags
arrow, sort, table


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
php Array to html table rushhh PHP 12 03-17-09 12:22 PM
Problem with a sort table js function tdubyou JavaScript 0 05-03-04 09:19 AM
Classified Ads skipper23 Perl 2 12-30-03 03:43 AM
extracting price quantity pairs from HTML table dewed PHP 3 12-11-03 02:31 PM
help plz: format retrieved Mysql data in HTML with PHP paulj000 PHP 2 10-19-03 08:03 PM


All times are GMT -5. The time now is 08:06 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.