PHP foreach, if ifadesi

4 Cevap

Ben birkaç elemanları, ve son kullanma tarihini listeleyen bir tablo var.

Ben "son kullanma tarihi (her eleman için) sonra bugün daha fazla ise, o zaman görünmüyor" diyor bir işlev gerekir.

Bu oldukça basit görünüyor, ama ben takıldım!


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

4 Cevap

Endişelerinizi size soru daha net görmek yardımcı olacaktır ayırın.

Sizin denetleyicisi:

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

Senin görünümünde:

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

Bu çok temel bir örnektir.

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

Tabii ki son formatı ne bağlı olarak, aşağıdaki karşılaştırma değiştirmek istiyorsunuz:

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

Burada hızlı ve muhtemelen çalışmayan bir şey, ama bu size bir fikir vereyim:

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