dongzhu6900 2013-12-06 21:48
浏览 145
已采纳

在foreach循环中输出计数数字以显示正确的行号

What's the best practice to output the each row's real counter from an array ? I have the object array of retrieved users from database that I'am echoing into the <table />

<tbody>
    <?php $i = 1;?>
    <?php foreach($members as $member):?>
    <tr>
        <td class="column-counter">
            <?php echo $i;?>
        </td>
        <td class="column-check">
            <input type="checkbox" class="checkbox" name="checked[]" value="<?php echo $member->id;?>" <?php if(intval($member->id) === intval($this->session->userdata('login_status')['id'])):?>disabled<?php endif;?>>
        </td>
        <td class="column-username">
            <?php echo $member->username;?>
        </td>
        <td class="column-email">
            <a href="#send-email"><?php echo $member->email;?></a>
        </td>
        <td class="column-id">
            <?php echo $member->id;?>
        </td>
    </tr>
    <?php $i++;?>
    <?php endforeach;?>
</tbody>

What I've done so far is that $i =1 incrementing each time the loop is triggered.

But the problem is that if I go to the second page, it starts from "1" again, instead of let's say 21 (in case it shows 20 rows per page).

How can I make it right so it will continue counting from last row of previous page ?

By the way I'am using a codeigniter if that helps.

==== UPDATE ====

The model models/members_model.php i'am using in controllers/members.php to retrieve the users holds this function mixed with pagination:

public function members($data = array(), $return_count = FALSE){

    $this->db
        ->select('
            members.id,
            categories.title as role,
            members.catid as role_id,
            members.firstname,
            members.lastname,
            members.username,
            members.email,
            members.status,
            members.image
        ')
        ->join('categories', 'members.catid = categories.id');

    if( empty($data) ){
        // If nothing provided, return everything
        $this->get_members();
    }else{
        // Grab the offset
        if( !empty($data['page']) ){
            $this->db->offset($data['page']);
        }

        // Grab the limit
        if( !empty($data['items']) ){
            $this->db->limit($data['items']);
        }

        if( $return_count ){
            return $this->db->count_all_results($this->_table_name);
        }else{
            return $this->db->get($this->_table_name)->result();
        }
    }

}
  • 写回答

1条回答 默认 最新

  • doudou5421 2013-12-06 22:15
    关注

    It sounds like $data['page'] holds the information you need since this is related to the specified offset. You didn't reference the name of this array in your global scope, so I will just call this $data_array in my answer.

    You can determine the starting value for $i as follows

    $i = $data_array['page'] + 1;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)