Link to home
Start Free TrialLog in
Avatar of fbouzek
fbouzek

asked on

ColdFusion, CFLoop, indented list from query

Trying to make an indented list from a query, There is a page Hierarchy column in the database

id                                  pageHierarchy
1                                           1
2                                         1~2
3                                           3
4                                           4
5                                         4~5
6                                       4~5~6
7                                     4~5~6~7
8                                          8


Indented list:

1
   2
3
4
   5
      6
        7
8

What would my query and output look like?
Avatar of dbanttari
dbanttari
Flag of United States of America image

Treat each value as a "~"-delimited list, and use CF's handy list functions.

query: select pageHierarchy from pages order by id

code:
<cfoutput query="q">
  #repeatString("&nbsp;", listLen(pageHierarchy,"~")-1)##listLast(pageHierarchy,"~")#
</cfoutput>

Open in new window

Brilliant solution by dbanttari. Just add "<br />" to the end of the variable line.
Avatar of fbouzek
fbouzek

ASKER

I was working with this, but couldn't figure out where to the list tags for  I can get the list, just cannot indent them
Avatar of fbouzek

ASKER

Well, that was clear as mud. let me try again.
I was working with this, but couldn't figure out where or how to put the list tags so it would indent the list for each level
ASKER CERTIFIED SOLUTION
Avatar of dbanttari
dbanttari
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Code indents the list with the output of each row being caused by the <Cfoutput> tag with the QUERY value. See attached code using dbanttari's solution with a test query created.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 
<!--- get data from table --->
<!--- <cfquery datasource="datasource" name="getHierarchy">
	SELECT id, pageHierarchy
	FROM hTable
	ORDER BY Id
</cfquery> --->
 
<!--- test query --->
<cfset getHierarchy= querynew("")>
<cfset queryaddcolumn  (getHierarchy, "id", "CF_SQL_integer", ListToArray("1,2,3,4,5,6,7,8"))>
<cfset queryaddcolumn  (getHierarchy, "pageHierarchy", "cf_sql_varchar", ListToArray("1,1~2,3,4,4~5,4~5~6,4~5~6~7,8"))> 
 
 
<html>
<head>
	<title>Untitled</title>
</head>
 
<body>
 
<cfoutput query="getHierarchy">
  #repeatString("&nbsp;&nbsp;&nbsp;", listLen(pageHierarchy,"~")-1)##listLast(pageHierarchy,"~")#<br>
</cfoutput>
 
</body>
</html>

Open in new window