我有一个通过Codeigniter运行的脚本,它从数据库中提取与某个产品相关的图像列表。每个产品的最大图像数是10,但数据库中并不总是这样。因此,我试图从数据库中拉出并完成图像列表,即使它少于10。例如,
图1图2图片3图片4
从数据库。在视图上,我试图仍然显示最后的六个图像(当然有空白数据),所以在页面本身它仍然显示容器。
这是我迄今为止根据其他建议和建议编译的代码。
编辑:我的问题本质上是我怎么能完成所有10个点的图像,即使我从数据库中提取的比上面的例子中引用的少?
From product model…
public function getImages($id) {
//run query
$this->db->from('images_main');
$this->db->where('images_main.product_id',$id);
$this->db->limit(10);
$this->db->order_by('img_order', 'asc');
$query = $this->db->get();
if ($query->num_rows() > 0)
{
return $query->result_array();
}else{
$this->session->set_flashdata( 'message', 'The query was not successful.' );
}
}
从视图… <ul id="sortable">
<?php
$count = count($img);
$max = 10;
$total = $max - $count;
for($t=1; $t<=$total; $t++):
$img[] = $t;
endfor;
print_r($img);
for ($i=1; $i<=10; $i++):
?>
<li class="img_list">
<div class="photo_content">
<div class="thumbnail" id="<?php echo $i; ?>"><img src="/crop.php?w=100&h=100&f=http://www.apriltwentyfive.com/product_images/regular_images/<?php echo $img[$i]['img_name'];?>"/><a href="#" class="delete" id="<?php echo $img[$i]['img_id']; ?>"></a><a href="#" class="zoom" id="<?php echo $img[$i]['img_id']; ?>"></a></div>
<div class="uploads"><input type="text" name="photo_order" size="1" value="<?php echo $img[$i]['img_order']; ?>" readonly="readonly"/></div>
<div class="browse"></div>
</div>
</li>
<?php endfor; ?>
</ul>
根据我对您代码的理解,变量$count
包含来自数据库的条目数。
因此,将for loop
设置为10,而不是将其设置为$count
(或在$img
上使用foreach
)。
您的$total
变量是不是总,而是有多少空白图像填充。有另一个循环
将两个循环放入<li class="img_list">...<li>
标签。
<?php
$count = count($img);
$total = 10;
$difference = $total - $count;
?>
<ul id="sortable">
<?php
// Images from the database
for ($i = 0; $i <= $count - 1; $i++):
?>
<li class="img_list">
<div class="photo_content">
<div class="thumbnail" id="<?php echo $i; ?>"><img src="/crop.php?w=100&h=100&f=http://www.apriltwentyfive.com/product_images/regular_images/<?php echo $img[$i]['img_name'];?>"/><a href="#" class="delete" id="<?php echo $img[$i]['img_id']; ?>"></a><a href="#" class="zoom" id="<?php echo $img[$i]['img_id']; ?>"></a></div>
<div class="uploads"><input type="text" name="photo_order" size="1" value="<?php echo $img[$i]['img_order']; ?>" readonly="readonly"/></div>
<div class="browse"></div>
</div>
</li>
<?php endfor; ?>
<?php
// Blank images
for ($i = 0; $i <= $difference - 1; $i++):
?>
<li class="img_list">
<div class="photo_content">
<div class="thumbnail">
<img src=".../path/to/blank_photo.png" alt="Title">
</div>
</li>
<!-- rest of HTML code -->
<?php endfor; ?>
</ul>
希望对你有帮助。
指出:
- id总是以0开始,因此您应该从0和开始循环
-
<img>
标签必须有alt
属性