This topic is locked

How to limit the number of records users can add

5/9/2012 2:05:41 PM
ASPRunnerPro Tips and tricks
admin

Lets say you want to limit the number of records users can add to the certain table. For example, if you run classifieds website you want free users to be able to post up to 3 ads, basic plan users can add up to 10 ads etc.
[size="4"]Sample database structure[/size]
Table: users

userid

password

limit
Table: ads

id

userid

adtext
Tables are linked via [b]userid[/b] field in both. It would also make sense to turn on Advanced security mode 'Users can see all data, can edit their own data only'.
The code in question needs to be added to BeforeAdd event.
[size="4"]Scenario 1[/size]
All users can add the same number of records

limit=3
set rs = CustomQuery("select count(*) as c from ads where userid = " & SESSION("UserID"))

count = rs("c")
if count>= limit then

Response.Write "Limit reached: " & count & " records added already"

Response.End

end if


[size="4"]Scenario 2[/size]
Each user has it's own limit. Limits are stored in limit field of users table.

set rs = CustomQuery("select count(*) as c from ads where userid = " & SESSION("UserID"))

count = rs("c")
set rs2 = CustomQuery("select limit from users where userid = " & SESSION("UserID"))

limit = rs2("limit")
if count>= limit then

Response.Write "Limit reached: " & count & " records added already"

Response.End

end if