我有这个PHP代码
<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>
显示时我得到了这个HTML代码
<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>
我已经尝试了很多循环方法,以获得正确的代码,这是示例hos html应该看起来?
<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>
我需要的是循环在不同的包装中,而不是在同一个col中?
这是如何工作的小提琴 http://jsfiddle.net/ckqth4a7/
这就是我现在所拥有的 http://jsfiddle.net/q0njhgfk/
答案 0 :(得分:2)
不,只是抛弃内部的foreach循环,只是在整行上构建它。
类似的东西:
<?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; ?>
或许,如果查询只是站在那里而不是动态的,则将其从父循环中取出。
<?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; ?>