Forums

How to initialized a field in a form

neorca 16 Feb, 2011
Hello,

I have made a form to register users (ID, name, surname) and save the informations in a table.
The first user (user1) has an ID=1
The second (user2) ID=2
...
I would like to initialized the field ID of the form with a incrementation of the value ID (which is in the table).

///////////////FORM////////////////////
ID : 3
Name :
Surname :

Thank you
GreyHead 16 Feb, 2011
Hi neorca,

You may want to give some more though to this. What happens if two users open the form at the same time?

You could get the current highest ID with a MySQL query at the beginning of the Form HTML.

But why not use the Joomla! Registration and the Joomla! User ID that is generated?

Bob
neorca 16 Feb, 2011
Thank you to your answer.
I've done a simple form as example because I need to do that for another bigest one.
That 's why I can't use the registration of Joomla.
I think the solution is to get the current highest ID with a MySQL query and to put the value in the field but my problem is how I do that ?
GreyHead 16 Feb, 2011
Hi neorca,

This is a **bad** idea. The code is
<?php
$db =& JFactory::getDBO();
$query = "
    SELECT MAX(`id`)
        FROM `#__some_form_name`;
";
$db->setQuery($query);
$max = $db->loadResult();
$max++;
?>
This topic is locked and no more replies can be posted.