开发者

Query return only one row of data

Am creating a web app in codeigniter, it retrieves posts from the database for a user based on the keywords they have added to their keyword list. Am trying to retrieve all the posts with the users keywords but,it returns only one post whereas there are mutliple matches for the keywords in the posts

This is are the functions from my model

This function retrieves the users keywords

function user_keywords($user_id) {
         //Get all the user keywords
         $q = $this->db->select('keywords')
                        ->from('keywords')
                        ->where('U_id',$user_id)
                        ->get();
        $words = $q->result_array();
        return $words;
     }

This function retrieves the开发者_如何学C posts

function get_latest_pheeds() {
        $this->load->helper('date');
         $time = time();
         $keyword = $this->user_keywords($this->ion_auth->user_id);
         foreach($keyword as $word) {
         $q = $this->db->select('user_id,pheed_id,datetime,pheed,COUNT(pheed_comments.comment_id) as comments')
         ->from('pheeds')
         ->join('pheed_comments','pheed_comments.P_id=pheeds.pheed_id','left')
         ->like('pheed',$word['keywords'])
         ->order_by('datetime','desc')
         ->get();
         }
         $rows = $q->result_array();
         return $rows;
     }

And my controller encodes it in JSON

function latest_pheeds() {
            if($this->isLogged() == true) {
            $this->load->model('pheed_model');
            $data = $this->pheed_model->get_latest_pheeds();
            echo json_encode($data);
            }
            return false;
    }

I really appreciate the help


I think (but it is hard without testing) that the problem comes form the function get_last_feeds :

you query is built over each loop's iteration, but you fetch the results after. I think you get the results only for the last query (if you don't fetch the results on each loop, the next iteration overwrites the original request without fetching it's results).

I would make something like this :

<?php
function get_latest_pheeds() {
     $rows = array(); // The final result, an array of rows
    $this->load->helper('date');
     $time = time();
     $keyword = $this->user_keywords($this->ion_auth->user_id);
     foreach($keyword as $word) {
     $q = $this->db->select('user_id,pheed_id,datetime,pheed,COUNT(pheed_comments.comment_id) as comments')
     ->from('pheeds')
     ->join('pheed_comments','pheed_comments.P_id=pheeds.pheed_id','left')
     ->like('pheed',$word['keywords'])
     ->order_by('datetime','desc')
     ->get();
     $rows[] = $q->result_array();
     }

     return $rows;
 }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜