我有一个显示产品的简单脚本。在我的数据库中,我将date_posted
和end_date
存储为产品表中的字段。在我的主页上,我显示了最近发布的20个产品。
我想展示剩下1天或更长时间的产品。到目前为止,我已经设法做到这一点,但使用前端脚本和CSS,这不是最佳实践,因为如果其中一些产品已经结束,它将不会显示20个产品。
以下是我的脚本:
我的观点=>
<?php $i=0;?>
<?php foreach($product as $p): ?>
<?php
$startDate = date('m/d/Y');
$endDate = $p['endDate'];
$seconds_left = (strtotime($endDate) - strtotime($startDate));
$days_left = floor($seconds_left / 3600 / 24);
?>
<?php $i+= 1; ?>
<td <?php //if($days_left<=0){echo "class='ended'";} ?>>
<a href="<?php echo base_url()."user/product/".$p['productID']; ?>" class="product_name"><?php echo $p['productName']; ?>
</a>
<ul class="nav">
<li><i class="icon-home"> </i> Location : <?php echo substr($p['location'],0,25); ?></li>
<li><i class="icon-tags"> </i> Budget : Tshs <?php echo number_format($p['Price']); ?></li>
<li><i class="icon-tasks"> </i> <?php if($p['CategoryID']==4){echo"Number of rooms: ";}else echo "Quantity :"?> <?php echo $p['quantity']; ?></li>
<li><i class="icon-eye-open"> </i> Bids : <?php echo $p['Bids']; ?></li>
<li><i class="icon-time"> </i> Days Left: <?php echo $days_left; ?></li>
</ul>
</td>
我的模特=&gt;
function get_product($productID){
$this->db->select()->from('products')->where('productID',$productID);
$query = $this->db->get();
return $query->first_row('array');
}
答案 0 :(得分:1)
您需要在where子句中获取日期。 这样的事情可以解决问题:
function get_product($productID){
$this->db->select()
->from('products')
->where('productID',$productID)
->where('end_date >=', date('Y-m-d H:i:s', strtotime('- 1 day')))
->limit(20);
$query = $this->db->get();
return $query->first_row('array');
}