2011-11-10 50 views
0

上午從檢索通過Ajax調用數據庫的帖子,並與jQuery和JSON顯示他們,我想實現一個負載更多的功能,讓用戶看到更多內容而無需刷新頁面。 在我試圖在這個我硬編碼的JavaScript函數使ajax請求的偏移量,所以函數不斷加載相同的職位。 這就是笨控制器加載更多功能

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

您可以使用全局JavaScript變量並將計數存儲在那裏,以便每次點擊「加載更多」。對於每個ajax調用,將計數作爲查詢字符串發送到服務器。將計數值作爲參數傳遞給您的函數。使用該計數值參數,您可以查詢數據庫中的「更多帖子」。

順便說一下,「get_latest_pheeds」函數做了什麼?

+0

它從數據庫中檢索帖子 – MrFoh