Jump to content

Recommended Posts

Posted (edited)

Or a server side language.

 

For example...

 

PHP:

 

<?php

$options = array('John', 'Eric', 'Thelma'); // and 97 others
sort($options);

?>

<?php foreach($options as $k=>$v){ ?>
<?php echo $v?>
<?php } ?>


 

Python:

 

options = ["John", "Eric", "Thelma"]
options.sort()
# Gives you a sorted list, then do your HTML thing as above

 

This could be done in javascript, but I wouldn't recommend it. Javascript might be switched off, and if you're injecting 100 elements into the DOM, that would be awfully slow. The best way to do it would be to put all 100 elements into an array, sort it, build the HTML in a string, then inject the string into the select list's innerHTML.

 

But really, server side is the way to go.

Edited by monkeysaurus
Posted

hi

i like this code

 

<?php

$options = array('John', 'Eric', 'Thelma'); // and 97 others
sort($options);

?>

<?php foreach($options as $k=>$v){ ?>
<?php echo $v?>
<?php } ?>

 

but forexaple if i have

 

Goolge

 

how can i write the values in PHP

 

as you wrote:

 

$options = array('John', 'Eric', 'Thelma'); // and 97 others
sort($options);

 

if i change this to

 

$options = array('Google', 'Yahoo', 'Hotmail'); // and 97 others
sort($options);

 

where can i write it links,

for exampel, for the above code:

 

http://www.google.com

http://www.yahoo.com

http://www.hotmail.com

 

thanks

Posted

Use an associative array, and asort instead of sort:

 

<?php

$options = array('http://google.com'=>'Google', 'http://www.yahoo.com'=>'Yahoo', 'http://www.hotmail.com'=>'Hotmail'); 
asort($options);

?>

<?php foreach($options as $k=>$v){ ?>
<?php echo $v?>
<?php } ?>


Posted (edited)

<?php
$options = array('http://google.com'=>'Google', 'http://www.yahoo.com'=>'Yahoo', 'http://www.hotmail.com'=>'Hotmail');
asort($options);
?>

<?php foreach($options as $k=>$v){ ?>
<?php echo $v?>
<?php } ?>

Edited by fazlionline
Posted

<?php
$options = array('http://google.com'=>'Google', 'http://www.yahoo.com'=>'Yahoo', 'http://www.hotmail.com'=>'Hotmail');
asort($options);
?>

<?php foreach($options as $k=>$v){ ?>
<?php echo $v?>
<?php } ?>

Posted (edited)

What is it doing (or not doing) 'properly'?

 

Works fine for me. Google, then Hotmail, then Yahoo! in a drop-down select box.

 

Did you add a Submit button to the Form?

Edited by jlhaslip

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...