tags:

views:

99

answers:

5

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;?>
+1  A: 

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"];
  }
}
Jonathan Sampson
A: 

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);
  }
}
Ignacio Vazquez-Abrams
+6  A: 

This is a very basic example.

foreach($elements as $element)
{
    if(strtotime($element['expiration_date']) < now())
    {
        echo $element['expiration_date'];
    }
}
Ikke
+1  A: 

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;?>
rockacola
Side note: to get todays time, you don't need to use `strtotime()`. If you insist on getting the time for midnight today, you can use `mktime()`, and thus avoid the necessity for string parsing. But as the comparison is for dates, it shouldn't matter if the current time of day is included as well, as the date will be smaller or larger anyway, so in this case `time()` would've been sufficient.
nikc
You right, that is redundant since it isn't necessary. Thanks for side note.
rockacola
A: 
<?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;?>
aslum