1

I have this php code

 <div class="form-group box">
                <div class="col-lg-7">
                    <?php 
                    foreach ($results as $row) {
                    echo '<select class="form-control" required name="article[]">
                    <option value="'.$row->articles_id.'">'.$row->article_name.'</option>';

                     $sql="SELECT * FROM articles WHERE article_active = 1";
                        $query = $this->db->query($sql);
                        $articles = $query->result();
                        foreach ($articles as $row){ 
                        echo'
                        <option value="'.$row->articles_id.'">'.$row->article_name.'</option>';
                        } 
                        echo'</select>';
                    }
                    ?>
                </div>

                <div class="col-lg-3">
                    <?php foreach ($results as $row) { 
                    echo '<input class="form-control kolicina" type="text" value="'.$row->order_qty.'" name="qty[]" '.$disabled.' required/>'; } 
                    ?>
                    </div>
                    <div class="col-lg-2">
                    <?php foreach ($results as $row) { 
                    echo'<button type="button" class="btn btn-danger button-remove">Remove</button>';
                    }
                    ?>

                    </div>
            </div>

When display this i got this HTML code

<div class="col-lg-7">
    <select class="form-control" required="" name="article[]">
        <option value="10">Cipiripi</option>
        <option value="8">Koka Kola</option>
        <option value="10">Cipiripi</option>
    </select>
    <select class="form-control" required="" name="article[]">
        <option value="8">Koka Kola</option>
        <option value="8">Koka Kola</option>
        <option value="10">Cipiripi</option>
    </select>
</div>
<div class="col-lg-3">
    <input class="form-control kolicina" value="44" name="qty[]" required="" type="text">
    <input class="form-control kolicina" value="44" name="qty[]" required="" type="text">
</div>
<div class="col-lg-2">
    <button type="button" class="btn btn-danger button-remove">Remove</button>
    <button type="button" class="btn btn-danger button-remove">Remove</button>
</div>

I have tried a lot ways with loop, to get proper code, this is example hos html should look?

<div class="form-group box">
    <div class="col-lg-7">
        <select class="form-control" required="" name="article[]">
            <option value="10">Cipiripi</option>
            <option value="8">Koka Kola</option>
            <option value="10">Cipiripi</option>
        </select>
    </div>
    <div class="col-lg-3">
        <input class="form-control kolicina" value="44" name="qty[]" required="" type="text">
    </div>
    <div class="col-lg-2">
        <button type="button" class="btn btn-danger button-remove">Remove</button>
    </div>
</div>

<div class="form-group box">
    <div class="col-lg-7">
        <select class="form-control" required="" name="article[]">
            <option value="8">Koka Kola</option>
            <option value="8">Koka Kola</option>
            <option value="10">Cipiripi</option>
        </select>
    </div>
    <div class="col-lg-3">
        <input class="form-control kolicina" value="44" name="qty[]" required="" type="text">
    </div>
    <div class="col-lg-2">
        <button type="button" class="btn btn-danger button-remove">Remove</button>
    </div>
</div>

What i need that loops goes in different wrapper, not in in same col?

This is working fiddle how it has to look http://jsfiddle.net/ckqth4a7/

And this is what i have now http://jsfiddle.net/q0njhgfk/

4
  • 2
    You use $row in both foreach loops. Try two non-clashing names... Commented Sep 23, 2014 at 0:04
  • This is just example, to show what i need :) Commented Sep 23, 2014 at 0:07
  • 2
    Actually, I think the issue is that he has the divs outside the foreach loop. I think you need to put them all in the same loop. Commented Sep 23, 2014 at 0:07
  • @TanjaPakovic: It was not really an answer but more a guideline: don't use the same variablename for two separate things. It will eventually clash and generate a lot of trouble. Commented Sep 23, 2014 at 0:10

1 Answer 1

2

No, just ditch that inner foreach loops inside just build it by the whole row.

Something like:

<?php foreach($results as $row): ?> <!-- loop this as a whole row -->
 <div class="form-group box">
    <div class="col-lg-7">
        <select class="form-control" required="" name="article[]">
        <?php
        // this is the only loop for the options inside the select
        $sql = "SELECT * FROM articles WHERE article_active = 1";
        $query = $this->db->query($sql);
        $articles = $query->result();

        ?>
        <?php foreach($articles as $article): ?>
            <option value="<?php echo $article->articles_id; ?>"><?php echo $article->article_name; ?></option>
        <?php endforeach; ?>
        </select>

    </div>

    <div class="col-lg-3">
        <input class="form-control kolicina" value="<?php echo $row->order_qty; ?>" name="qty[]" required="" type="text">
    </div>
    <div class="col-lg-2">
        <button type="button" class="btn btn-danger button-remove">Remove</button>
    </div>              
</div>
<?php endforeach; ?>

Or maybe, if the query is just standing there and will not be dynamic take it off the parent loop.

<?php
// take this outside, its always the same anyway, so that it queries only once
// this is the only loop for the options inside the select
$sql = "SELECT * FROM articles WHERE article_active = 1";
$query = $this->db->query($sql);
$articles = $query->result();
?>

<?php foreach($results as $row): ?> <!-- loop this as a whole row -->
 <div class="form-group box">
    <div class="col-lg-7">
        <select class="form-control" required="" name="article[]">
            <?php foreach($articles as $article): ?>
                <option value="<?php echo $article->articles_id; ?>"><?php echo $article->article_name; ?></option>
            <?php endforeach; ?>
        </select>                    
    </div>

    <div class="col-lg-3">
        <input class="form-control kolicina" value="<?php echo $row->order_qty; ?>" name="qty[]" required="" type="text">
    </div>
    <div class="col-lg-2">
        <button type="button" class="btn btn-danger button-remove">Remove</button>
    </div>              
</div>
<?php endforeach; ?>
Sign up to request clarification or add additional context in comments.

9 Comments

@Charles actually i was thinking of ditching the select every time it loops inside the parent loop, i think thats much of a overhead since the query is still the same
Yeah the "SELECT" can definitely be taken out the loop, but I think the op should get the gist by now though. You can still be an angel and edit it for him, lol.
This way html is correct, but i got two same results in SELECT?
@TanjaPakovic so then you query is dynamic?
And you forgotet this one foreach ($results as $row) { <option value="'.$row->articles_id.'">'.$row->article_name.'</option>';
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.