Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Alphabetical Listing in ASP

Status
Not open for further replies.

blondebier

Programmer
Jun 19, 2003
142
GB
I'm trying to break up a huge list of data from a database into alphabetical segments.

Ideally i'd like to display A B C ... Z at the top of a page and when the user clicks a letter it filters the data alphabetically according to the letter selected.

Are there any articles that would illustrate this?

Thanks for your comments,

Francis.
 
its easy,
you have your links on top.
a b c d e ...
each has a query string in the url. <a href = &quot;orderpage.asp?letter=a>a</a>

the sql in the order page will look like

sql = select * from table where myFld like '&quot;&request.querystring(&quot;letter&quot;)&&quot;%' order by myFld&quot;

A common mistake that people make when trying to design something completely foolproof is to underestimate the ingenuity of complete fools.
- Quote by Douglas Adams
 
Basically the application displays a catalogue of roses.

I've created a loop that generates the index list from ascii characters. My problem is getting it to display the list it's new indexed form. ie a List for when A is selected, List for when B is selected and so on...

My code so far is:

<%@ LANGUAGE = &quot;VBScript&quot; %>
<!-- #include file=&quot;db.asp&quot; -->
<!-- #include file=&quot;config.asp&quot; -->
<!-- #include file=&quot;functions.asp&quot; -->

<%
catcode=request.querystring(&quot;catcode&quot;)

set rscat=db.execute(&quot;select * from categories where catcode=&quot; & catcode)

if rscat.eof then
response.redirect(&quot;error.asp?Unable to locate category!&quot;)
end if

'Selects the names and codes of all the roses in that category
set rscatlist=db.execute(&quot;select name,roseid from roses where catcode=&quot; & catcode & &quot; order by name&quot;)
%>

<html>

<head>
<title>My Rose Catalogue</title>

<body bgcolor=&quot;<%= bgcolor %>&quot; topmargin=&quot;0&quot; leftmargin=&quot;0&quot; marginwidth=&quot;0&quot; marginheight=&quot;0&quot; text=&quot;<%= text %>&quot; alink=&quot;<%= COLlight %>&quot; link=&quot;<%= COLlight %>&quot; vlink=&quot;<%= vlink %>&quot;>
<font face=&quot;tahoma&quot;>
<%
header

categorymenu
%>
<table>
<td valign=top align=right>
<font face=&quot;tahoma&quot; size=&quot;3&quot; color=&quot;<%= COLdark %>&quot;><B><%= rscat(&quot;catname&quot;) %> Roses</B></font>
<%
for num = 97 to 122
lett = &quot;&#&quot; & num & &quot;;&quot;
response.write &quot;<a href=category.asp?catcode=&quot; & catcode & &quot; &letter=&quot; & lett & &quot;> &quot; & lett & &quot;</a>&quot;

next%>
<br>
<%

'Displays a list of the other roses in the category
if not rscatlist.eof then
rscatlist.movefirst
while not rscatlist.eof
%>
<font size=&quot;2&quot; face=&quot;tahoma&quot; color=&quot;<%= COLlight %>&quot;>
<a href=&quot;rose.asp?roseid=<%= rscatlist(&quot;roseid&quot;)%>&quot;><%=rscatlist(&quot;name&quot;)%></a>
<br>
<%
rscatlist.movenext
wend
end if
%>
</td>
<%
ci=rscat(&quot;catimage&quot;)
if ci<>&quot;&quot; then
%>
<td width=180 align=&quot;left&quot;>
<%=rscat(&quot;catimage&quot;) %>
<%
else
%>
<td width=300 align=&quot;center&quot; height=250 valign=center>
<font face=&quot;tahoma&quot; size=&quot;9&quot;>
<b><%=lcase(rscat(&quot;catname&quot;)) %></b><br><br>
<font face=&quot;tahoma&quot; size=&quot;3&quot;>
<b><%=lcase(rscat(&quot;catdes&quot;)) %>
<%
end if
%>

</td>
</table>

<% footer %>
</body>
</html>
<%
db.close
set db=nothing
%>

At the moment the output is something like:

CatName a b c d e f g h i j k l m n o p q r s t u v w x y z

Then the complete list
.
.
.
.
.
.
.
.
.
.

Thanks for your help.


 
What database you using SQL or Acces?

________
George, M
 
Add this
...
catcode=request.querystring(&quot;catcode&quot;)
letter=request.querystring(&quot;letter&quot;)
...
'Selects the names and codes of all the roses in that category
set rscatlist=db.execute(&quot;select name,roseid from roses where catcode=&quot; & catcode & &quot; and name like '&quot;&letter&&quot;%' order by name&quot;)


This should fix it

________
George, M
 
Would I remove this section of the code as well?

<%
'Displays a list of the other roses in the category
if not rscatlist.eof then
rscatlist.movefirst
while not rscatlist.eof
%>
<font size=&quot;2&quot; face=&quot;tahoma&quot; color=&quot;<%= COLlight %>&quot;>
<a href=&quot;rose.asp?roseid=<%= rscatlist(&quot;roseid&quot;)%>&quot;><%=rscatlist(&quot;name&quot;)%></a>
<br>
<%
rscatlist.movenext
wend
end if
%>
 
No only modify those lines i showed you, it should work with the code you have now

________
George, M
 
I modified those two lines but it still lists the entire list from the &quot;name&quot; field in my database.

Thanks for your help.
 
Try to
response.write(&quot;select name,roseid from roses where catcode=&quot; & catcode & &quot; and name like '&quot;&letter&&quot;%' order by name&quot;)
and see if the querry is ok

________
George, M
 
I get this output:

Bush Roses Aselect name,roseid from roses where catcode=1 and name like '%' order by name Bselect name,roseid from roses where catcode=1 and name like '%' order by name Cselect name,roseid from roses where catcode=1 and name like '%' order by name .........and so on.
 
yes that's ok, but like '%' it means that letter is empty instead of having a letter value.
do you have these lines?

letter=request.querystring(&quot;letter&quot;)

response.write &quot;<a href=category.asp?catcode=&quot; & catcode & &quot; &letter=&quot; & lett & &quot;> &quot; & lett & &quot;</a>&quot;

set rscatlist=db.execute(&quot;select name,roseid from roses where catcode=&quot; & catcode & &quot; and name like '&quot;&letter&&quot;%' order by name&quot;)
?


________
George, M
 
This is what it looks like now:

<%@ LANGUAGE = &quot;VBScript&quot; %>
<!-- #include file=&quot;db.asp&quot; -->
<!-- #include file=&quot;config.asp&quot; -->
<!-- #include file=&quot;functions.asp&quot; -->

<%
catcode=request.querystring(&quot;catcode&quot;)
letter=request.querystring(&quot;letter&quot;)

set rscat=db.execute(&quot;select * from categories where catcode=&quot; & catcode)

if rscat.eof then
response.redirect(&quot;error.asp?Unable to locate category!&quot;)
end if

'Selects the names and codes of all products in that category
set rscatlist=db.execute(&quot;select name,roseid from roses where catcode=&quot; & catcode & &quot; and name like '&quot;&letter&&quot;%' order by name&quot;)
%>

<html>

<head>
<title>My Rose Catalogue</title>

<body bgcolor=&quot;<%= bgcolor %>&quot; topmargin=&quot;0&quot; leftmargin=&quot;0&quot; marginwidth=&quot;0&quot; marginheight=&quot;0&quot; text=&quot;<%= text %>&quot; alink=&quot;<%= COLlight %>&quot; link=&quot;<%= COLlight %>&quot; vlink=&quot;<%= vlink %>&quot;>
<font face=&quot;tahoma&quot;>
<%
header

categorymenu
%>
<table>
<td valign=top align=right>
<font face=&quot;tahoma&quot; size=&quot;3&quot; color=&quot;<%= COLdark %>&quot;><B><%= rscat(&quot;catname&quot;) %> Roses</B></font>
<%
for num = 65 to 90
lett = &quot;&#&quot; & num & &quot;;&quot;
response.write &quot;<a href=category.asp?catcode=&quot; & catcode & &quot; &letter=&quot; & lett & &quot;> &quot; & lett & &quot;</a>&quot;
response.write(&quot;select name,roseid from roses where catcode=&quot; & catcode & &quot; and name like '&quot;&letter&&quot;%' order by name&quot;)

next%>
<br>
<%

'Displays a list of the other roses in the category
if not rscatlist.eof then
rscatlist.movefirst
while not rscatlist.eof
%>
<font size=&quot;2&quot; face=&quot;tahoma&quot; color=&quot;<%= COLlight %>&quot;>
<a href=&quot;rose.asp?roseid=<%= rscatlist(&quot;roseid&quot;)%>&quot;><%=rscatlist(&quot;name&quot;)%></a>
<br>
<%
rscatlist.movenext
wend
end if
%>
</td>
<%
ci=rscat(&quot;catimage&quot;)
if ci<>&quot;&quot; then
%>
<td width=180 align=&quot;left&quot;>
<%=rscat(&quot;catimage&quot;) %>
<%
else
%>
<td width=300 align=&quot;center&quot; height=250 valign=center>
<font face=&quot;tahoma&quot; size=&quot;9&quot;>
<b><%=lcase(rscat(&quot;catname&quot;)) %></b><br><br>
<font face=&quot;tahoma&quot; size=&quot;3&quot;>
<b><%=lcase(rscat(&quot;catdes&quot;)) %>
<%
end if
%>

</td>
</table>

<% footer %>

</body>
</html>

<%
db.close
set db=nothing
%>
 
And does it work? the query it seems ok unless the letter as i see it's empty so will return all the fields available.

________
George, M
 
No it doesn't work. It's very frustrating.

I think either; it doesn't wait for a letter to be passed and returns all the values in the database. Or it loops in some way and gets all the letters passed to it and returns all of the values?

It's most puzzling?



 
Put the following function in your global header (or the top of your page)
Function view()
echo &quot;#- debug message -#&quot; & &quot;<br>&quot;
echo &quot;#- debug queryString -#&quot; & &quot;<br>&quot;
For Each Name In Request.QueryString
echo Name & &quot;:&quot; & GetVar(Name) & &quot;<br>&quot;
Next
echo &quot;#- debug form -#&quot; & &quot;<br>&quot;
For Each Name In Request.Form
echo Name & &quot;:&quot; & GetVar(Name) & &quot;<br>&quot;
Next
echo &quot;#- debug end -#&quot; & &quot;<br>&quot;
Response.end
End Function

then call view() at the top of this page. It is a useful little function that shows you all the inputs your page is recieving. If it isn't recieving a letter, the query above won't limit. If it is then we have more work to do.
 
I've changed my code and removed that loop. It looks like this, but now I get this syntax error.

This page cannot be displayed.

Error Type:
Microsoft OLE DB Provider for ODBC Drivers (0x80040E14)
[Microsoft][ODBC Microsoft Access Driver] Syntax error (missing operator) in query expression 'catcode='.
/public/category.asp, line 10


<%@ LANGUAGE = &quot;VBScript&quot; %>
<!-- #include file=&quot;db.asp&quot; -->
<!-- #include file=&quot;config.asp&quot; -->
<!-- #include file=&quot;functions.asp&quot; -->

<%
catcode=request.querystring(&quot;catcode&quot;)
letter=request.querystring(&quot;letter&quot;)

set rscat=db.execute(&quot;select * from categories where catcode=&quot; & catcode)

if rscat.eof then
response.redirect(&quot;error.asp?Unable to locate category!&quot;)
end if

'Selects the names and codes of all products in that category
set rscatlist=db.execute(&quot;select name,roseid from roses where catcode=&quot; & catcode & &quot; and name like '&quot;&letter&&quot;%' order by name&quot;)
%>

<html>

<head>
<title>My Rose Catalogue</title>

<body bgcolor=&quot;<%= bgcolor %>&quot; topmargin=&quot;0&quot; leftmargin=&quot;0&quot; marginwidth=&quot;0&quot; marginheight=&quot;0&quot; text=&quot;<%= text %>&quot; alink=&quot;<%= COLlight %>&quot; link=&quot;<%= COLlight %>&quot; vlink=&quot;<%= vlink %>&quot;>
<font face=&quot;tahoma&quot;>
<%
header

categorymenu
%>

<table>
<td valign=top align=right>
<font face=&quot;tahoma&quot; size=&quot;3&quot; color=&quot;<%= COLdark %>&quot;><B><%= rscat(&quot;catname&quot;) %> Roses</B></font>

<font SIZE=&quot;4&quot;> <a href=&quot;category.asp?catcode= &quot; & catcode & &quot; &letter=A&quot;>A</a> 
<a href=&quot;category.asp?catcode= &quot; & catcode & &quot; &letter=B&quot;>B</a> 
<a href=&quot;category.asp?catcode= &quot; & catcode & &quot; &letter=C&quot;>C</a> 
<a href=&quot;category.asp?catcode= &quot; & catcode & &quot; &letter=D&quot;>D</a> 
<a href=&quot;category.asp?catcode= &quot; & catcode & &quot; &letter=E&quot;>E</a> 
<a href=&quot;category.asp?catcode= &quot; & catcode & &quot; &letter=F&quot;>F</a> 
<a href=&quot;category.asp?catcode= &quot; & catcode & &quot; &letter=G&quot;>G</a> 
<a href=&quot;category.asp?catcode= &quot; & catcode & &quot; &letter=H&quot;>H</a> 
<a href=&quot;category.asp?catcode= &quot; & catcode & &quot; &letter=I&quot;>I</a> 
<a href=&quot;category.asp?catcode= &quot; & catcode & &quot; &letter=J&quot;>J</a> 
<a href=&quot;category.asp?catcode= &quot; & catcode & &quot; &letter=K&quot;>K</a> 
<a href=&quot;category.asp?catcode= &quot; & catcode & &quot; &letter=L&quot;>L</a> 
<a href=&quot;category.asp?catcode= &quot; & catcode & &quot; &letter=M&quot;>M</a> 
<a href=&quot;category.asp?catcode= &quot; & catcode & &quot; &letter=N&quot;>N</a> 
<a href=&quot;category.asp?catcode= &quot; & catcode & &quot; &letter=O&quot;>O</a> 
<a href=&quot;category.asp?catcode= &quot; & catcode & &quot; &letter=P&quot;>P</a> 
<a href=&quot;category.asp?catcode= &quot; & catcode & &quot; &letter=Q&quot;>Q</a> 
<a href=&quot;category.asp?catcode= &quot; & catcode & &quot; &letter=R&quot;>R</a> 
<a href=&quot;category.asp?catcode= &quot; & catcode & &quot; &letter=S&quot;>S</a> 
<a href=&quot;category.asp?catcode= &quot; & catcode & &quot; &letter=T&quot;>T</a> 
<a href=&quot;category.asp?catcode= &quot; & catcode & &quot; &letter=U&quot;>U</a> 
<a href=&quot;category.asp?catcode= &quot; & catcode & &quot; &letter=V&quot;>V</a> 
<a href=&quot;category.asp?catcode= &quot; & catcode & &quot; &letter=W&quot;>W</a> 
<a href=&quot;category.asp?catcode= &quot; & catcode & &quot; &letter=X&quot;>X</a> 
<a href=&quot;category.asp?catcode= &quot; & catcode & &quot; &letter=Y&quot;>Y</a> 
<a href=&quot;category.asp?catcode= &quot; & catcode & &quot; &letter=Z&quot;>Z</a> <br>
<br>

<%

'Displays a list of the other roses in the category
if not rscatlist.eof then
rscatlist.movefirst
while not rscatlist.eof
%>
<font size=&quot;2&quot; face=&quot;tahoma&quot; color=&quot;<%= COLlight %>&quot;>
<a href=&quot;rose.asp?roseid=<%= rscatlist(&quot;roseid&quot;)%>&quot;><%=rscatlist(&quot;name&quot;)%></a>
<br>
<%
rscatlist.movenext
wend
end if
%>
</td>
<%
ci=rscat(&quot;catimage&quot;)
if ci<>&quot;&quot; then
%>
<td width=180 align=&quot;left&quot;>
<%=rscat(&quot;catimage&quot;) %>
<%
else
%>
<td width=300 align=&quot;center&quot; height=250 valign=center>
<font face=&quot;tahoma&quot; size=&quot;9&quot;>
<b><%=lcase(rscat(&quot;catname&quot;)) %></b><br><br>
<font face=&quot;tahoma&quot; size=&quot;3&quot;>
<b><%=lcase(rscat(&quot;catdes&quot;)) %>
<%
end if
%>

</td>
</table>

<% footer %>

</body>
</html>

<%
db.close
set db=nothing
%>

 
as you may see you need to pass catcode and letter as querry string, since you dont pass any catcode anywere it wont work.

________
George, M
 
I found the problem.

lett = &quot;&#&quot; & num & &quot;;&quot;

Should've been written as:

lett = &quot;&#0&quot; & num & &quot;;&quot;


Little things hey?


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top