我有表数据,它从foreach获取数据并通过for循环循环。循环 3 次,与 foreach 的值不同)我想知道我怎样才能在最后<td>
放一个词?
我的代码;
<?php
foreach($halls_all_array AS $row){?>
<td nowrap class="auto-style34" style="height: 30px; width: 20%" align="left">
time1-<?php echo format_time($row[$start]['time'])?>
</td>
<?php } ?>
foreach($halls_all_array AS $row){
for($j=$start;$j<$limit;$j++){ ?>
<td nowrap class="auto-style34" style="height: 30px; width: 20%" align="left"> time1-<?php echo format_time($row[$j]['time'])
if($j==($limit-1)){ echo "word";}
?>
</td>
<?php
}
}
?>
视图
<?php foreach(... ) { ?>
<td>loop td</td>
<?php } ; ?>
使用 :
if($j == ($limit - 1)) {
// last <td> here. You can add the text here.
}
编辑
<?php
$count = count($halls_all_array);
$i = 0;
foreach($halls_all_array AS $row){
?>
<td nowrap class="auto-style34" style="height: 30px; width: 20%" align="left">time1-<?php echo format_time($row[$start]['time'])?>
<?php
if(++$i === $count){
//echo 'WORD TO PRINT';
}
?>
</td>
<?php
}
?>
尝试以下操作
首先检查是否定义了$halls_all_array和$start
<?php
$count=count($halls_all_array);
$i=1;
foreach($halls_all_array AS $row){
echo'<td nowrap class="auto-style34" style="height: 30px; width: 20%" align="left">time1-'.format_time($row[$start]['time']);
if($i == $count){
//echo 'WORD TO PRINT';
}
echo'</td>';
$i++;
}
?>
试试这段代码
<?php
$length = count($halls_all_array); // TAKE THE LENGTH OF ARRAY
$i = 1;
foreach($halls_all_array as $row){ ?>
<td nowrap class="auto-style34" style="height: 30px; width: 20%" align="left">
<?php if($i < $length){ ?>
time1- <?php echo format_time($row[$start]['time']);?>
<?php }else{
echo "YOUR WORD..."; // Last Looping..
} ?>
</td>
<?php
$i++; // INCREMENT THE COUNTER
} ?>
首先从数组中获取最后一个键
end($halls_all_array); // move the internal pointer to the end of the array
$last_key = key($halls_all_array);// fetches the key of the element pointed to by the internal pointer
试试这段代码:
<?php
/*get the last key*/
end($halls_all_array);
$last_key = key($halls_all_array);
foreach($halls_all_array as $key => $row){?>
<td nowrap class="auto-style34" style="height: 30px; width: 20%" align="left">
time1-<?php echo format_time($row[$start]['time'])?>
</td>
<?php if ($last_key == $key): ?>
<td><?php echo $row ?></td>
<?php endif ?>
<?php } ?>
请编写如下代码:
<?php
$counter = 0; // start a counter
$total_count = count($halls_all_array); //total Counts
foreach($halls_all_array AS $row){?>
<td nowrap class="auto-style34" style="height: 30px; width: 20%" align="left">
time1-<?php echo format_time($row[$start]['time'])?>
<?php if($total_count==$counter) echo 'word'; ?> // you condition for last and word
</td>
<?php
$counter++;
}
?>