SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Hybrid View

  1. #1
    SitePoint Enthusiast
    Join Date
    Jun 2010
    Posts
    82
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Problem with nested foreach loop

    I have foreach loop within a foreach loop, but when I insert into the db, it inserts more than 1 entry for the each item.. Is my foreach loop wrong for what I am trying to do.

    VIEW

    HTML Code:
    <input type="hidden" name="question_id[]" value="<?php echo $j->id;?>"/>
    
    <input name="answers[]" id="questions" class="deal_form"/>
    CONTROLLER

    PHP Code:
            $answers $this->input->post('answers');
                
                
    $question_id $this->input->post('question_id');
                
                
    $this->load->model('model_survey');
                
                    foreach(
    $answers as $d) {
                        
                        foreach(
    $question_id as $q){
                        
                            
    $this->model_survey->insert_survey_answers($d,$q);    
                        
                        }
                        
                    } 
    MODEL

    PHP Code:
            function insert_survey_answers($answer,$id){
                
                
    $query "INSERT INTO survey_answers (answer,question_id) VALUES(".$this->db->escape($answer).",'".$id."')";
                
                
    $this->db->query($query);
                
            } 

  2. #2
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    73 Post(s)
    Tagged
    0 Thread(s)
    Something tells me you dont want nested foreach loops, you just want to do


    foreach($answers as $key => $d) {
    $this->model_survey->insert_survey_answers($d,$question_id[$key]);
    }
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  3. #3
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,197
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    To be very blunt yes it is as you as simple loop through one array then through another until your completely finishing running through the answers array, the below code however should fix the problem.

    PHP Code:
    foreach ($question_id as $key => $question) {
        
    $this->model_survey->insert_survey_answers($answers[$key], $question);

    Off Topic:

    StarLion beat me to it but we both had the same idea

  4. #4
    SitePoint Wizard gRoberts's Avatar
    Join Date
    Oct 2004
    Location
    Birtley, UK
    Posts
    2,439
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It may be worth re-structuring/renaming your input fields.

    You can use

    Code php:
    <input type="text" name="Answer[1]" value="Answer value" />

    the number within [1] will contain the question id, allowing you to get both the QuestionID and the Answer to insert into the database in one foreach, rather than doing multiple foreach's etc.

    HTH?


  5. #5
    Non-Member bronze trophy
    Join Date
    Nov 2009
    Location
    Keene, NH
    Posts
    3,760
    Mentioned
    23 Post(s)
    Tagged
    0 Thread(s)
    What I think you're trying to do, and what I think people are trying to say is you've got a hidden input for NOTHING.

    <input name="answers[',$j->id,']" id="questions" class="deal_form"/>

    That's it, just one input. (note I'd put that in an echo and lose the stupid open/close on every line -- but I'm the nut that wants to see <?php and ?> removed from PHP entirely.)

    Then it's just a simple:
    Code:
    $answers = $this->input->post('answers');
    foreach($answers as $key => $data) {
    	$this->model_survey->insert_survey_answers($data,$key);    
    }
    Honestly though -- it looks to me like you're taking something simple and making it needlessly complex; making copies of elements and throwing objects around elements for no good reason.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •