'*********************************************************************** ' DISPLAYED WITH PERMISSION OF ROY SCOTT ENTERPRISES ' AN ARCHIVING FUNCTION ' http://www.scottserver.net/forum/index.php ' ALL RIGHTS RESERVED '************************************************************************ ' MINOR EDITING PERFORMED BY UPLA STAFF '************************************************************************ 'smoses 'Microdeveloper ' Posted: Tue Jan 20, 2004 2:34 pm Post subject: fun with hash arrays '-------------------------------------------------------------------------------- 'First, welcome to the Shorthand board. 'Most of you probably haven't seen Shorthand code before, so I thought I would post some. 'Awhile back, I wrote a shopping cart in Shorthand. 'My favorite part of it was the "also bought" feature. It was the most fun to program. So I thought I 'would share it with you guys. 'Enjoy. 'Code: '<~ 'alsobought.shl include "common.shh" 'creating the recordset ors = New RecordSet(conn, "SELECT orderid, productId FROM orderdetails where productid='"&q("p")&"'") ors.execute() 'this is my prefered way of checking a recordset, there are other ways to do it. if ors.count >0 then ors.Next() 'moving on i = 0 'dumping recordset into a hash array. 'this is the fun part. :) While not ors.eof x{"ORS"&i} = ors.orderid ors.Next() i=i+1 End While Foreach x as key=>value 'stringafying it. g = g & " orderid = '" & value & "' OR " End for 'determining the length of the leng variable. leng = length(g)-3 ' dynamically building a second recordset based on the information from the hash table. dynostring = substring(g, 0, leng) sqlstring = "select * from orderdetails where " & dynostring ors2 = New RecordSet(conn, sqlstring) ors2.Execute() ors2.Next() i=0 While not ors2.eof y{ORS&i} = ors2.productid i=i+1 ors2.Next() End While Foreach y as key=>value 'checking to make sure youre not recomending the product youre looking at... if value <> q("p") then 'creating a second recordset h = h & " Productid = "& value &" OR " End if End for ' same thing here. drop the length to get rid of the gunk. lenh = length(h)-3 'creating the more polished string dynostring2 = substring(h, 0, lenh) sqlstring2 = "select * from products where " & dynostring2 & " order by modelname ASC" ors3 = New RecordSet(conn, sqlstring2) ors3.Execute() ors3.Next() 'heres the other way to do it. if not ors3.eof then print "People who bought this item also purchased:
" While not ors3.eof dynolink = "" & ors3.modelname &"
" print dynolink ors3.Next() End While End if Else print "nobody has bought this item" End if '~> 'Best Regards, 'Sam Moses