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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

looping through two lists

Status
Not open for further replies.

Leakyradiator

Technical User
Jul 12, 2002
35
0
0
US
As the result of using a couple of forms, I end up with the following lists being passed to the action page:
category list
147
101
102
103
145

and

subcategory list
147117
145114

the first three digits of the subcategory are the same as the category in which they reside, i.e., subcategory 147117 belongs to category 147. Not all categories have subcategories. (All subcategories belong to a category)

I want to be able to determine if the category has a subcategory. If not, I want to be able to insert the category into the database. If it does have a subcategory, I want to be able to put the category and the last three digits of the subcategory into the database.

Any help with how to do this is greatly appreciated.

 
Are they always three digits or six digits? I doubt it somehow but, its a worthy question...

If they are... This should work...

Code:
<cfset clist="147,101,102,103,145">
<cfset slist="147117,145114">

<cfloop list="#slist#" index="i">
  <cfif listfind(clist,left(i,3))>
    <cfset clist=listdeleteat(clist,left(i,3))>
  </cfif>
</cfloop>

<cfset sclist = listappend(slist,clist)>

<cfloop list="#sclist#" index="i">
  <cfif len(i) eq 6>
    <cfquery...>
      insert into tbl(catid,subcatid)
      values('#left(i,3)#','#right(i,3)#')
   </cfquery>
</cfloop>

would be one manner but I'm not sure that's what you want.

Code:
<cfset clist="147,101,102,103,145">
<cfset slist="147117,145114">

<cfset sclist = listappend(clist,slist)>

<cfset inslist = "">
<cfloop list="#sclist#" index="i">
  <cfif not listfind(inslist,i)>
    <cfset inslist = listappend(inslist,i)>
    <cfif not listfind(inslist,left(i,3))>
      <cfquery...>
        insert into cats(catid)
        values('#left(i,3)#')
      </cfquery>
      <cfset inslist = listappend(inslist,left(i,3))>
    </cfif>
    <cfquery...>
      insert into subcats(catid,subcatid)
      values('#left(i,3)#','#right(i,3)#')
    </cfquery>
  </cfif>
</cfloop>

That would seem like it would work...

ALFII.com
---------------------
If this post answered or helped to answer your question, please reply with such so that forum members with a similar question will know to use this advice.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top