Limit number of lines displayed in a table by wordpress (php


Limit number of lines displayed in a table by wordpress (php(

我在wordpress中有一个表,它越来越长。

我想设置一个限制,所以只显示最近250个结果,或者只显示最近30天的结果。更简单的。

此代码来自Wordpress联盟管理插件

    <p><?php printf(__( 'Displaying %1$d of %2$d impressions', 'wpam' ), count($this->viewData['impressions']), $this->viewData['impressionCount']); ?></p>
    <table class="widefat">
        <thead>
        <tr>
             <th width="25"><?php _e( 'ID', 'wpam' ) ?></th>
             <th width="200"><?php _e( 'Date Occurred', 'wpam' ) ?></th>
             <th width="100"><?php _e( 'Creative', 'wpam' ) ?></th>
             <th><?php _e( 'Referrer', 'wpam' ) ?></th>
        </tr>
        </thead>
        <tbody>
        <?php
        $creativeNames = $this->viewData['creativeNames'];
        foreach ( $this->viewData['impressions'] as $impression ) {
        ?>
        <tr class="impression">
            <td><?php echo $impression->impressionId?></td>
            <td><?php echo date("m/d/Y H:i:s", $impression->dateCreated)?></td>
            <td><?php echo $creativeNames[$impression->sourceCreativeId]?></td>
            <td><?php echo $impression->referer?></td>
        </tr>
        <?php } ?>
        </tbody>
    </table>
    <?php
     if ( ! count( $this->viewData['impressions'] ) ):
    ?>
         <div class="daterange-form"><p><?php _e( 'No records found for the date range selected.', 'wpam' ) ?></p></div>
    <?php endif; ?>

您应该检查插件上是否有限制印象数量的选项。如果没有,可以通过用array_slice对数组进行切片来限制它,如下所示:

<?php
    $creativeNames = $this->viewData['creativeNames'];
    $impressions = array_slice($this->viewData['impressions'], 0, 250);
    foreach ( $impressions as $impression ) {
?>
<tr class="impression">
    <!-- your code -->
</tr>
<?php } ?>

如果你只需要最后250个,你可以把它和array_reverse:结合起来

$impressions = array_slice(array_reverse($this->viewData['impressions']), 0, 250);