We help IT Professionals succeed at work.

Coldfusion memu

erikTsomik
erikTsomik asked
on
High Priority
144 Views
Last Modified: 2020-03-03
I am trying to create a menu from API and Coldfusion. I have DeserializeJSON result and got the nested struct of array.

The name of menu Items is called name. the parent menu may have submenu the sumbenus are in cabcategories

The sample structure is attached

The code is here:

<cfset myArrayObj = obj.categories>
	
	<ul>
	<cfloop from="1" to="#ArrayLen(myArrayObj)#" index="i">
			<li class="all-product-content-menu">
				#i#
				<span>
					<a id="tst_productMenu_#myArrayObj[i].id#" href="#myArrayObj[i].path#" onclick="insite.nav.hideMenu();">#myArrayObj[i].name#</a>
					<cfif ArrayLen(myArrayObj[i].subCategories)>
						<cfset objSubcategories = myArrayObj[i].subCategories>
						<!---<cfdump var="#objSubcategories#" >--->
						<ul>
							<cfloop from="1" to="#ArrayLen(objSubcategories)#" index="ii">
								<li><a id="tst_productMenu_#objSubcategories[ii].id#" href="#objSubcategories[ii].path#" onclick="insite.nav.hideMenu();">#objSubcategories[ii].name#</a></li>
								
								<cfif isStruct(objSubcategories[ii].subCategories)>
									11
								<cfelse>
								dd	
								</cfif>	
								
							</cfloop>
						</ul>
					</cfif>
				</span>
				
         		
         	</li>

	</cfloop>

Open in new window

Comment
Watch Question

Charlie ArehartTroubleshooting Consultant
CERTIFIED EXPERT

Commented:
What is the question or problem?
erikTsomikSystem Architect, CF programmer

Author

Commented:
i cant access  subcategories in structures
Charlie ArehartTroubleshooting Consultant
CERTIFIED EXPERT

Commented:
Sorry, still not enough for me.. Perhaps others will see through to your issue.

It would help to offer whatever code you have that does process this form, and show what you can and cannot successfully access.

You may be thinking,"well can't you do that yourself?", but consider that some of us see these on our mobile devices and so can't readily copy the code and work up an example.

More important, by your showing what you are or are not doing, or are or are not seeing as a result, even we who can't readily run some code may still be able to see what's amiss for you and make a suggestion.

Just trying to help.
CERTIFIED EXPERT
Most Valuable Expert 2015

Commented:
... also, you forgot to attach the sample structure

The sample structure is attached
erikTsomikSystem Architect, CF programmer

Author

Commented:
sorry here is the screen shot screen shot
Here is the code I got so far and it breaks on the fours array

<cfset obj =DeserializeJSON(result.FileContent)>
	


	<cfset myArrayObj = obj.categories>
	
	<ul>
		#ArrayLen(myArrayObj)#<br>
	<cfloop from="1" to="#ArrayLen(myArrayObj)#" index="i">
		#i#
			<li class="all-product-content-menu">
				<span class="nav-item-container all-products">
					<a id="tst_productMenu_#myArrayObj[i].id#" href="https://www.testequity.com#myArrayObj[i].path#" onclick="insite.nav.hideMenu();">#myArrayObj[i].name#</a>
					 <label style="display:block;line-height: 28px;" for="nav-#myArrayObj[i].id#" class="toggle-sub" onclick="insite.nav.goToSubnav(this)">&##9658;</label>
				</span>	
				
					<cfif ArrayLen(myArrayObj[i].subCategories)>
						b:#i#
						<cfset objSubcategories = myArrayObj[i].subCategories>
						<!---<cfdump var="#objSubcategories#" >--->
						<ul id="sub-#objSubcategories[i].id#" class="subnav sub-tier-panel">
							 <li class="sub-heading"></li>
							<cfloop from="1" to="#ArrayLen(objSubcategories)#" index="ii">
								ii:#ii#
								<li class="all-product-content-menu">
									<a id="tst_productMenu_#objSubcategories[ii].id#" href="https://www.testequity.com#objSubcategories[ii].path#" onclick="insite.nav.hideMenu();">#objSubcategories[ii].name#</a>
								</li>
								
								<cfif isArray(objSubcategories[ii].subCategories)>
									<cfset objSubcategories2 = objSubcategories[ii].subCategories>
									<ul>
										<cfloop from="1" to="#ArrayLen(objSubcategories2)#" index="iii">
											<li><a id="tst_productMenu_#objSubcategories2[iii].id#" href="https://www.testequity.com#objSubcategories2[iii].path#" onclick="insite.nav.hideMenu();">#objSubcategories2[iii].name#</a></li>
										</cfloop>
									</ul>
								
								</cfif>	
								
							</cfloop>
						</ul>
					</cfif>
				
				<!---<span class="nav-item-container all-products">
					<a id="tst_productMenu_#myArrayObj[i].id#" href="#myArrayObj[i].path#" onclick="insite.nav.hideMenu();">#myArrayObj[i].name#</a>
					<!---<label style="display:block;line-height: 28px;" for="nav-#myArrayObj[i].id#" class="toggle-sub" onclick="insite.nav.goToSubnav(this)">&#9658;</label>--->
				</span>--->
         		 <!---<input type="checkbox" name="nav" id="nav-#myArrayObj[i].id#" class="subnav-check" />
         		<ul id="sub-#myArrayObj[i].id#" class="subnav sub-tier-panel">
         			<li class="sub-heading"></li>
         		</ul>	--->
         	</li>

	</cfloop>
	</ul>

Open in new window

Charlie ArehartTroubleshooting Consultant
CERTIFIED EXPERT

Commented:
Where is it breaking? What line of code? what's the error? It's really difficult for us to try to debug this just be looking at the code. It would be far more helpful if you could create a stand-alone example, where the code included the creation of that array and its elements. At least then we could try to run the code and might experience the error and be able to debug things.

And I see you have some commented out cfdump's, so clearly you have been trying. But since we don't see the output of the cfdump, we can't know where your problem may be. When you're trying to traverse such deeply nested structures, it is indeed easy to get lost along the way, with just one "wrong turn".

Even better would be for you to create that stand-alone example by running it at cffiddle.org. Then, you could give us a link to the code and we too could run it there. You may even find in putting that together that you discover your problem. People often do.

Or again, if someone else can "see through the fog" and spot your problem, all the more power (and credit) to them.
erikTsomikSystem Architect, CF programmer

Author

Commented:
I get this error The element at position 4 of dimension 1,                  of array variable "OBJSUBCATEGORIES," cannot be found.
Charlie ArehartTroubleshooting Consultant
CERTIFIED EXPERT

Commented:
There are 10 references to that array (where you refer to an element within that array) which could be where this error is.

PLEASE, HELP US HELP YOU.

Which one gets the error? What line of code? And then tell us what line that is within the snippet you have given us.

Better yet, please consider ALL that I wrote in my last comment. You did not reply to it, other than to offer this one sentence.
erikTsomikSystem Architect, CF programmer

Author

Commented:
it is error out on the 4 element of the main array
CERTIFIED EXPERT
Most Valuable Expert 2015

Commented:
Thanks for the additional info, but a screen shot isn't going to be very useful in this case.  The structure is too complex, and I unfortunately don't have time to try and reproduce that whole string by hand...  

Could you post the sample JSON as text instead? Or better yet, create a runnable snippet like Charlie said: https://cffiddle.org/ or https://trycf.com/ .  That should give us enough info to run the code.  Then we can focus on fixing the error, instead of expending that time on set up code...
CERTIFIED EXPERT
Most Valuable Expert 2015
Commented:

<cfset myArrayObj = obj.categories>
<cfloop from="1" to="#ArrayLen(myArrayObj)#" index="i">
...

Have you considered using "array" loops instead of indexed loops? IMO an array loop makes for more readable code.  The loop automatically populates the "index" (or "item") variable with the current data element.  So there's no need to create extra variables or extract the element with long strings like #myArrayObj[ i ].subcategories[ jj ]#  

https://trycf.com/gist/09267cda2ed6cef3c9f637327ce8eae7/acf2018?theme=monokai
CERTIFIED EXPERT

Commented:
I suggest you that serialize this nested structures and arrays in streamlined flat structure, i had faced this issues tons of time and the best approach i did was created a function which will flatline everything for me using a period operator and i will use it directly until the values build are not very dynamic

let me know if you are willing to flatten its base and i will share the function. my function also has a support for null when doing a deserializejson or serialize

Thanks