dongsu7049 2014-10-17 13:47
浏览 42

通过wordpress限制表格中显示的行数(php(

I have a table in wordpress which keeps being longer and longer.

I would like to set a limit, so only the last 250 results are shown or only the last 30days. Whatever simpler.

This code come from the Wordpress Affiliate Manager Plugin

    <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; ?>
  • 写回答

1条回答 默认 最新

  • duan19750503 2014-10-17 13:53
    关注

    You should check if there's an option on the plugin for limiting the impressions number. If there isn't, you can limit it by slicing the array with array_slice, like this:

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

    If you need only the last 250, you can combine it with array_reverse:

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

报告相同问题?

悬赏问题

  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 个人网站被恶意大量访问,怎么办
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制