Page 1 of 1

Autoincrease ID field by 1

Posted: 2016-05-24 06:54
by Travnicanin
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.

Re: Autoincrease ID field by 1

Posted: 2016-05-24 19:26
by a.gneady
All you need to do is set the primary key field to auto-increment in AppGini, then regenerate your code.

Re: Autoincrease ID field by 1

Posted: 2016-05-25 20:58
by Travnicanin
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...)

Re: Autoincrease ID field by 1

Posted: 2016-05-26 22:26
by a.gneady
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

Re: Autoincrease ID field by 1

Posted: 2016-06-02 11:47
by Travnicanin
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

Re: Autoincrease ID field by 1

Posted: 2016-06-02 17:59
by DevGiu
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.

Re: Autoincrease ID field by 1

Posted: 2016-06-04 21:47
by Travnicanin
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!

Re: Autoincrease ID field by 1

Posted: 2016-06-09 12:22
by a.gneady
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?

Re: Autoincrease ID field by 1

Posted: 2016-07-03 10:17
by Travnicanin
YES!

and sorry for a long delay...

Re: Autoincrease ID field by 1

Posted: 2016-07-19 01:22
by Travnicanin
Now what is my next step in order to make this script working?

Re: Autoincrease ID field by 1

Posted: 2016-07-20 03:25
by a.gneady
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.

Re: Autoincrease ID field by 1

Posted: 2016-08-16 19:07
by Travnicanin
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

Re: Autoincrease ID field by 1

Posted: 2016-08-16 21:06
by grimblefritz
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();

Re: Autoincrease ID field by 1

Posted: 2016-08-17 19:30
by Travnicanin
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?

Re: Autoincrease ID field by 1

Posted: 2016-08-17 21:55
by grimblefritz
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.

Re: Autoincrease ID field by 1

Posted: 2016-08-17 21:57
by grimblefritz
Sorry, that should be Grupe.php, not Grupe-dv.php (shouldnt' be one of those anyway.)

Re: Autoincrease ID field by 1

Posted: 2016-08-18 09:00
by Travnicanin
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!

Re: Autoincrease ID field by 1

Posted: 2016-08-18 15:21
by grimblefritz
You're welcome. As with anything, it takes a while to grasp how the pieces of AppGini work together. Glad I could help.