0

我正在使用此代码获取最新事件:

   <?php
$all_events = array();
$ten_events = array();
for($i = 0; $events = mysql_fetch_object($events_resource); $i++){
if($i < 5){
  $ten_events[] = $events;
}
$all_events[] = $events;
}  
?>

<?php foreach($ten_events as $events){ ?>

<tr>
<td class="row_<?PHP echo $i % 2; ?>"><?php echo $events->date; ?></td>
<td class="row_<?PHP echo $i % 2; ?>"><?php echo $events->category; ?></td>
<td class="row_<?PHP echo $i % 2; ?>"><?php echo $events->who; ?></td>
<td class="row_<?PHP echo $i % 2; ?>"><?php echo $events->location; ?></td>
</tr>

<?php } ?>

现在我有这段代码让它最终工作,但我不知道如何将它与上面的代码结合起来:

<?PHP
$i = 0;
while($row = @mysql_fetch_row($result)){
?>

<?PHP
$i++;
}
?>

CSS 类“row_0”和“row_1”已准备就绪。

.row_0 {
background-color:        #424140;
padding: 2px;
color: #f3f2ea;
}
.row_1 {
background-color:        #555352;
padding: 2px;
color: #f3f2ea;
}

希望您能够帮助我。

4

1 回答 1

2
<?php
$all_events = array();
$ten_events = array();

for($i = 0; $events = mysql_fetch_object($events_resource); $i++){

    if($i < 5){

        $ten_events[] = $events;

    }

    $all_events[] = $events;

}  

$i = 0;

foreach($ten_events as $event){ 

    $row = $i % 2;

    echo "<tr>
            <td class='row_{$row}'>{$event->date}</td>
            <td class='row_{$row}'>{$event->category}</td>
            <td class='row_{$row}'>{$event->who}</td>
            <td class='row_{$row}'>{$event->location}</td>
          </tr>";

    $i++;

} 
?>

我会这样。

于 2012-08-14T23:12:07.713 回答