Welcome Guest, Not a member yet? Register   Sign In
how can i get the particular post id in foreach loop
#2

You should name your fields as arrays, by adding square brackets after the field's name, like this:
PHP Code:
<textarea name="comment_text[]" rows=3 cols=40></textarea>
<
input type="hidden" name="post_id[]" value="<?= post_id;?>" /> 
When your form is posted, you can loop through the arrays like this:
PHP Code:
$posts $this->input->post('post_id');  //returns an array!!
$comments $this->input->post('comment_text');  //also returns an array
foreach ($posts as $key=>$post_id) {
  if (empty($comments[$key]) continue;  //don't save empty comments fields
  $data = array(
    'post_id' => $post_id,
    'comment' => $comments[$key]
  );
  $this->db->insert('table',$data);

Reply


Messages In This Thread
RE: how can i get the particular post id in foreach loop - by Wouter60 - 06-16-2015, 12:48 PM



Theme © iAndrew 2016 - Forum software by © MyBB