Help with db read

kimos 04 Sep, 2014
Hello,i am trying to retrieve a record to my form.
For now i have a simple form just to understand how db read works.
So i have 3 text boxes with field names and field id text1,text2 and text3 respectively.
I crated a table to the database with name read_db.
Here is a photo for my record to database.
[attachment=0]read_db.png[/attachment]

How can i load the data?
Please help
kimos 04 Sep, 2014
I forgot to say that i am using Chronoforms v5 and joomla version 3.3.3.
GreyHead 04 Sep, 2014
Hi kimos,

You don't say why you need to read the data so my answer may not be quite correct ;-(

In the DB Read action:

+ set the Table to read_db
+ set Enable Model ID to No
+ add this to the Conditions box to identify the record to read
<?php
return array( 'id' => 1);
?>

Bob
kimos 04 Sep, 2014
Hi Bob,

Thanks for you quick reply!
Well my aim is when a user submit a form to save the data to a database. When the same user tries to submit again the same form retrieve his old record from the database to the form and if he want to edit the old record submit a new one and replace the old.
I hope you understand what i mean...
kimos 04 Sep, 2014
Can you please help me how to do it?
GreyHead 04 Sep, 2014
Hi Kimos,

OK then my instructions should work but you will need to update the Conditions entry to search for a selected entry. Please see this FAQ for instructions.

Bob
kimos 04 Sep, 2014
Hi Bob,

I have 2 questions:
1)The code you told me works fine if i have only one record on my database but how can i fix if i have records from different users?

2)To the link that you gave me i see the ChronoConnectivity... I must install it in order to achieve my goal?

Thanks,
Kimos
GreyHead 04 Sep, 2014
Hi Kimos,

No you don't need ChronoConnectivity - though it may help.

You haven't said how you will identify the form you want to edit . . . one way is to include an id in a URL e.g. my_id=1 - then use that value in the Conditions box of the DB Read:
<?php
return array( 'id' => $form->data['my_id'] );
?>


Bob
kimos 04 Sep, 2014
Hi Bob,

I can't understand exactly what are you saying...
I will try to explain you better what i am trying to achieve...
The form's name is 'read' with the fields i said before.Users can see the form only if they were login.If a user has submited the form,i want if he revisit the form to load to him the previous record and if he want to edit some fields and save again the form and replace the old one.
For that reason i want the form loads the record from the user that he is login if he has sumbit the form of course...

I hope this time i was clear enough...
Thank,
Kimos
GreyHead 04 Sep, 2014
Hi kimos,

So there is only one record per user and that is the record that you want them to edit.

In that case you should:

a) add an Authenticator action so that only Logged in Users can access the form.

b) use the User ID to identify the record that you wan to load. For this to work you need to save the User ID in the record.

Please see this FAQ about getting the User ID.

Bob
kimos 04 Sep, 2014
Hi Bob,

Ok now i think you get me!
If i understand correct the Authenticator must be like this?
[attachment=0]authenticator.png[/attachment]

and my setup like this?
[attachment=1]setup.png[/attachment]


Also the drop down fields in Authenticator have to change?

Thanks,
Kimos
kimos 04 Sep, 2014
And also,i have to add this code to the condition box in DB Read?

<?php
$user =& JFactory::getUser();
$form->data['user_id'] = $user->id;
?>
GreyHead 04 Sep, 2014
Hi kimos,

No

Bob
kimos 04 Sep, 2014
Hi Bob,

No to what?
What code i have to add to DB Read or i have to use custom code?

Thanks,
Kimos
kimos 05 Sep, 2014
Hi Bob,

Can you please help me how to fix it?

Thanks,
Kimos
GreyHead 05 Sep, 2014
Answer
Hi kimos,

In the Authenticator you need to set Public and Guest to Disallowed and Registered to Allowed - I think that should do it,

You also need an Event Loop action in the pink On Fail event to handle failures.

Apologies, you do need some code in the DB Read (I got confused with another thread :-( )
It will be something like this in CFv5
<?php
$user =& JFactory::getUser();
return array('user_id' => $user->id);
?>

Bob
kimos 05 Sep, 2014
Hi Bob,

It works fine!
Thank you very much for your help and i'm sorry if i disturb you with my questions...
Very good support!

Thanks,
Kimos
kimos 07 Sep, 2014
Hi Bob,

I have one more question... The db read works perfect and loads all the fields from the database but i want if a user press submit again to the form because he edit the fiels to save the new data to the old record,i want to overwrite the old one and not to create a new record with a new id.
How can i do it?Please help...

Thanks,
Kimos
GreyHead 07 Sep, 2014
Hi Kimos,

You need to add the value of the Primary Key of the record you want to edit to a hidden input in the form to make sure that record is updated. If ChronoForms created the table the primary key is probably id - add a hidden input with this name to the form in the Designer tab.

Bob

PS Using id as a primary key is a bad idea and can cause problems with forms shown in articles - I'd suggest that you change it to cf_id if you can.
kimos 07 Sep, 2014
Hi Bob,

I have few questions:

1) I have to create a hidden field and rename it to id?(because that is the name of the primary key)

2)How can i save the primary key to the hidden field?I have to add code?

Thanks,
Kimos
GreyHead 08 Sep, 2014
Hi Kimos,

1) Yes

2) No

Bob
This topic is locked and no more replies can be posted.