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;
$rs = CustomQuery("select count(*) as c from ads where userid = " . $_SESSION["UserID"]);
$data=db_fetch_array($rs);
$count = $data["c"];
if ($count>= $limit)
{
echo "Limit reached: $count records added already";
exit();
}
[size="4"]Scenario 2[/size]
Each user has it's own limit. Limits are stored in limit field of users table.
$rs = CustomQuery("select count(*) as c from ads where userid = " . $_SESSION["UserID"]);
$data=db_fetch_array($rs);
$count = $data["c"];
$rs2 = CustomQuery("select limit from users where userid = " . $_SESSION["UserID"]);
$data2=db_fetch_array($rs2);
$limit = $data2["limit"];
if ($count>= $limit)
{
echo "Limit reached: $count records added already";
exit();
}