Reputation: 2240
Creating a birthday club.
The database consists of clients and their pets. Some clients may only have 1 pet whereas others may have 10. Clients
are stored in one table and Pets
in another table with client_id
as the primary and foreign key accordingly.
The web page will show hundreds of clients and their pets but we'll focus on one dummy record named John Doe. Now, lets say John Doe has a dog, a horse, and a cat. The dog and cat have birthdays in January and the horse in March. When I view the web page, I can see John's cat and dog with the input box checked but not for the horse (since his birthday is in March and not January). I click the "proceed" button and an email will be generated to John wishing his dog and cat a happy birthday but I don't want to include the horse. He'll get another email in March for the horse but not for the cat. With me so far?
Now, my form, I have setup like this:
<cfset ClientPet = ArrayNew(2)>
<cfoutput>
<cfset x = 1>
<cfloop index="p" list="#select_pet#" delimiters=",">
Client ID: #listfirst(p,'|')#<br />
Pet ID: #listlast(p,'|')#
<cfset ArrayAppend(ClientPet[x][1], '#listfirst(p,'|')#')>
<cfset ArrayAppend(ClientPet[x][2], '#listlast(p,'|')#')>
</cfloop>
</cfoutput>
On the action page, I'd like to only send one email to a client and dynamically list their pets but only the pets that were selected from the previous page so I'm doing this:
<cfset ClientPet = ArrayNew(2)>
<cfoutput>
<cfset x = 1>
<cfloop index="p" list="#select_pet#" delimiters=",">
Client ID: #listfirst(p,'|')#<br />
Pet ID: #listlast(p,'|')#
<cfset ArrayAppend(ClientPet[x][1], '#listfirst(p,'|')#')>
<cfset ArrayAppend(ClientPet[x][2], '#listlast(p,'|')#')>
</cfloop>
</cfoutput>
I don't know if I should use Arrays or structures and whichever way I do it, I need to be able to loop over all of the client_id and then all of the pets, then the next client_id and all of their pets, and so on. Am I on the right path above? If so, I should be able to loop over my 2d array or structure and then systematically list the pets accordingly, right?
Upvotes: 1
Views: 208
Reputation: 28873
As Nathan suggested, this might be a better candidate for a scheduled task rather than a manual form.
.. But to address the question about arrays and structures .. it does not sound like you need either one. Just make the checkbox value the petid
value.
<input name="select_pet" type="checkbox" value="#petID#">
When the form is submitted, the field will contain a list of the selected pet id's. Just feed that list into a query to retrieve both the client and pet information.
SELECT c.client_name, p.pet_id, p.pet_name
FROM pet p INNER JOIN clients c ON c.client_id = p.client_id
WHERE p.pet_id IN
(
<cfqueryparam value="#form.select_pet#" list="true" cfsqltype="cf_sql_integer">
)
Upvotes: 1