加载更多功能

时间:2011-11-10 00:50:34

标签: php jquery codeigniter

我正在通过ajax调用从数据库中检索帖子并使用jQuery和JSON显示它们,我希望实现更多加载功能,以允许用户在不刷新页面的情况下查看更多帖子。 在我的尝试中,我硬编码了javascript函数中的偏移量,从而产生了ajax请求,因此该函数不断加载相同的帖子。 这是codeigniter控制器

    function latest_pheeds($offset = 0) {
                //Confirm if a user is logged before allowing access
                if($this->isLogged() == true) {
                //Limit
                $limit = 20;
                //user id
                $user_id = $this->session->userdata('user_id');     
                //Load the date helper to calculate time difference between post time and current time
                $this->load->helper('date');
                //Current time(unix timetamp)
                $time = time();
                $pheeds = array();
                    $dt = $this->pheed_model->get_latest_pheeds($limit,$offset);
                    $data = $dt['pheeds'];
                    if($data != false && !empty($data)) {
//total no of pheeds
$total_rows = $dt['total']
                        foreach($data as $pheed) {
                            $row['pheed_id'] = $pheed->pheed_id;
                            $row['user_id'] = $this->user_model->return_username($pheed->user_id);
                            $row['pheed'] = $pheed->pheed;
                            $row['datetime'] = $pheed->datetime;
                            $row['comments'] = $this->comment_model->count_comments($pheed->pheed_id);
                            $row['repheeds'] = $pheed->repheeds;
                            if($this->user_model->is_owner($pheed->pheed_id,$user_id,'pheed'))
                            {
                                $row['owner'] = "yes";
                            }else {
                                $row['owner'] = "no";
                            }
                            if($pheed->avatar == 0)
                            {
                                $row['avatar_src'] = site_url().$this->site_config->get_setting_value('default_avatar_small');
                            }else {
                                $row['avatar_src'] = $pheed->avatar_small;
                            }
                            $pheeds[] = $row;
                        }
                        echo json_encode($pheeds);
                    }
                } else {
                    $this->output->set_status_header('401',"Attempting Unauthorized Access");
                }
            }

我如何确定下一个偏移量以编码到json数组中进行分页,因为我拥有请求的总行数

1 个答案:

答案 0 :(得分:0)

您可以使用全局javascript变量并在每次点击“加载更多”时将计数存储在那里。对于每个ajax调用,将计数作为查询字符串发送到服务器。将计数值作为参数传递给函数。使用该计数值参数,您可以在数据库中查询“更多帖子”。

那么“get_latest_pheeds”函数的作用是什么?