NamePros
Welcome, Guest! Ready to make a name for yourself in the domain business? We welcome both the hobbyist and professional domainer to join the discussion as part of the NamePros community.

Click here to create your profile to start earning reputation for posting, and trader ratings for buying & selling in our free e-marketplace. Build your trader rating with each successful sale. Our system has tracked over 100,000 sales and counting!
FAQ & TOS Register Search Today's Posts Mark Forums Read

Go Back   NamePros.com > Website Development Discussion Forums > Programming
Reload this Page Alphabetizing a textbox

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

Advanced Search


Closed Thread
 
LinkBack Thread Tools
Old 08-07-2007, 11:42 AM THREAD STARTER               #1 (permalink)
jdk
Senior Member
 
jdk's Avatar
Join Date: Jul 2004
Location: Florida
Posts: 1,492
jdk has a reputation beyond reputejdk has a reputation beyond reputejdk has a reputation beyond reputejdk has a reputation beyond reputejdk has a reputation beyond reputejdk has a reputation beyond reputejdk has a reputation beyond reputejdk has a reputation beyond reputejdk has a reputation beyond reputejdk has a reputation beyond reputejdk has a reputation beyond repute
 



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, 12:06 PM   #2 (permalink)
NamePros Regular
Join Date: Mar 2006
Location: United Kingdom
Posts: 413
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');
????: NamePros.com http://www.namepros.com/programming/358930-alphabetizing-a-textbox.html
sort($names);
print_r($names); 
Will output all values in the $names array alphabetically
__________________
Linux Screenshots
lee101 is offline  
Old 08-07-2007, 12:07 PM   #3 (permalink)
cef
NamePros Regular
Join Date: May 2004
Location: NYC
Posts: 236
cef is a jewel in the roughcef is a jewel in the roughcef is a jewel in the rough
 


Animal Rescue
PHP Code:
<?php
????: NamePros.com http://www.namepros.com/showthread.php?t=358930
????: NamePros.com http://www.namepros.com/showthread.php?t=358930
/*
    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, 01:18 PM THREAD STARTER               #4 (permalink)
jdk
Senior Member
 
jdk's Avatar
Join Date: Jul 2004
Location: Florida
Posts: 1,492
jdk has a reputation beyond reputejdk has a reputation beyond reputejdk has a reputation beyond reputejdk has a reputation beyond reputejdk has a reputation beyond reputejdk has a reputation beyond reputejdk has a reputation beyond reputejdk has a reputation beyond reputejdk has a reputation beyond reputejdk has a reputation beyond reputejdk has a reputation beyond repute
 



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, 03:12 PM   #5 (permalink)
cef
NamePros Regular
Join Date: May 2004
Location: NYC
Posts: 236
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
????: NamePros.com http://www.namepros.com/showthread.php?t=358930
    
if ($_POST['rsort'])
        
$lines array_reverse($lines,false);
        
    
// make the list display safe
    
$sorted htmlentities(implode("\n",$lines));
????: NamePros.com http://www.namepros.com/showthread.php?t=358930
}
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


Liquid Web Smart Servers  
All times are GMT -7. The time now is 09:36 PM.

Managed Web Hosting by Liquid Web
Domain name forum recommended by Domaining.com Powered by: vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.6.0 Ad Management plugin by RedTyger