This topic is locked

Limit one add only

3/5/2009 3:14:17 PM
PHPRunner General questions
M
mmponline author
  1. How do one limit a user to add only 1 record to his list. Thereafter he must only be able to edit the record, but not add more.
  2. Is there a way to set the amounts of edits by adding for instance a field to a record saying limit and then add a number of the amount of records a user can add. Eg. 3 or 5 or 7
    Thanks for help.

J
Jane 3/6/2009

Stephan,
please see my answers below:

  1. you can check number of records on the page and hide add button if needed in the List page: Before display event on theEventstab.

    Here is a sample:
    global $conn;

    $str = "select count(*) from TableName";

    $rs = db_query($str,$conn);

    $data = db_fetch_array($rs);

    if ($data[0]) //if record exist

    $xt->assign("add_link",false);


2. use the same mechanism for edit link in the List page: After record processed event.

Check number of edits and hide edit link if needed:

global $record;

$record["edit_link"]=false;

M
mmponline author 3/6/2009

I get an error message:

Undefined offset: 0


My code:

global $conn;

$str = "select count(*) from magstay";

$rs = db_query($str,$conn);

$data = db_fetch_array($rs);

if ($data[0]) //if record exist

$xt->assign("add_link",false);


replaced the 0 with a 1 then get an error:

Undefined offset: 1


Also I would love to be able to give admin the option to add a number to a field (Eg. "number of credits" in the login table, and it will then allow only that amount of adds for that user. IOW instead of a global value for all.

J
Jane 3/6/2009

Check me changes below:

data = db_fetch_numarray($rs);


Also you can select number of credits from login table manually and then compare it with $data[0] value.

M
mmponline author 3/6/2009

This code works fine, but is applicable to the whole table. IOW Even the Admin and other users can't add if there is 1 record in the table.
I have the table setup so that users can see and edit their own data only. They now can't add with this code as there are data in another user already.
Can you help me more on the credits option via login as well. I'll add a field Credits (It's the members template and they have to be activated and dataed already) and can tehn set the credits as well.
Thanks again for your always willing support.

J
Jane 3/6/2009

Stephan,
I suppose you need to add where clause to your SQL query in your code.

$_SESSION["UserID"] contains current username.

M
mmponline author 3/7/2009

I've added the code as suggested but now the add shows again no matter the amount of records.

global $conn;

$str = "select count(*) from magstay where LoginID='".$_SESSION["UserID"]."'";

$rs = db_query($str,$conn);

$data = db_fetch_numarray($rs);

if ($data[0]) //if record exist

$xt->assign("add_link",false);


Please help.
Anyone that used a code to set the "credits" so that one can add the value? The credits can be added to the table in question or the Login table.
I need this desperately.

M
mmponline author 3/7/2009

See above comment...

M
mmponline author 3/9/2009

No help on this yet. Please assist!

J
Jane 3/10/2009

Stephan,
It's difficult to tell you what's happening without seeing actual files.
Please publish your project on Demo Account and open a ticket at http://support.xlinesoft.com sending a URL to your pages along with instructions on reproducing this error.

M
mmponline author 3/11/2009

With the help of Jane, I got this snippet that will only allow 1 add per user once logged on. This is ideal if a person pays for a specific venue for example. He will then only be able to load one item but then edit and maintain it as needed.

$rs2 = CustomQuery("select * from memusers where UserName='".$_SESSION["UserID"]."'");

$data2 = db_fetch_array($rs2);
$rs = CustomQuery("select count(*) from magstay where LoginID=".$data2["ID"]."");

$data = db_fetch_numarray($rs);
if ($data[0]) //if record exist

$xt->assign("add_link",false);


Thanks again for your excellent support, Jane and the rest of the team.
Blessings!

M
mmponline author 3/12/2009

Here is the code for those interested.

Scenario:

Eg. Peter has 5 credits - can only add up to 5 records. Then the Add link disappears.

Mary has 2 credits and can only add 2 fields.

Etc.
Add a Credits field to your Login table. Add the number of credits you would allow a specific user. When he exceeds this amount, the add link disappears until you increase the amount again. The assumption is that users only have access to their own records and not those of others for the specific table. (In this case magstay)
The final code with the help of Jane(Thanks again!):

$rs2 = CustomQuery("select * from memusers where UserName='".$_SESSION["UserID"]."'");

$data2 = db_fetch_array($rs2);
$rs3 = CustomQuery("select Credits from memusers where UserName='".$_SESSION["UserID"]."'");

$data3 = db_fetch_array($rs3);



$rs = CustomQuery("select count(*) from magstay where LoginID=".$data2["ID"]."");

$data = db_fetch_numarray($rs);
if ($data[0]>=$data3["Credits"])

$xt->assign("add_link",false);