[advanced search]
 

Go Back   NamePros.com > Discussion > Web Design & Development > Programming

Programming PHP, Perl, Ruby on Rails, AJAX, HTML, XHTML, CSS, JavaScript, MySQL and any other coding topics.


Closed Thread
 
LinkBack Thread Tools
Old 08-07-2007, 10:42 AM   #1 (permalink)
jdk
Senior Member
 
jdk's Avatar
 
Join Date: Jul 2004
Location: Florida
Posts: 1,223
306.40 NP$ (Donate)

jdk is a name known to alljdk is a name known to alljdk is a name known to alljdk is a name known to alljdk is a name known to alljdk is a name known to all


Alphabetizing a textbox

I tried searching google, but didn't find anything.

I want to post data into a textbox and click a button to alphabetize the data either A to Z or Z to A

Does anyone know of a code to do such a thing?
jdk is offline  
Old 08-07-2007, 11:06 AM   #2 (permalink)
NamePros Regular
 
Join Date: Mar 2006
Location: United Kingdom
Posts: 344
482.75 NP$ (Donate)

lee101 is a jewel in the roughlee101 is a jewel in the roughlee101 is a jewel in the rough


If you can get all the pieces of data into an array, then you can do it using PHP's sort() function, example:
PHP Code:
$names = array('lee101','jdk');
sort($names);
print_r($names);
Will output all values in the $names array alphabetically
__________________
http://bypasstopsite.com - Submit your proxy!
http://biggertwitter.com - Make twitter a bit bigger!
Currently Developing - Linux Screenshots
lee101 is offline  
Old 08-07-2007, 11:07 AM   #3 (permalink)
cef
NamePros Regular
 
Join Date: May 2004
Location: NYC
Posts: 236
76.50 NP$ (Donate)

cef is a jewel in the roughcef is a jewel in the roughcef is a jewel in the rough

Animal Rescue
PHP Code:
<?php
/*
    Alphabetizes the content of a textarea on a line-by-line basis.
    Sorting is case-insensitive.
    Blank lines are removed.
*/

if (isset($_POST['content'])) // alphabetize everything
{
    
// break the text area into lines
    
$lines = explode("\n", $_POST['content']);
    
    
// now remove blank lines
    
function strip_blanks($val) {return trim($val) == '' ? false : true;}
    
$lines = array_filter($lines,strip_blanks);
    
    
// now alphabetize the array.  The array is passed as a reference,
    // and sorting is done in-situ
    
natcasesort($lines);
        
    
// make the list display safe
    
$sorted = htmlentities(implode("\n",$lines));
}
else
// initialize
{
    
$sorted = '';
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>

    <title>Textarea Sorter</title>
    
</head>
<body>
    <p>
        Enter or paste a list of items.  When you press the "sort" button,
        the list will be sorted alphabetically.  Case is ignored.
    </p>
    <form action="" method="post">
        <textarea cols="64" rows="10" name="content"><?php echo $sorted; ?></textarea><br />
        <input type="submit" value="sort" />
    </form>
</body>
</html>
cef is offline  
Old 08-07-2007, 12:18 PM   #4 (permalink)
jdk
Senior Member
 
jdk's Avatar
 
Join Date: Jul 2004
Location: Florida
Posts: 1,223
306.40 NP$ (Donate)

jdk is a name known to alljdk is a name known to alljdk is a name known to alljdk is a name known to alljdk is a name known to alljdk is a name known to all


Thanks both of you. I think I have what I need to make what I am trying to. I will create something else to sort in reverse order.
jdk is offline  
Old 08-07-2007, 02:12 PM   #5 (permalink)
cef
NamePros Regular
 
Join Date: May 2004
Location: NYC
Posts: 236
76.50 NP$ (Donate)

cef is a jewel in the roughcef is a jewel in the roughcef is a jewel in the rough

Animal Rescue
Oops, forgot about the reverse. That only requires one extra optional call, and another button on the form.

I'll update shortly.

With reverse sorting added:

PHP Code:
<?php
/*
    Alphabetizes the content of a textarea on a line-by-line basis.
    Sorting is case-insensitive.
    Blank lines are removed.
    One form button for forward sorting, another for reverse sorting.
*/

if (isset($_POST['content'])) // alphabetize everything
{
    
// break the text area into lines
    
$lines = explode("\n", $_POST['content']);
    
    
// now remove blank lines
    
function strip_blanks($val) {return trim($val) == '' ? false : true;}
    
$lines = array_filter($lines,strip_blanks);
    
    
// now alphabetize the array.  The array is passed as a reference,
    // and sorting is done in-situ
    
natcasesort($lines);
        
    
// now see if we're reverse sorting
    
if ($_POST['rsort'])
        
$lines = array_reverse($lines,false);
        
    
// make the list display safe
    
$sorted = htmlentities(implode("\n",$lines));
}
else
// initialize
{
    
$sorted = '';
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>

    <title>Textarea Sorter</title>
    
</head>
<body>
    <p>
        Enter or paste a list of items.  When you press the "sort" button,
        the list will be sorted alphabetically.  Case is ignored.
    </p>
    <form action="" method="post">
        <textarea cols="64" rows="10" name="content"><?php echo $sorted; ?></textarea><br />
        <input type="submit" name="sort" value="sort" />
        <input type="submit" name="rsort" value="reverse sort" />
    </form>
</body>
</html>
cef is offline  
Closed Thread


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

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


Site Sponsors
Advertise your business at NamePros

All times are GMT -7. The time now is 02:12 AM.


Powered by: vBulletin® Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0
Template-Modifications by TMS
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85