This topic is locked

limit records for users on each table in particulary

10/4/2007 6:34:02 PM
PHPRunner General questions
L
luck author

Hi,
I have 4 tables:
users
clients
contracts
ads
I want to limit records only for first 3 tables on add. I need for user to enter only 1 record for first 3 table and unlimited for last table.
Please tell me how can i do that.
Thanks!

Alexey admin 10/5/2007

Hi,
to hide Add new button from the List page open your page in Visual Editor, set cursor to Add new button and switch to HTML mode.

Enclose the button into this code:

{if $user_has_no_records}...{/if}

Then use List page: Before Displayevent to check if user already added a record.

Here is the sample code:

global $conn;

$sql = "select count(*) from TableName where userid=...";

$rscount=db_query($sql,$conn);

$datacount=db_fetch_numarray($rscount);

if($datacount[0])

$smarty->assign("user_has_no_records",false);

else

$smarty->assign("user_has_no_records",true);


Also add the similar code to Before record added event to restrict users adding records if they use browser address box to go directly to Add page.

global $conn;

$sql = "select count(*) from TableName where userid=...";

$rscount=db_query($sql,$conn);

$datacount=db_fetch_numarray($rscount);

if($datacount[0])

return false;

else

return true;

L
luck author 10/11/2007

Hi Alexey,
I try what you send to me, but i don't know what i need to do to get this think working.
You give me a line like this
$sql = "select count(*) from TableName where userid=...";
I complete on the end where userid=$_SESSION["UserID"] but i don't know if it is correct.
Please tell me what i must complete at the end.
My autoincremental field is ID and on the security tab, advanced: main table ownerID is UserID and Users Table ownerID is ID.
Thank you in advance!

Alexey admin 10/11/2007

Try using this code:

$sql = "select count(*) from TableName where userid=".$_SESSION["_TableName_OwnerID"];

J
jetacera 9/16/2008

I'm using this code for a photo section using the members template, phpr version 4.2, and a mysql database.
I'm wanting each user to be able to add a maximum of 15 photos.
I'm using the default layout, and have the Add new link disappearing correctly, but when they go to the Add page directly they can still add more photos. I would also like to add a message if they go to the Add page as described in this post:

Limit Number of Records per User
The photos are associated with the members primary key mid. Since I'm using the members template, I used $_SESSION["ppuid"];
Here is my event code for the List page Before Display (this works):

[codebox]// List page: Before display

function BeforeShowList(&$smarty,&$templatefile)

{

//** Custom code ****

// put your custom code here
global $conn;

$sql = "select count(*) from photos where mid=".$_SESSION["ppuid"]."";

$rscount=db_query($sql,$conn);

$datacount=db_fetch_numarray($rscount);

if($datacount[0]>14)

$smarty->assign("user_has_no_records",false);

else

$smarty->assign("user_has_no_records",true);
} // function BeforeShowList

[/codebox]
Here is my event code for Before record added (this does not work):

[codebox]// Before record added

function BeforeAdd(&$values,&$message,$inline)

{

// Parameters:

// $values - Array object.

// Each field on the Add form is represented as a 'Field name'-'Field value' pair
//** Custom code ****

// put your custom code here
global $conn;

$sql = "select count(*) from photos where mid=".$_SESSION["ppuid"]."";

$rscount=db_query($sql,$conn);

$datacount=db_fetch_numarray($rscount);

if ($datacount[0]>14)

{

echo "You added 15 records. You can't add more than 15 records. Please delete some if you wish to add others.";

return false;

}

else

return true;
// return true if you like to proceed with adding new record

// return false otherwise
} // function BeforeAdd

[/codebox]
How do I get the Before Add event to work? I've played and played with the code and nothing seems to prevent a user from adding more photos if they go directly to the Add page.
Help!
Thanks,
JET
Thanks!

J
Jane 9/16/2008

Hi,
It's difficult to tell you what's happening without seeing actual files.
Please publish your project on Demo Account and send to support@xlinesoft.com a URL to your pages along with instructions on reproducing this error.

I'll find what's wrong with your project inspecting it at Demo account site.

J
jetacera 9/16/2008

Never mind, now it works. I was expecting the Add page to show the message before they tried to add the 16th image, but it shows when the user clicks the Save button, so I'm good to go. (my code worked fine for anyone else that needs to use it)
Thanks for your quick reply Jane!
JET