Welcome Guest, Not a member yet? Register   Sign In
Loop inside a loop in controller.. help needed....:(
#1

[eluser]Roy MJ[/eluser]
Hi guys,

Can anyone help me solve this problem. Im having one table members which contain details of registered people. And another table friends linking the ids to make them friends. the table structure is as follows :

Code:
Table friends

id     member_id     friend     status
1     12             17     0
9     13             17     0
3     12             29     0

Bu i need to display the friends details as in any social networking site. Like name, photo etc. But all that contents are in another table called members. Hoe can i connect the two tables together in the controller part..

The existing controller for displayin frnds is as follows :


Code:
function index($pgoffset='')
    {
        //$this->data['service'] = $this->Services_model->select_services();
        $this->data['pagetitle'] ='Friends List';
        if(($this->session->userdata('user_id'))!=NULL)
        {
            $row    =      $this->Profile_model->get_selected($this->session->userdata('user_id'));
            $this->data['username'] = $row->username;
            $id =  $row->id;
        }
        $config['per_page'] = 20;
        $config['total_rows'] = $this->Friends_model->get_total($id);
        $config['base_url'] = site_url().'/friends/index/';
        $config['uri_segment'] = 3;
        $this->data['friends'] = $this->Friends_model->select_friends($config['per_page'], $pgoffset,$id);
        $this->data['pgoffset'] = $pgoffset;
        $this->pagination->initialize($config);
        $this->load->view('profile/friends', $this->data);
    }
The model :

Code:
function get_total($id)
    {
        $count=0;
        $this->db->select('COUNT(id) AS total');
        $this->db->distinct();
        $this->db->from('friends');
        /*$this->db->where($where);*/
        $this->db->where('member_id', $id);
        $result_total = $this->db->get();
        if($result_total->num_rows()>0){
            $row    = $result_total->row();
            $count    =    $row->total;
        }
        return $count;
    }
   function select_friends($limit, $pgoffset,$id)
   {
           $this->db->select('friend');
           $this->db->limit($limit, $pgoffset);
        $this->db->distinct();
        $this->db->from('friends');
        $this->db->where('member_id', $id);
        $result_news = $this->db->get();
          return $result_news->result();
        //$result_news = $this->db->query('select * from news');
   }

View :
Code:
<?php    foreach($friends as $row){?>
<div id="friends">
<a class="newshead">friend?&gt;">&lt;?php echo $row->friend?&gt;</a><br />
</div>                    
&lt;?php } ?&gt;
<div>&lt;?php echo $this->pagination->create_links(); ?&gt;</div>
#2

[eluser]Roy MJ[/eluser]
But the current code will only help me display all the friends ids. I need a way of getting all the details of the ids as friends from the members table.
The structure for table members is as follows:

Code:
id firstname lastname screenname username password city state phone zip email    
11 Roy          MJ     roy       zxcv    zxcv        tvm     india 123   123 [email protected]                             1

The id in the members table and the field friend in friends table are the same.
#3

[eluser]Roy MJ[/eluser]
Ive tried out join function and an error is logging in.. The model is as follows:

Code:
function get_total($id)
    {
        $count=0;
        $getid = $id;
        $this->db->select('COUNT(friends.id) AS total');
        $this->db->distinct();
            $this->db->from('friends');
        $this->db->join('members','friends.member_id ='. $getid);
        $result_total = $this->db->get();
        if($result_total->num_rows()>0){
            $row    = $result_total->row();
            $count    =    $row->total;
        }
        return $count;
    }

The error is :

Code:
A Database Error Occurred

Error Number: 1054

Unknown column '13' in 'on clause'

SELECT DISTINCT COUNT(friends.id) AS total FROM (`friends`) JOIN `members` ON `friends`.`member_id` =`13`

I cant figure out why the error. 13 is actually the id thats getting passed.. Anyone..??
#4

[eluser]Roy MJ[/eluser]
figured out.. but not sure this is the actual way.. Tongue

Code:
function get_total($id)
    {
        $count=0;
        $this->db->select('COUNT(friends.id) AS total');
        $this->db->distinct();
        $this->db->from('friends');
        $this->db->join('members','friends.member_id');
        $this->db->where('friends.member_id =', $id);
        $this->db->where('friends.status =', 1);
        $result_total = $this->db->get();
        if($result_total->num_rows()>0){
            $row    = $result_total->row();
            $count    =    $row->total;
        }
        return $count;
    }
   function select_friends($limit,$pgoffset,$id)
   {
           $this->db->select('members.id,members.screenname,members.photo,members.email');
           $this->db->limit($limit,$pgoffset);
        $this->db->distinct();
        $this->db->from('members');
        $this->db->join('friends','friends.member_id');
        $this->db->where('friends.member_id =', $id);
        $this->db->where('friends.status =', 1);
        $result_news = $this->db->get();
          return $result_news->result();
   }
#5

[eluser]Roy MJ[/eluser]
apparently the above code is nt workin properly. Its listing all contents from the members table.. anyone figure out a way around this??
#6

[eluser]InsiteFX[/eluser]
See if this will work for you.
Code:
$this->db->join('members','friends.member_id', 'left');

Also try putting your where causes above your joins.

InsiteFX
#7

[eluser]Roy MJ[/eluser]
No its still listing all the contents from the table friends. The condition that
Code:
$this->db->where('friends.member_id =', $id);
is not taking. One more thing, is there a way of printing the model query as in controller..?? so that i can check how the query is being excecuted..??...

I have removed the condition $this->db->where('friends.status =', 1); also. But still no change...Sad
#8

[eluser]Roy MJ[/eluser]
I was thinking of a way where i can call anothr function in the model but im still a starter in codeigniter and i dun have the depth in this.

In controller
Code:
$this->data['friends']=$this->Friends_model->select_friends($config['per_page'],$pgoffset,$id);

And the model for this wud be to just get friends id from table friends.

So that the details of friends can be got from another function in model. But that means ill have to pass the data as loop in the controller.

Like

Code:
foreach($this->['friends']){
$this->data['friends_details']=$this->Friends_model->select_friends_details(friends->$friend);
}

This is just an idea am i dunno whether its correct. Im a bit afraid cuz ill have to change the entire thing and the due date is up.. Sad




Theme © iAndrew 2016 - Forum software by © MyBB