Best Way to Limit record in table to only 1

The recommended method of customizing your AppGini-generated application is through hooks. But sometimes you might need to add functionality not accessible through hooks. You can discuss this here.
Post Reply
bambinou
Veteran Member
Posts: 162
Joined: 2013-02-01 15:09

Best Way to Limit record in table to only 1

Post by bambinou » 2016-01-13 14:39

Hello,

I am trying to hook up the back end with a front end template, my problem is that I need to be able to only let the customer upload 1 record in the table whih consist of (1 id, 1 picture, 1 page title, 1 page description). The user should be able to edit or delete this record but not add another one.

What is the best way to do this please?

Thanks,

Bertv
Veteran Member
Posts: 52
Joined: 2013-12-11 15:59

Re: Best Way to Limit record in table to only 1

Post by Bertv » 2016-01-13 14:59

Hi,
I have the same situation and solved it as followed:
- create the table in AppGini; checked the 'hide in homepage and 'hide in navigation menu'.
- in phpMyAdmin I create the record with id 1
- in the links-home.php and links-navmenu.php in the hooks folder I create an entry with the url: 'url' => 'tablename_view.php?SelectedID=1'

Regards,
Bert
I am using Appgini 5.62

bambinou
Veteran Member
Posts: 162
Joined: 2013-02-01 15:09

Re: Best Way to Limit record in table to only 1

Post by bambinou » 2016-01-13 15:09

Thanks, I will give it a shot tonight! Great way of doing it:-)
I did not think about hard coding the parameters......

bambinou
Veteran Member
Posts: 162
Joined: 2013-02-01 15:09

Re: Best Way to Limit record in table to only 1

Post by bambinou » 2016-01-13 18:41

I tried your solution, unfortunately there is a problem with that.

Imagine that I do not want to have any data in that table before giving my script to someone, the person will have to click on "add", it is fine because the first id will be 1 if the table has been reset. The problem arise after that, after inserting that value, a "add new" button shows up, now I could edit this in PHP but will need to go into the fields themselves with a condition like echo button if id is not equal to 1 but if I do this, I am going to mix myself up between the script auto generation from appgini and my own code....I wonder if there is a way to change that....

Thanks,

User avatar
shasta59
AppGini Super Hero
AppGini Super Hero
Posts: 231
Joined: 2013-01-08 19:40
Location: Calgary, Alberta, Canada

Re: Best Way to Limit record in table to only 1

Post by shasta59 » 2016-02-07 23:38

Using Appgini 5.50

Try placing this script in your hooks file for the table you wish to limit to one record per user. It goes under the before insert function. You will get a message displaying which states it could not save the new record. User can modify their one record all they want but cannot save a new copy or a new record.

It will make the program not allow the save. It is very basic but works. You also may wish to add in a note about limiting to one record in the template file etc.

Code: Select all

function yourtablename_before_insert(&$data, $memberInfo, &$args){
	$memberid1 = ($memberInfo['username']);
	$getrecordcount = sqlValue("select COUNT(tableName) FROM membership_userrecords WHERE tableName = 'yourtablename' AND memberID = '$memberid1' ");
if ($getrecordcount > 0) return FALSE;

		return TRUE;
	}
$memberid1 - this holds the value for the member username
$getrecordcount - this is the number of records where the count of the number of records in membership_userrecords contains the value in $memberid1.
membership_userrecords - this is a default table created automatically by AppGini
tableName - a field in membership_userrecords which holds the name of the table the record belongs to
yourtablename - this is the name of your table (it should match the name of the hook file you are putting this code in minus the php extension)



You do not need to use or create the variable $memberid1 if you do not want to. You can incorporate the variable value directly in the statement if you wish. I find this way makes it easier to explain.

I also recommend you trap for the admin user or other user group like this to allow the admin or some other group to create more than one. I use this where I have a database which I need to have a member info table but only want 1 record in there.

Here is sample code for allowing the admin or others to create more than one record if you wish. Depends upon your usage I guess. It goes before the line beginning with $memberid1. Right below the function line.

Code: Select all

if($memberInfo['group']=='Admins' || $memberInfo['group']=='someothergroup'){
		 return TRUE;
		
	}else{
Hope this works for you.

Alan
Calgary, Alberta, Canada - Using Appgini 5.50 -

User avatar
shasta59
AppGini Super Hero
AppGini Super Hero
Posts: 231
Joined: 2013-01-08 19:40
Location: Calgary, Alberta, Canada

Re: Best Way to Limit record in table to only 1

Post by shasta59 » 2016-02-08 00:08

Using Appgini 5.50

The method I described above, with a little additional code, can also be used to limit new record creation between dates.

For example:

You wish to not allow a member/user to create more than X number of records between Jan 1 and Jan 15. You just adapt the above code to check how many records they created between the two dates and either allow or do not allow new record creation.

In cases where I use this I check to see how many records have been created in the last X number of days. If it exceeds a certain limit then they are not allowed to create more records. I set this value in the Admin section using code I added to allow easy setting of the number of days. But that is a whole other discussion area.

Alan
Calgary, Alberta, Canada - Using Appgini 5.50 -

bambinou
Veteran Member
Posts: 162
Joined: 2013-02-01 15:09

Re: Best Way to Limit record in table to only 1

Post by bambinou » 2016-02-08 09:45

Thank you Alan,

I wish your reply came 1 month earlier :-))) as I was so stuck in the project that I had to move to Yii2 to get the project going.
I will give it a shot with Appgini sometime next week as I would still want to use Appgini.

Thanks again,

Ben

Mike Ryba
Posts: 1
Joined: 2016-11-26 18:15

Re: Best Way to Limit record in table to only 1

Post by Mike Ryba » 2016-11-26 20:57

Hi Shasta,

I'm in version 5.0 rev. 835, and have tried your hook code solution for limiting 1 record saved to a table - but with no success. My code implementation is as follows:

Code: Select all

function ad_manager_before_insert(&$data, $memberInfo, &$args){

   $memberid1 = ($memberInfo['username']);
   $getrecordcount = sqlValue("select COUNT(tableName) FROM membership_userrecords WHERE tableName = 'ad-manager' AND memberID = '$memberid1' ");
if ($getrecordcount > 0) return FALSE;
		return TRUE;
	}
I consistently get a "record saved" message and find that additional record(s) get added. I tried the IF statement as >1 in case that might have made a difference. But neither >0 or >1 do the trick.

I even changed the return TRUE; to return FALSE; just to make sure the before_insert section of the hook file was working. In that case, I did get the "record cannot be saved" message. If it hadn't been for that, I would've thought the code wasn't being executed at all. I had placed several ECHO "text"; commands in various places in your code but they didn't display.

Any guidance would be appreciated - greatly!

bambinou
Veteran Member
Posts: 162
Joined: 2013-02-01 15:09

Re: Best Way to Limit record in table to only 1

Post by bambinou » 2017-04-21 10:12

I wish they had a lot ore tutorials on their website(without the need to pay more on udemy....) Basic things like this one becomes really difficult .

Post Reply