PHP foreach,if语句

时间:2010-01-22 05:41:32

标签: php

我有一个表格列出了几个元素及其到期日期。

我需要一个函数,说“如果到期日期晚于今天(对于每个元素),那么就不要显示”。

看起来很简单,但我被卡住了!


修改

<?php foreach($codes->result_array() as $row):?>
<tr>
    <td><?php print $row['description']?></td>
    <td><?php print $row['credits']?></td>
    <td><?php print $row['code']?></td>
    <td><? echo date("F jS, Y", strtotime($row['exp_date']));?></td>
    <td><? echo date("F jS, Y", strtotime($row['create_date']));?></td>
    <td>
        <!-- Icons -->
         <a href="<? echo base_url()?>admin/home/editCode/<?php print $row['id']?>" title="Edit"><img src="<? echo base_url()?>assets/images/icons/pencil.png" alt="Edit" /></a>
         <a href="<? echo base_url()?>admin/home/deleteCode/<?php print $row['id']?>" title="Delete" class="delete-code"><img src="<? echo base_url()?>assets/images/icons/cross.png" alt="Delete" /></a> 
    </td>
<?php endforeach;?>

5 个答案:

答案 0 :(得分:7)

这是一个非常基本的例子。

foreach($elements as $element)
{
    if(strtotime($element['expiration_date']) < now())
    {
        echo $element['expiration_date'];
    }
}

答案 1 :(得分:1)

当然,根据您的到期格式,下面的比较会发生变化:

foreach ($items as $item) {
  if ($item["expiration"] < $today) {
    print $item["name"];
  }
}

答案 2 :(得分:1)

分开您的疑虑将有助于您更清楚地看到问题。

在您的控制器中:

<?php
$result_array = $codes->result_array();
$results = array();
$today = time();

foreach($codes->result_array() as $row)
{
    if(strtotime($row['exp_date']) <= $today)
    {//-- Keep this
        $results[] = $row;
    }
}
?>

在您看来:

<?php foreach($results as $result): ?>
<tr>
    <td><?php print $result['description']?></td>
    <td><?php print $result['credits']?></td>
    <td><?php print $result['code']?></td>
    <td><? echo date("F jS, Y", strtotime($result['exp_date']));?></td>
    <td><? echo date("F jS, Y", strtotime($result['create_date']));?></td>
    <td>
        <!-- Icons -->
         <a href="<? echo base_url()?>admin/home/editCode/<?php print $result['id']?>" title="Edit"><img src="<? echo base_url()?>assets/images/icons/pencil.png" alt="Edit" /></a>
         <a href="<? echo base_url()?>admin/home/deleteCode/<?php print $result['id']?>" title="Delete" class="delete-code"><img src="<? echo base_url()?>assets/images/icons/cross.png" alt="Delete" /></a> 
    </td>
<?php endforeach;?>

答案 3 :(得分:0)

这是一些快速而且可能不起作用的东西,但它会给你一个想法:

foreach($table as $entry)
{
  if($entry->expdate <= $today)
  {
    showentry($entry);
  }
}

答案 4 :(得分:0)

<?php 
// format this so that it's in the same format as your exp_date field.
$today = mktime(); 

foreach($codes->result_array() as $row):
// If you want it to show expired elements use a < instead of > in the if below.
if ($row['exp_date']>$today) {?>
<tr>
<td><?php print $row['description']?></td>
<td><?php print $row['credits']?></td>
<td><?php print $row['code']?></td>
<td><? echo date("F jS, Y", strtotime($row['exp_date']));?></td>
<td><? echo date("F jS, Y", strtotime($row['create_date']));?></td>
<td>
    <!-- Icons -->
     <a href="<? echo base_url()?>admin/home/editCode/<?php print $row['id']?>" title="Edit"><img src="<? echo base_url()?>assets/images/icons/pencil.png" alt="Edit" /></a>
     <a href="<? echo base_url()?>admin/home/deleteCode/<?php print $row['id']?>" title="Delete" class="delete-code"><img src="<? echo base_url()?>assets/images/icons/cross.png" alt="Delete" /></a> 
</td>
<?php }
else {
// If you want to display a blank table or warning or something, that woudl go here.
} endforeach;?>