2010-01-22 9 views
0

몇 가지 요소와 만료 날짜가 나열된 테이블이 있습니다.PHP foreach, if 문

"만료일이 오늘보다 늦으면 (각 요소에 대해) 표시하지 않음"이라는 기능이 필요합니다.

아주 간단하게 보입니다.하지만 막혀 있습니다!

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

지금까지 테이블을 표시하도록 코드가 무엇입니까? – Amber

+0

어쩌면 지금까지 가지고있는 것을 보여줘야 할 것입니다. 테이블에 의해 당신은 데이터베이스 테이블을 의미한다고 가정합니다 - 당신은 어떻게 그것을 당신의 스크립트에 저장하고 있습니까? 일반적으로 사람들은 최소한 세부 정보를 표시 할 수 없다면 코드를 작성하지 않을 것입니다. – Erik

+0

코드로 업데이트! :) –

답변

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

사이드 노트 : 오늘은'strtotime()'을 사용할 필요가 없습니다. 오늘 밤 자정을 지킬 것을 주장한다면'mktime()'을 사용할 수 있으므로 문자열 파싱의 필요성을 피할 수 있습니다. 그러나 날짜를 비교하는 것이므로 날짜가 더 작거나 클 것이므로 현재 시간이 포함되는지는 중요하지 않습니다. 따라서이 경우에는'time()'이 충분했을 것입니다. –

+0

맞습니다. 필요하지 않으므로 중복됩니다. 사이드 노트 주셔서 감사. –

1

, 아래의 비교는 바꿀 것 아이디어를 줄 것입니다 :

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

여기에 신속하고 아마 비 작업 일,하지만 :


을 편집 만료 형식은 내용에 따라 물론

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

아주 기본적인 예입니다.

foreach($elements as $element) 
{ 
    if(strtotime($element['expiration_date']) < now()) 
    { 
     echo $element['expiration_date']; 
    } 
} 
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;?>