【发布时间】: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