This topic is locked

pseudo incremental

5/30/2008 7:19:55 PM
PHPRunner General questions
V
vytb author

I would like to automatically number every child record for the particular parent record. How to do that?
Auto incremental fields are autoincremental for the table. This means that the child record ID's s for the master record will not be strictly autoincremental (I mean, not n+1). Thus, how to configure a field where all the child records will be autoincremental?
I presume, it would be a kind of small piece of PHP code? Again, I used to hear about mysql solutions...
Does anybody knows more?

J
Jane 6/2/2008

Hi,
you can increase value of this field in the Before record added event on the Events tab for detail table.

Also you can set up primary key in the detail field as auto-increment in the database directly.
There is only one restriction. You can't use auto-increment field as foreign key in the detail table.

V
vytb author 6/2/2008

Hi,

you can increase value of this field in the Before record added event on the Events tab for detail table.

Also you can set up primary key in the detail field as auto-increment in the database directly.
There is only one restriction. You can't use auto-increment field as foreign key in the detail table.


Could you offer a sample code (to send to the field (say 'n') value n = 1, next - n+1, etc.)?

J
Jane 6/4/2008

Hi,
here is a sample:

global $conn;

$rs = db_query("select max(FieldName) from TableName", $conn);

$data = db_fetch_numarray($rs);
$new_value = $data[0]+1;

V
vytb author 6/4/2008

Hi,

here is a sample:


Thank you Jane,
It should work ... however, there is nothing to select for the first child because it does not exist yet before adding record! Maybe for the first one the value '1' should be inserted simply?... For the second one, as well, I'm getting an error 'table does not exist'. What's the workaround?

J
Jane 6/4/2008

Hi,
you need to replace TableName with your actual table name.

Also you can check max value in your code:

...

if ($data)

$new_value = $data[0]+1;

else

$new_data = 1;

V
vytb author 6/4/2008

Hi,

you need to replace TableName with your actual table name.

Also you can check max value in your code:



Of course... '_' was missing, sorry <img src='https://asprunner.com/forums/file.php?topicimage=1&fieldname=reply&id=29984&image=1&table=forumreplies' class='bbc_emoticon' alt=':)' />
However, the value is not inserted...

The tables are _table1 (master) and _table2 (child). _table2 has the field 'test' where I would like to put the number of a child of a particular master.
the code is
[codebox]global $conn;

$rs = db_query("select max(test) from _table2", $conn);

$data = db_fetch_numarray($rs);
if ($data)

$new_value = $data[0]+1;

else

$new_data = 1;
return true;[/codebox]

J
Jane 6/5/2008

It's difficult to tell you what's happening without seeing actual files.
Please publish your project on Demo Account and send to support@xlinesoft.com a URL to your pages along with instructions on reproducing this error.

I'll find what's wrong with your project inspecting it at Demo account site.

V
vytb author 6/6/2008

It's difficult to tell you what's happening without seeing actual files.

Please publish your project on Demo Account and send to support@xlinesoft.com a URL to your pages along with instructions on reproducing this error.

I'll find what's wrong with your project inspecting it at Demo account site.


Thanks,
I've published the project on Demo Account (email sent).

T
thomaso 3/28/2009

I am trying to do the same thing with a Sequence column. What I don't see from the code sample is how $new_data is assigned to a column. Also to make the value visible in the Sequence column, in my case, to the user. How would that be done? One other thing, I am doing this using In-Line add.
Thanks!

V
vytb author 3/28/2009

I am trying to do the same thing with a Sequence column. What I don't see from the code sample is how $new_data is assigned to a column. Also to make the value visible in the Sequence column, in my case, to the user. How would that be done? One other thing, I am doing this using In-Line add.

Thanks!


If you think that the case is similar, your 'Sequence' is the 'test' in the code sample. Moreover, other variables could be needed, like in 'where' or else. Concerning, visibility to the user, I may not understand your issue - this value is not hidden, usually 'readonly' only...

T
thomaso 3/28/2009



If you think that the case is similar, your 'Sequence' is the 'test' in the code sample. Moreover, other variables could be needed, like in 'where' or else. Concerning, visibility to the user, I may not understand your issue - this value is not hidden, usually 'readonly' only...


Please forgive me for my confusing statement about about the value being visible. I would like the new incremented value to be placed in the forms 'Sequence' field when the user clicks on the InLine Add button/link.
As far as the 'test' goes, that is the field name from the original code, if I understand you correctly. I still do not see how "$new_value = $data[0]+1;" or "$new_data = 1;" knows which field to assign itself to. In my case I want the $new_value or $new_data to be assigned to the 'Sequence' field in my table and on the form field.
All this to say, I have been programming for a long time, but am new to PHP. I may be missing something in the syntax.

V
vytb author 3/28/2009



Please forgive me for my confusing statement about about the value being visible. I would like the new incremented value to be placed in the forms 'Sequence' field when the user clicks on the InLine Add button/link.
As far as the 'test' goes, that is the field name from the original code, if I understand you correctly. I still do not see how "$new_value = $data[0]+1;" or "$new_data = 1;" knows which field to assign itself to. In my case I want the $new_value or $new_data to be assigned to the 'Sequence' field in my table and on the form field.
All this to say, I have been programming for a long time, but am new to PHP. I may be missing something in the syntax.


I see...
At the end before 'return true', you will add $values["test"] = $new_value;
Should it mean "Sequence"?
I use it for the list page, not for the form (add page). May you test?
Another solution would be to use session variables: to save it/them in the event and to write defaults in the add or/and edit pages. However, I'm not an expert here and did no test. You may want to wait until Monday support help...

T
thomaso 3/28/2009

Thanks, vitas, for your help.
After reading your original post I did some more checking and playing around and did just what you mentioned. Now I get an error " Column 'Sequence' specified twice" Not sure what that means. I can't sem to find anything here in the forums.
I am looking at another solution. Using the Before display > BeforeShow... function. I am looking at $xt. Not sure how xt works but I have found a couple of things here on the forums that might guide me along. I am doing something like "$xt->assign("sequence_fieldblock", "10");" I don't know if that will work or not, but I am pressing along...
Thanks for your help!!
Thomas

T
thomaso 3/28/2009

Ok, after doing a lot more research, guessing, and such, I got it to work (for me at least).

global $data, $conn, $control_Sequence, $control_SectionQuestionID;
$QID = $control_SectionQuestionID["params"]["value"];
$str = "select max(sequence) from section_answer where SectionQuestionID =".$QID;

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

$data = db_fetch_numarray($rs);
$control_Sequence["params"]["value"] = $data[0]+1;
return true;


'Sequence', 'SectionQuestionID' are my field names and 'section_answer' is my table name. You will need to replace those values with your field and table names.
I also referenced this post.