Autoincrease ID field by 1
-
- Posts: 12
- Joined: 2016-05-23 13:29
Autoincrease ID field by 1
Hello everybody.
I was searching this forum and found simillar topics, but I would need a little help because I'm a newbee here.
I want to do following:
1. user see a table view of a "Table1"
2. click "add new"
3. in a detail view automatically is offered a new id with a number increased for 1 (last max value of Table1.ID+1)
Please help!
I tried to "play" with Table1.php in the hooks directory, but no success.
Thanks.
I was searching this forum and found simillar topics, but I would need a little help because I'm a newbee here.
I want to do following:
1. user see a table view of a "Table1"
2. click "add new"
3. in a detail view automatically is offered a new id with a number increased for 1 (last max value of Table1.ID+1)
Please help!
I tried to "play" with Table1.php in the hooks directory, but no success.
Thanks.
Re: Autoincrease ID field by 1
All you need to do is set the primary key field to auto-increment in AppGini, then regenerate your code.
AppGini plugins to add more power to your apps:
- DataTalk is an innovative AppGini plugin based on ChatGPT that allows you to interact with your AppGini database using natural language questions, without writing any SQL. Check the demo video
- Mass Update plugin: Update multiple records at once and improve your workflow efficiency.
- Check our other plugins and get a generous discount of up to 30% when buying 2 or more plugins.
-
- Posts: 12
- Joined: 2016-05-23 13:29
Re: Autoincrease ID field by 1
That is not what I want.
user has to be able to change it to another value according to his/her wish,but satisfying the conditions (unique, is number etc...)
user has to be able to change it to another value according to his/her wish,but satisfying the conditions (unique, is number etc...)
Re: Autoincrease ID field by 1
Okay .. in that case, the field should be editable ... then in the tablename_dv hook (where tablename is the name of the concerned table), you should check if this is a new record ($selectedID is false) and if so, retrieve the maximum value of the primary key field from the table, add one to it, and inject jQuery code for placing the incremented max value into the form ... Assuming the primary key field is named 'id', here is a sample code to do this:
For more info on hooks:
Hooks documentation: https://bigprof.com/appgini/help/advanced-topics/hooks/
AppGini customization course on Udemy (not free but has some lessons that are free to preview): https://www.udemy.com/customizing-appgi ... ode=TENOFF
Code: Select all
if(!$selectedID){
$max_id = intval(sqlValue("select max(id) from tablename")) + 1;
$html .= '<script>$j( function() {';
$html .= ' $j('#id').val("' . $max_id . '");';
$html .= '})</script>';
}
Hooks documentation: https://bigprof.com/appgini/help/advanced-topics/hooks/
AppGini customization course on Udemy (not free but has some lessons that are free to preview): https://www.udemy.com/customizing-appgi ... ode=TENOFF
AppGini plugins to add more power to your apps:
- DataTalk is an innovative AppGini plugin based on ChatGPT that allows you to interact with your AppGini database using natural language questions, without writing any SQL. Check the demo video
- Mass Update plugin: Update multiple records at once and improve your workflow efficiency.
- Check our other plugins and get a generous discount of up to 30% when buying 2 or more plugins.
-
- Posts: 12
- Joined: 2016-05-23 13:29
Re: Autoincrease ID field by 1
Thank you.
I have copied the code into tablename.php file under Grupe_dv section, but I get blank screen.
Here is the code :
function Grupe_dv($selectedID, $memberInfo, &$html, &$args){
if(!$selectedID){
$max_id = intval(sqlValue("select max(SifraGrupe) from Grupe")) + 1;
$html .= '<script>$j( function() {';
$html .= ' $j('#SifraGrupe').val("' . $max_id . '");';
$html .= '})</script>';
}
}
Legend:
"Grupe" is a table
"SifraGrupe" is ID field of "Grupe" table
I have copied the code into tablename.php file under Grupe_dv section, but I get blank screen.
Here is the code :
function Grupe_dv($selectedID, $memberInfo, &$html, &$args){
if(!$selectedID){
$max_id = intval(sqlValue("select max(SifraGrupe) from Grupe")) + 1;
$html .= '<script>$j( function() {';
$html .= ' $j('#SifraGrupe').val("' . $max_id . '");';
$html .= '})</script>';
}
}
Legend:
"Grupe" is a table
"SifraGrupe" is ID field of "Grupe" table
Re: Autoincrease ID field by 1
Try with:
The problem is in second html you are closing the string with the second single quote.
Code: Select all
$html .= '<script>$j( function() {';
$html .= " $j('#SifraGrupe').val(\"{$max_id}\");";
$html .= '})</script>';
/Giuseppe
Professional Outsourcing Services
Professional Outsourcing Services
-
- Posts: 12
- Joined: 2016-05-23 13:29
Re: Autoincrease ID field by 1
OK, we have some progress.
Now the detailed view appear, but no value in the ID field named "SifraGrupe".
I hope someone understood my intention:
-when a user press "new", detailed view appear with a value "maxID+1"
Anybody has a suggestion?
Thanks!
Now the detailed view appear, but no value in the ID field named "SifraGrupe".
I hope someone understood my intention:
-when a user press "new", detailed view appear with a value "maxID+1"
Anybody has a suggestion?
Thanks!
Re: Autoincrease ID field by 1
Let's make a quick test to debug this: On the detail view page of the Grupe table, click F12 to open the inspector window. Go to the console, and type this command (then press Enter or click 'Run' to execute it):
Does this place a value of 100 inside the ID field?
Code: Select all
$j('#SifraGrupe').val(100);
AppGini plugins to add more power to your apps:
- DataTalk is an innovative AppGini plugin based on ChatGPT that allows you to interact with your AppGini database using natural language questions, without writing any SQL. Check the demo video
- Mass Update plugin: Update multiple records at once and improve your workflow efficiency.
- Check our other plugins and get a generous discount of up to 30% when buying 2 or more plugins.
-
- Posts: 12
- Joined: 2016-05-23 13:29
Re: Autoincrease ID field by 1
YES!
and sorry for a long delay...
and sorry for a long delay...
-
- Posts: 12
- Joined: 2016-05-23 13:29
Re: Autoincrease ID field by 1
Now what is my next step in order to make this script working?
Re: Autoincrease ID field by 1
Try this code in the Grupe_dv function:
If it's not working, please open the console window as explained in a previous post (press F12, then go to the console tab). Refresh the page while the console window is open, and check to see if there are any error message displayed in the console. The console should also display the value of max_id.
Code: Select all
if($selectedID) return;
$max_id = intval(sqlValue("select max(SifraGrupe) from Grupe")) + 1;
$html .= '<script>$j( function() {';
$html .= ' console.log("max_id: ' . $max_id . '")';
$html .= ' $j('#SifraGrupe').val("' . $max_id . '");';
$html .= '})</script>';
AppGini plugins to add more power to your apps:
- DataTalk is an innovative AppGini plugin based on ChatGPT that allows you to interact with your AppGini database using natural language questions, without writing any SQL. Check the demo video
- Mass Update plugin: Update multiple records at once and improve your workflow efficiency.
- Check our other plugins and get a generous discount of up to 30% when buying 2 or more plugins.
-
- Posts: 12
- Joined: 2016-05-23 13:29
Re: Autoincrease ID field by 1
Still no number in the field "SifraGrupe". Error messages displayed:
Grupe-dv.js:3 Uncaught SyntaxError: Unexpected token if
Grupe_view.php:229 Uncaught TypeError: "#SifraGrupe".val is not a function
Grupe-dv.js:3 Uncaught SyntaxError: Unexpected token if
Grupe_view.php:229 Uncaught TypeError: "#SifraGrupe".val is not a function
-
- AppGini Super Hero
- Posts: 336
- Joined: 2015-12-23 16:52
Re: Autoincrease ID field by 1
This might spare you some headaches trying to track down quoting issues. Same code, just minus all the concatenations and embedded quotes.
Code: Select all
if($selectedID) return;
$max_id = intval(sqlValue("select max(SifraGrupe) from Grupe")) + 1;
ob_start();
?>
<script>
$j(function(){
console.log('max_id: <?php echo $max_id; ?>');
$j('#SifraGrupe').val(<?php echo $max_id; ?>);
});
</script>
<?php
$html .= ob_get_contents();
ob_end_clean();
-
- Posts: 12
- Joined: 2016-05-23 13:29
Re: Autoincrease ID field by 1
This is what I have in Grupe-dv.js
function grupe_dv($selectedID, $memberInfo, &$html, &$args){
if($selectedID) return;
$max_id = intval(sqlValue("select max(SifraGrupe) from Grupe")) + 1;
ob_start();
?>
<script>
$j(function(){
console.log('max_id: <?php echo $max_id; ?>');
$j('#SifraGrupe').val(<?php echo $max_id; ?>);
});
</script>
<?php
$html .= ob_get_contents();
ob_end_clean();
}
This is a list of errors:
Uncaught SyntaxError: Unexpected token & Grupe-dv.js:1
Uncaught TypeError: "#SifraGrupe".val is not a function Grupe_view.php:229
What am I doing wrong?
function grupe_dv($selectedID, $memberInfo, &$html, &$args){
if($selectedID) return;
$max_id = intval(sqlValue("select max(SifraGrupe) from Grupe")) + 1;
ob_start();
?>
<script>
$j(function(){
console.log('max_id: <?php echo $max_id; ?>');
$j('#SifraGrupe').val(<?php echo $max_id; ?>);
});
</script>
<?php
$html .= ob_get_contents();
ob_end_clean();
}
This is a list of errors:
Uncaught SyntaxError: Unexpected token & Grupe-dv.js:1
Uncaught TypeError: "#SifraGrupe".val is not a function Grupe_view.php:229
What am I doing wrong?
-
- AppGini Super Hero
- Posts: 336
- Joined: 2015-12-23 16:52
Re: Autoincrease ID field by 1
I see the problem. It's not the code - it's where you're putting it. You are putting php code into a javascript file.
You need to put this code in the Grupe-dv.php file, in the function grupe_dv().
Put it there, remove it from Grupe-dv.js, and it should work.
You need to put this code in the Grupe-dv.php file, in the function grupe_dv().
Put it there, remove it from Grupe-dv.js, and it should work.
-
- AppGini Super Hero
- Posts: 336
- Joined: 2015-12-23 16:52
Re: Autoincrease ID field by 1
Sorry, that should be Grupe.php, not Grupe-dv.php (shouldnt' be one of those anyway.)
-
- Posts: 12
- Joined: 2016-05-23 13:29
Re: Autoincrease ID field by 1
YES YES YES !!!
The last code is working perfect after I put it in Grupe.php under Grupe_dv function.
So much trouble, but finally it's working.
THANKS A LOT!
The last code is working perfect after I put it in Grupe.php under Grupe_dv function.
So much trouble, but finally it's working.
THANKS A LOT!
-
- AppGini Super Hero
- Posts: 336
- Joined: 2015-12-23 16:52
Re: Autoincrease ID field by 1
You're welcome. As with anything, it takes a while to grasp how the pieces of AppGini work together. Glad I could help.