Welcome Guest, Not a member yet? Register   Sign In
result() ? or row()? or what?!
#1

[eluser]Tom Taila[/eluser]
hey guys, im havin a lil bit of a noob moment, so basically i made a lil mini friends system for a website, and i wanna echo out friend requests (if there are any) on a users profile page, now if i have two or more friend requests these requests will be shown with no problem as i know i can use return $query->result() to get a number of rows from my 'friends' table, but if theres only one single request it will not be shown, also i know i can use $query->row() but that would mean if there were more than one request than those wouldnt show, so what do i do? im using a foreach() statement to echo em out, ill show u my codes below:

my user profilepage view file:
Code:
<?php
    if(isset($friend_requests))
        {
            if($friend_requests->num_rows() == 1)
            {
                echo $friend_requests->sender_id . " would like to be your friend.<br/>";
            }
            else
            {
                foreach($friend_requests as $row)
                {
                    echo $row->sender_id . " would like to be your friend.<br/>";
                }
            }
        }
    ?&gt;

my user controller function to load the users own page:
Code:
function own_page()//this function is for loading your own page
    {
        $data = array();
        
        $this->load->model('user_model');
        if($query = $this->user_model->get_current_user_data())
        {
            $data['current_user_data'] = $query;
        }
        if($query2 = $this->user_model->get_friend_requests())
        {
            $data['friend_requests'] = $query2;
        }
        
        $this->load->view('own_page', $data);
    }

and my model:
Code:
function get_friend_requests()
    {
        $this->db->where('reciever_id', $this->session->userdata('id'));
        $this->db->where('pending', 1);
        $query = $this->db->get('friends');
        
        if($query->num_rows() > 1)
        {
            return $query->result();
        }
        else if($query->num_rows() == 1)
        {
            return $query->row();
        }
    }


thanks so much, cheers
#2

[eluser]InsiteFX[/eluser]
Code:
function get_friend_requests()
    {
        $data - array();

        $this->db->where('reciever_id', $this->session->userdata('id'));
        $this->db->where('pending', 1);
        $query = $this->db->get('friends');
        
        if($query->num_rows() > 0)
        {
            $data = $query->row_array();
        }

        $query->free_result();
        return $data;
    }

Here is another way:

Code:
$data = array();

     if ($query->num_rows() > 0)
     {
         foreach ($query->result_array() as $row)
         {
             $data[] = $row;
         }
     }

    $query->free_result();
    return $data;

InsiteFX
#3

[eluser]Tom Taila[/eluser]
wow, awesome, before i had to creat if statments depending on wether i had one result or many, was much longer than ur way, thanks so much Smile




Theme © iAndrew 2016 - Forum software by © MyBB