• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how can i get the particular post id in foreach loop

#1
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    


Attached Files Thumbnail(s)
                   
Manikanta
Reply

#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

#3
(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);

Manikanta
Reply

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

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

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


Attached Files
.php   regcheck.php (Size: 5.71 KB / Downloads: 165)
.php   home_view.php (Size: 4.57 KB / Downloads: 182)
Manikanta
Reply

#7
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?
Reply

#8
(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 ....


Attached Files Thumbnail(s)
   
Manikanta
Reply

#9
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).
Reply

#10
(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 
Manikanta
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.