【问题标题】:Load more feature加载更多功能
【发布时间】:2011-12-25 19:15:43
【问题描述】:

我正在通过 ajax 调用从数据库中检索帖子并使用 jQuery 和 JSON 显示它们,并且我想实现加载更多功能,以允许用户在不刷新页面的情况下查看更多帖子。 在我的尝试中,我在发出 ajax 请求的 javascript 函数中硬编码了偏移量,因此该函数不断加载相同的帖子。 这是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 数组中进行分页,因为我有请求的总行数

【问题讨论】:

  • 这个代码太多了,问题好像不够具体。您能否尝试澄清并减少相关部分的代码?

标签: php jquery codeigniter


【解决方案1】:

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

顺便问一下,“get_latest_pheeds”函数是做什么的?

【讨论】:

  • 它从数据库中检索帖子
猜你喜欢
  • 2019-06-02
  • 2019-10-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多