⚠ In case you've missed it, we have migrated to our new website, with a brand new forum. For more details about the migration you can read our blog post for website migration. This is an archived forum. ⚠

  •     

profile picture

Insert query question



alex646
  • profile picture
  • Member

Posted 11 June 2013 - 03:16 AM

Hey guys,

 

CG is the greatest out there!

 

I'm just starting playing with it and I love it. I'm not that good with coding : )

 

My question is:

 

I have two tables:  'posts'  and 'users'

 

I was able to set relationship and all works great when I do crud operations on these tables. One problem I have is:

 

I have to select the user id in the dropdown box to insert post into correct users table, e.g. tell GC to insert new post where

user_id is by selecting it from the correctly populated dropdown box which has a user_id.

 

Can this be done automatically so that I don't have to select the user_id manualy from the dropdown box?

 

Thanks a lot!


davidoster
  • profile picture
  • Member

Posted 11 June 2013 - 11:15 AM

Hello [member=alex646] and welcome to the forums.

So let's rephrase the question,

You are on the add form of the posts table which has (via a set_relation) a dropdown with the users.

And you need to auto select the correct user_id.

 

So the question is:

from where do you get this correct user_id? from the logged in user, from a session variable from the url?

 

depending on the origin of the user_id you follow the proper path to reslove it.

Most probably you will need a combination of PHP and js(jquery) to make it happen.


alex646
  • profile picture
  • Member

Posted 11 June 2013 - 13:28 PM

Thanks davidoster!

 

 

I'm getting the correct user_id from the logged in user's session variable. Any ideas how to implement this?

 

 

Thanks!!

 

 

davidoster
  • profile picture
  • Member

Posted 11 June 2013 - 21:31 PM

Well I am not sure if this is going to work but try to callback_add_field and use something like this:

 

return '<input type="text" maxlength="50" value="' . $user_id . '" name="user_id" style="width:462px">';

 

You could also try to use the field_type function and use the hidden or invisible to set the value of the field.

 

Another way is to call the callback_before_insert and set there before saving the value of the user_id. This can be accompanied with the option of not displaying the user_id field via the fields function.


alex646
  • profile picture
  • Member

Posted 11 June 2013 - 22:48 PM

Thanks davidoster!!

 

field_type   did the job very easily

 

Thanks a lot!