CodeIgniter Forums
how can i get the particular post id in foreach loop - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: how can i get the particular post id in foreach loop (/showthread.php?tid=62173)

Pages: 1 2


how can i get the particular post id in foreach loop - Chandini - 06-15-2015

Hi , friend ....
  user posts display on foreach loop .. view looking good (See the below Images) when ever try to post the comment in  particular post ...  it work on only first row in foreach loop .. remain post comments are not inserted .. How can i insert the particular post id's, and and user comment s in particular post ... ?
I  try  to insert the comment in 2nd row in foreach loop the post id insert the first row id ... ?.... 
Only its work on first row in loop .. how can solve this problem ....? 

Help Me , thank u    


RE: how can i get the particular post id in foreach loop - Wouter60 - 06-16-2015

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);




RE: how can i get the particular post id in foreach loop - Chandini - 06-17-2015

(06-16-2015, 12:48 PM)Wouter60 Wrote: 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);




RE: how can i get the particular post id in foreach loop - Chandini - 06-17-2015

Its not working ... Only first record in loop comment and post is inserted remain posts are empty values inserted ...


RE: how can i get the particular post id in foreach loop - Wouter60 - 06-18-2015

Please add your controller and view files as attachments, not the print-screens.


RE: how can i get the particular post id in foreach loop - Chandini - 06-21-2015

(06-18-2015, 08:06 AM)Wouter60 Wrote: Please add your controller and view files as attachments, not the print-screens.



RE: how can i get the particular post id in foreach loop - Wouter60 - 06-22-2015

I think I found the problem.
Your view builds up a series of forms (within a foreach {  }  loop).
All forms have the same name: post_comment.
In your javascript function do_something(), you submit the form with the name post_comment. Since all forms have that name, only the first one is posted.

Now my question is: when the user hits the Enter key, do you want all comment-forms to be posted, or just the one where the user has filled-in some text and hit Enter?


RE: how can i get the particular post id in foreach loop - Chandini - 06-23-2015

(06-22-2015, 07:13 AM)Wouter60 Wrote: I think I found the problem.
Your view builds up a series of forms (within a foreach {  }  loop).
All forms have the same name: post_comment.
In your javascript function do_something(), you submit the form with the name post_comment. Since all forms have that name, only the first one is posted.

Now my question is: when the user hits the Enter key, do you want all comment-forms to be posted, or just the one where the user has filled-in some text and hit Enter?

When ever user write comment in  particular post  .. that post-id , and that post  comment will be inserted  in to data base show in db...
Thanks for problem finding ....


RE: how can i get the particular post id in foreach loop - Wouter60 - 06-24-2015

In a very compact form, your view should have this structure:

PHP Code:
//html header etc.

<?php foreach($result as $row) : ?>
 
// info about this post

<div>
<?php
echo form_open('regcheck/comment_insert',array('name'=>'comments_form[]','id'=>'form_' $row->post_id));
echo 
form_hidden('post_id[]'$row->post_id);
echo 
form_input('post_comment[]',NULL,'size="40" class="ta-comment"');
echo 
form_close();
?>    
</div>
 
<?php endforeach; ?>

<script>

 $('.ta-comment').keypress(function(event) {
  var frm_id = $(this).parents('form').attr('id');
  if (event.which == 13 ) {
   event.preventDefault();
   $('#' + frm_id).submit();
  } 
  
 });  
 
</script>

//html footer etc. 
Take a good look at the Jquery script at the end. This script triggers the submit event only for the form where the user has typed text and hit Enter.

Now in your controller, put this code to fetch the posted values:
PHP Code:
$post_id $this->input->post('post_id[0]');
$post_comment $this->input->post('post_comment[0]');
//insert these values into your database 
The reason why you need to add [0] after the post_id and post_comment variables, is that these are arrays. Since only one form is posted, this array contains only one key (0).


RE: how can i get the particular post id in foreach loop - Chandini - 06-25-2015

(06-24-2015, 07:37 AM)Wouter60 Wrote: In a very compact form, your view should have this structure:


PHP Code:
//html header etc.

<?php foreach($result as $row) : ?>
 
// info about this post

<div>
<?php
echo form_open('regcheck/comment_insert',array('name'=>'comments_form[]','id'=>'form_' $row->post_id));
echo 
form_hidden('post_id[]'$row->post_id);
echo 
form_input('post_comment[]',NULL,'size="40" class="ta-comment"');
echo 
form_close();
?> 
</div>
 
<?php endforeach; ?>

<script>

 $('.ta-comment').keypress(function(event) {
  var frm_id = $(this).parents('form').attr('id');
  if (event.which == 13 ) {
   event.preventDefault();
   $('#' + frm_id).submit();
  } 
  
 });  
 
</script>

//html footer etc. 
Take a good look at the Jquery script at the end. This script triggers the submit event only for the form where the user has typed text and hit Enter.

Now in your controller, put this code to fetch the posted values:

PHP Code:
$post_id $this->input->post('post_id[0]');
$post_comment $this->input->post('post_comment[0]');
//insert these values into your database 
The reason why you need to add [0] after the post_id and post_comment variables, is that these are arrays. Since only one form is posted, this array contains only one key (0).
Wow Thanks .. Its working ... really thanks to  Wouter60  Smile