Autoincrease ID field by 1

Discussions related to customizing hooks. Hooks are documented at http://bigprof.com/appgini/help/advanced-topics/hooks/
Post Reply
Travnicanin
Posts: 12
Joined: 2016-05-23 13:29

Autoincrease ID field by 1

Post by Travnicanin » 2016-05-24 06:54

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.

User avatar
a.gneady
Site Admin
Posts: 1281
Joined: 2012-09-27 14:46
Contact:

Re: Autoincrease ID field by 1

Post by a.gneady » 2016-05-24 19:26

All you need to do is set the primary key field to auto-increment in AppGini, then regenerate your code.
:idea: 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.

Travnicanin
Posts: 12
Joined: 2016-05-23 13:29

Re: Autoincrease ID field by 1

Post by Travnicanin » 2016-05-25 20:58

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 avatar
a.gneady
Site Admin
Posts: 1281
Joined: 2012-09-27 14:46
Contact:

Re: Autoincrease ID field by 1

Post by a.gneady » 2016-05-26 22:26

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:

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>';
}
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
:idea: 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.

Travnicanin
Posts: 12
Joined: 2016-05-23 13:29

Re: Autoincrease ID field by 1

Post by Travnicanin » 2016-06-02 11:47

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

DevGiu
AppGini Super Hero
AppGini Super Hero
Posts: 151
Joined: 2016-05-27 09:08

Re: Autoincrease ID field by 1

Post by DevGiu » 2016-06-02 17:59

Try with:

Code: Select all

$html .= '<script>$j( function() {';
$html .= " $j('#SifraGrupe').val(\"{$max_id}\");";
$html .= '})</script>';
The problem is in second html you are closing the string with the second single quote.
/Giuseppe
Professional Outsourcing Services

Travnicanin
Posts: 12
Joined: 2016-05-23 13:29

Re: Autoincrease ID field by 1

Post by Travnicanin » 2016-06-04 21:47

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!

User avatar
a.gneady
Site Admin
Posts: 1281
Joined: 2012-09-27 14:46
Contact:

Re: Autoincrease ID field by 1

Post by a.gneady » 2016-06-09 12:22

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):

Code: Select all

$j('#SifraGrupe').val(100);
Does this place a value of 100 inside the ID field?
:idea: 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.

Travnicanin
Posts: 12
Joined: 2016-05-23 13:29

Re: Autoincrease ID field by 1

Post by Travnicanin » 2016-07-03 10:17

YES!

and sorry for a long delay...

Travnicanin
Posts: 12
Joined: 2016-05-23 13:29

Re: Autoincrease ID field by 1

Post by Travnicanin » 2016-07-19 01:22

Now what is my next step in order to make this script working?

User avatar
a.gneady
Site Admin
Posts: 1281
Joined: 2012-09-27 14:46
Contact:

Re: Autoincrease ID field by 1

Post by a.gneady » 2016-07-20 03:25

Try this code in the Grupe_dv function:

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>';
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.
:idea: 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.

Travnicanin
Posts: 12
Joined: 2016-05-23 13:29

Re: Autoincrease ID field by 1

Post by Travnicanin » 2016-08-16 19:07

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

grimblefritz
AppGini Super Hero
AppGini Super Hero
Posts: 336
Joined: 2015-12-23 16:52

Re: Autoincrease ID field by 1

Post by grimblefritz » 2016-08-16 21:06

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();

Travnicanin
Posts: 12
Joined: 2016-05-23 13:29

Re: Autoincrease ID field by 1

Post by Travnicanin » 2016-08-17 19:30

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?

grimblefritz
AppGini Super Hero
AppGini Super Hero
Posts: 336
Joined: 2015-12-23 16:52

Re: Autoincrease ID field by 1

Post by grimblefritz » 2016-08-17 21:55

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.

grimblefritz
AppGini Super Hero
AppGini Super Hero
Posts: 336
Joined: 2015-12-23 16:52

Re: Autoincrease ID field by 1

Post by grimblefritz » 2016-08-17 21:57

Sorry, that should be Grupe.php, not Grupe-dv.php (shouldnt' be one of those anyway.)

Travnicanin
Posts: 12
Joined: 2016-05-23 13:29

Re: Autoincrease ID field by 1

Post by Travnicanin » 2016-08-18 09:00

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!

grimblefritz
AppGini Super Hero
AppGini Super Hero
Posts: 336
Joined: 2015-12-23 16:52

Re: Autoincrease ID field by 1

Post by grimblefritz » 2016-08-18 15:21

You're welcome. As with anything, it takes a while to grasp how the pieces of AppGini work together. Glad I could help.

Post Reply