⚠ 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 user id into DB while sending a new post



osama2
  • profile picture
  • Member

Posted 04 September 2012 - 20:29 PM

hi freinds

in my cms i allow users to add posts and i want to insert the user id dynamicaly from asession into db

when a user send anew post ?

any help ?


Note i'm using Tank auth so i can use get_user_id method to know the user id but my question is how to send this id while sending a post ?

Osama
  • profile picture
  • Member

Posted 08 September 2012 - 00:11 AM

can any one help me with this issue ? :(

victor
  • profile picture
  • Member

Posted 08 September 2012 - 00:17 AM

I do not understand the question?

victor
  • profile picture
  • Member

Posted 08 September 2012 - 00:21 AM

this code I use in my project:



function add_comment($module,$post_id, $user_id, $text)
{
$data = array(
'module' => $module ,
'post_id' => (int)$post_id ,
'user_id' => (int)$user_id,
'text' =>$text,
);

$this->db->insert('comments', $data);

$sql = "UPDATE `users` SET `count_comments` =`count_comments`+1 WHERE `id`='".$user_id."'";
$this->db->query($sql);
return TRUE;

}


controller:
if($data['article']['data']['commentable'] == "Y")
{
if($this->_USER_DATA['id'])
{
$this->form_validation->set_rules('text', 'comment', 'trim|required|min_length[1]|max_length[10000]|xss_clean|htmlspecialchars');
$this->form_validation->set_rules('post_id', 'post_id.', 'trim|required|min_length[1]|max_length[10]|xss_clean|is_natural');
if ($this->form_validation->run())
{
$this->comments->add_comment(__CLASS__,$this->input->post('post_id'), $this->_USER_DATA['id'], $this->input->post('text'));
redirect($this->uri->uri_string());
}
}
}


Helped you?

P.S: I have use other library,but the meaning is the same.

Osama
  • profile picture
  • Member

Posted 08 September 2012 - 02:48 AM

hi victor

i'm a new learner to codeigniter and gcrud too!

i'm using this code to allow users to add posts on my site

function new_post()
{
$crud = new grocery_CRUD();
$crud->set_table('articles');
$crud->columns('article_title');
$crud->fields('article_title','[color=#ff0000][b]user_id[/b][/color]');
$data = $crud->render();
}

so i want to set [color=#ff0000][b]user_id[/b][/color] value to $this->tank_auth_get_user_id();
how can i do this ?
and how to hide this input field 'user_id' from adding form ?
thank you for your answer

victor
  • profile picture
  • Member

Posted 08 September 2012 - 08:24 AM

[s]if you use sessions:
function new_post(){
...
$crud->callback_add_field(user_id,array($this,'user_id'));
...
}

function user_id()4
{
return '<input type="hidden" value="'.$this->session->userdata('user_id');.'" name="user_id" >';
}

but I would not do it, because it can be faked "user_id"[/s]

victor
  • profile picture
  • Member

Posted 08 September 2012 - 09:01 AM

use function callback_before_insert. http://www.grocerycrud.com/documentation/options_functions/callback_before_insert

Osama
  • profile picture
  • Member

Posted 23 September 2012 - 14:04 PM

[quote name='victor' timestamp='1347094860' post='3315']
use function callback_before_insert. http://www.grocerycr...k_before_insert
[/quote]

Thanks Victor