Click to See Complete Forum and Search --> : Dropdown menu and dynamic values
GurusGuru
08-24-2004, 10:18 AM
How can the number of records in a particular option be shown in a drop down menu. The values are obtained from a flat text file. Example www.batchmates.com. When one sees the year wise list, he gets to see how many users are registered for that particular year.
<option value="1919">1919(1)</option>
<option value="1947">1947(2)</option>
<option value="1951">1951(1)</option>
<option value="1961">1961(2)</option>
<option value="1963">1963(3)</option>
<option value="1964">1964(3)</option>
The values in brackets () are obtained from a .dat file.
chrisranjana
08-25-2004, 05:56 AM
YOu need to use PERL and Mysql
and maybe a COUNT(*) sql statement
silent11
08-25-2004, 09:06 AM
what does the text file look like?
silent11
08-25-2004, 09:18 AM
assuming the text file is comma seperated you could so something like this...
use strict;
for (<DATA>){
chomp((my $year, my $num) = split '\,',$_);
print qq`<option value="$year">$year($num)</option>\n`;
}
__DATA__
1991,7
1992,3
1993,20
1994,90
I'm using "DATA" here to represent a text file. You would need to open a file then read it line by line with a for loop as I have here.
Then split on the delimiter, here it's a comma, just replace the comma with what ever it is you are using.
Then print it out.
I chomp()ed the variables to remove the '\n' from the number.
OUTPUT:
D:\>option.pl
<option value="1991">1991(7)</option>
<option value="1992">1992(3)</option>
<option value="1993">1993(20)</option>
<option value="1994">1994(90)</option>
GurusGuru
08-27-2004, 03:14 AM
The data base looks like as below and resides in a cgi-bin
Mr::Mark::Hull::1967::Nyc::USA
Mr::Bill::Todd::1972::Melbourne::Australia
Mr::Rob::Wilson::1967::LA::USA
Mr::Bryan::Gray::1971::Chicago::USA
Mr::Peter::Hudson::1977::Washington::USA
Mr::Bryan::Strange::1972::Baltimore::USA
The result should be:
<option value="1967">1967(2)</option>
<option value="1971">1971(1)</option>
<option value="1972">1972(2)</option>
<option value="1977">1977(1)</option>
silent11
08-27-2004, 07:25 AM
schweeeet! I actually got it to work :D I'm not really good with hashes, but I got it to work first try ( and that never happens ).
use strict;
my @line;
my %years;
for (<DATA>){
chomp( @line = split '::',$_);
$years{$line[3]}++;
}
for (sort keys %years){
print qq`<option value="$_">$_($years{$_})</option>\n`;
}
__DATA__
Mr::Mark::Hull::1967::Nyc::USA
Mr::Bill::Todd::1972::Melbourne::Australia
Mr::Rob::Wilson::1967::LA::USA
Mr::Bryan::Gray::1971::Chicago::USA
Mr::Peter::Hudson::1977::Washington::USA
Mr::Bryan::Strange::1972::Baltimore::USA
you'll want to use something like this... just replace './path/to/file.txt' with the actual path to your file with the data in it.
#!/usr/bin/perl
# -silent11
# just change file path
use strict;
my $file = './path/to/file.txt';
my @line;
my %years;
open(FILE,$file) || die $!;
for (<FILE>){
chomp( @line = split '::',$_);
$years{$line[3]}++;
}
for (sort keys %years){
print qq`<option value="$_">$_($years{$_})</option>\n`;
}
GurusGuru
08-27-2004, 09:30 AM
Was getting an error. So to check the error I added the following lines.
BEGIN {
$| = 1;
open (STDERR, ">&STDOUT");
print qq~Content-type: text/html\n\n~;
}
Finally got the results like this -
1933(1) 1936(2) 1939(1) 1940(1) 1941(1) 1943(1) 1944(1) 1945(2) 1946(2) 1947(5) 1948(2) 1950(1) 1951(2) 1953(2) 1954(1) 1955(4) 1956(1)
How to get it in drop down menu in a static html page?
silent11
08-27-2004, 09:47 AM
That script will spit out an error if you are calling it from a browser since it doesnt print a header. To do so do this:
#!/usr/bin/perl
# -silent11
# just change file path
use strict;
use CGI ':standard';
print header;
my $file = './path/to/file.txt';
my @line;
my %years;
open(FILE,$file) || die $!;
for (<FILE> ){
chomp( @line = split '::',$_);
$years{$line[3]}++;
}
for (sort keys %years){
print qq`<option value="$_">$_($years{$_})</option>\n`;
}
to get this output into a static file you can run this script, view the source then paste it into the static file. The problem here is that you have dynamic data and a static file. Perhaps you can convert this page to a dynamic one.
You could also just run the script from your desktop like so: perl thisScript.pl > output.html
then get the html code it produced out of output.html