0

I'm having a problem at the moment where I have a column called rating in the links table and there is definitely values other than 0 within the column but 0 is the only value which is returned foreach link. When I do a simple get for that column it then shows all the other values but not when I do an SQL Join.

I know the problem is my joining of the tables but I'm unsure how I would go about joining these specific tables.

Database Table Structure

The rating column is the one which is causing me problems.

'links' id | title | url | user_id | list_id | rating | weight | date_created

'list' id | list_title | list_description | user_id | rating | views | date_created

'link_ratings' id | user_id | link_id | rated | date_created

Model:

public function get_latest(){

    $this->db->limit(100);
    $this->db->order_by('links.date_created', 'DESC');
    $this->db->select('*');
    $this->db->select('links.id as current_link_id');
    $this->db->from('links');
    $this->db->join('list', 'links.list_id = list.id'); 
    $this->db->join('users', 'links.user_id = users.id');
    $this->db->join('link_ratings', 'links.id = link_ratings.link_id','left');      

    $get_latest = $this->db->get();

    return $get_latest;

}

Any Help is appreciated.

2 Answers 2

1

You should try this:

function get_latest(){
    $this->db->select('list.*, users.*, links.id as current_link_id');
    $this->db->from('links');
    $this->db->join('list', 'links.list_id = list.id'); 
    $this->db->join('users', 'links.user_id = users.id');
    $this->db->join('link_ratings', 'links.id = link_ratings.link_id','left');      
    $this->db->order_by('links.date_created', 'DESC');
    $this->db->limit(100);
    $get_latest = $this->db->get()->result_array(); #fetch all rows here
    echo "<pre>";print_r( $get_latest );die; #print all rows and see if its fetching ratings corrctly or not.
    echo $this->db->last_query();die; #check the query generated
    return $get_latest;
}
Sign up to request clarification or add additional context in comments.

5 Comments

I maybe wasn't clear. I am retrieving data but it is the rating column within the links table which is causing me problems. Foreach time I loop through in my view it simply shows 0 for the rating index rather than the numbers which are in the database for that specific row. My joins are the problem.
Any ideas on how I should construct my joins?
Have you seen the resultant array structure you are getting? see above and try it once.
Yes I've already had a look and it shows all the correct keys/columns but link rating values are all set to 0. Any suggestions on what join queries may work?
Any idea what it could be?
0

The reason will be purely logical, in that the join will be causing no results to be returned because there are no results. I've fallen into this many times.

I am not able to diagnose your particular problem but when faced with issues like this I:

1- turn on the CI profiler

2- var_dump the array so you can see what's going on

3- write a traditional SQL query and run it in PHPMyAdmin

One, or a combination of all three, will enable you to diagnose.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.