PHP foreach, if 语句

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2115300/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 05:05:32  来源:igfitidea点击:

PHP foreach, if statement

php

提问by Kevin Brown

I have a table listing several elements, and their expiration date.

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

I need a function that says, "If the expiration date is later than today (for each element), then don't show".

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

It seems pretty simple, but I'm stuck!

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



Edit

编辑

<?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;?>

回答by Ikke

This is a very basic example.

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

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

回答by Trav L

Seperate your concerns will help you see your question clearer.

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

In your controller:

在您的控制器中:

<?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;
    }
}
?>

In your view:

在您看来:

<?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;?>

回答by Sampson

Of course depending on what your expiration format is, the comparison below would change:

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

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

回答by Ignacio Vazquez-Abrams

Here's something quick and probably non-working, but it'll give you an idea:

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

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

回答by aslum

<?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;?>