ASP loop

Panda On Smack

Can't get enough of FH
Joined
Dec 22, 2003
Messages
1,030
Hello

I use the while function to loop through numbers all the time as it beats writing loads of line of code if you can have it made on the fly

what i want to know though is can you do it with Letters?

Code:
<%
dim x
x = 0
while x < 10
%>
use x here etc
<%
x = x + 1
wend
%>

i want to do this with letters, any ideas?

Thanks
 

Jonty

Fledgling Freddie
Joined
Dec 22, 2003
Messages
1,411
Hi Panda

Literally the first thing that springs to mind is to create an array with the letters of the alphabet in it. You then continue to use numbers, but these numbers refer to the position in the array. Like I say, this was just the first thing that sprung up mind, so it may be a terrible way of doing it, I don't know (plus I'm not too au fait with ASP).

Kind Regards
 

Jonty

Fledgling Freddie
Joined
Dec 22, 2003
Messages
1,411
In PHP, for what it's worth, you would have something like

Code:
<?php
# Define the alphabet array
$Alphabet = array("a","b"..."z");

# Define the pointer
$i = 0;

# Print the first ten letters of the alphabet
while ($i < 10)
{
  # Print the letter
  echo $Alphabet[i];
  # Increment the pointer
  $i++;
}
?>
There are lots of ways of doing it, and as I say I know ASP syntax is different, but I hope it helps in some small way.

Kind Regards
 

Doh_boy

Part of the furniture
Joined
Dec 22, 2003
Messages
1,007
Does asp have a function that returns the character for a given ascii number? That's how I would do it, count up (or down the numbers) then call the function with the number. the number go from 60 upwards I think. Lower case then upper case. I'm not sure if this is better than jonty's way but you don't need an array. Which I'm sure is preferable. :)
 

Panda On Smack

Can't get enough of FH
Joined
Dec 22, 2003
Messages
1,030
Top banana

Thanks for the pointers lads. I used the chr() solution in the end

works a treat

Code:
<%
dim y
y = 97
while y < 123
%>
<a href='#' class='login' onClick="document.form.Username.value += '<%= chr(y) %>';"><%= chr(y) %></a><%
if y = 105 then response.Write("&nbsp;<br>") end if
if y = 114 then response.Write("<br>") end if
y = y + 1
wend
%>

Thanks for your suggestions as well Jonty

:)

Im doing this for a little login 'pad' for a site. instead of typing their username and password in the 2 text boxes they just click the relevant letters and numbers to populate the boxes instead.
 

Jonty

Fledgling Freddie
Joined
Dec 22, 2003
Messages
1,411
Ah, well done guys, that's a better solution :D

Kind Regards
 

Xavier

Can't get enough of FH
Joined
Dec 22, 2003
Messages
1,542
It's at this moment someone, in this case I, step in and tell you all off for the inefficiency of <% blah %><% blah %><% blah %><% blah %><% blah %><% blah %> :p
 

Xavier

Can't get enough of FH
Joined
Dec 22, 2003
Messages
1,542
no I didn't - it's not just bad practice when using '=' (which is incidentally called response.write ;)) - you should never leap in and out of ASP like that for any reason. If you need to write out bits of html between your ASP generated code use the ampersand to concatenate the line and encapsulate the raw HTML in double quotes - for tricky characters such as the double-quote, just write it out manually using the ascii character.

Oh, and using aliases such as '=' is pretty bad practice either. Just as the server has to order process each <%%> set, it spends that little extra converting = to response.write.

In general, even the most complex ASP page shouldn't need to include more than 5 modules of code. Some really complex HTML pages don't need a single line of true HTML at all.
 

Padwah

Fledgling Freddie
Joined
Dec 25, 2003
Messages
127
ah I though you were trying to show how using <%= rather than response.write in a block of code was more inefficient not how jumping in and out of ASP is inefficient.
 

[UKLans]Khan

Fledgling Freddie
Joined
Dec 23, 2003
Messages
52
Code:
<%
dim y
dim strTMP

y = 97
while y < 123

strTMP = strTMP & "<a href='#' class='login' onClick='document.form.Username.value += '" & chr(y) & "';'>" & chr(y) & "</a>"
if y = 105 then 
strTMP = strTMP & "nbsp;<br />"
end if
if y = 114 then 
strTMP = strTMP & "<br />"
end if
y = y + 1
wend
%>
<%=strTMP%>

How about that :D

Khan
 

Panda On Smack

Can't get enough of FH
Joined
Dec 22, 2003
Messages
1,030
you left and re-entered asp mode on the last line, should be:

Code:
<%
dim y
dim strTMP

y = 97
while y < 123

strTMP = strTMP & "<a href='#' class='login' onClick='document.form.Username.value += '" & chr(y) & "';'>" & chr(y) & "</a>"
if y = 105 then 
strTMP = strTMP & "nbsp;<br />"
end if
if y = 114 then 
strTMP = strTMP & "<br />"
end if
y = y + 1
wend

response.write(strTMP)
%>

i did it differently though :)
 

Users who are viewing this thread

Top Bottom