On Thu, 25 Jul 2002 22:37:05 +0300 Hytham Shehab <[EMAIL PROTECTED]> wrote:

> here is a code snippet:
> 
> $locationh = $dbh->prepare_cached("select id,name from location where
> id<100
> order by id");
> $locationh ->execute();
> while (@row=$locationh->fetchrow_array) {
> $loc{$row[0]} = $row[1];
> }
> now, when i get the %loc and print it as a key,value pairs, it gives me
> the
> wrong order:
> 
> <option value=2>zone2</option>
> <option value=1>zone2</option>
> 
> which is not what i want, what i want is
> 
> <option value=1>zone1</option>
> 
> <option value=2>zone2</option>
> 
> thats it, i hope this could make it more clarified.

As soon as you store the results in a hash (%loc) you lose the ordering. 
Either print the options immediately or save them in an array.  If you
_must_ have the key/value pairs in a hash, create it as well.

my ( @row, @loc, %loc );
while ( @row = $locationh -> fetchrow_array ) {
   $loc{$row[0]} = $row[1]; # Save for later
   print "<option value=$row[0]>$row[1]</option>\n" if $creating_the_page;
   # or store in an array to print later: push @loc, [ @row[0,1] ];
}

This is basic Perl.  Run 'perldoc perlfaq4' and look for 'hashes' (without
the quotes).
-- 
Mac :})
** I normally forward private questions to the appropriate mail list. **
Ask Smarter: http://www.tuxedo.org/~esr/faqs/smart-questions.html
Give a hobbit a fish and he eats fish for a day.
Give a hobbit a ring and he eats fish for an age.


Reply via email to