2

I have user_sport_tbl,sport_tbl tables. in user_sport_tbl i have sport_id but not have sport name.Sport name is in sport_tbl.

i have a function of getUserSport() of Model.

my query :-

$query = $this->db->get_where('user_sport_tbl',array('username' => $username));

i want sport_name for every sport_id and want to merge sport_name in array.

my code :-

    public function getUserSport()
{
    $username = $this->session->userdata('username');
    $query = $this->db->get_where('user_sport_tbl',array('username' => $username));
    if($query->num_rows > 0)
    {
        foreach($query->result() as $item)
        {
            $query1 = $this->db->query("select sport_name from sport_tbl where sport_id=$item->sport_name order by sport_id limit 1");
            $sql1 = $query1->row();

            $data[] = array_push($item, $sql1->sport_name);
        }
        return $data;
    }
}

my present return data:-

 [user_match_id] => 7 [sport_id] => 2 [match_id] => 3 [username] => admin

i want to add [sport_name] => cricket.

I want to return an array with sportname

2 Answers 2

1

Try this coding

public function getUserSport()
{
   $data = array();
    $username = $this->session->userdata('username');
    $query = $this->db->get_where('user_sport_tbl',array('username' => $username));
    if($query->num_rows > 0)
    {
        foreach($query->result() as $key=>$item)
        {
            $query1 = $this->db->query("select sport_name from sport_tbl where sport_id=$item->sport_name order by sport_id limit 1");
            $sql1 = $query1->row();

            $item[$key]['sport_name']  = $sql1->sport_name;

            $data[] = $item;
        }
        return $data;
    }
}
Sign up to request clarification or add additional context in comments.

14 Comments

i have got this error: Fatal error: Cannot use object of type stdClass as array in /Applications/XAMPP/xamppfiles/htdocs/game/application/models/user_model.php on line 166
what is 166 line ? result() replace to result_array(); and $item->sport_name replace to $item['sport_name']; then working fine
166 line is : $item[$key]['sport_name'] = $sql1->sport_name;
result() replace to result_array(); and $item->sport_name replace to $item['sport_name']; then working fine
i am getting this error Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING) in /Applications/XAMPP/xamppfiles/htdocs/game/application/models/user_model.php on line 147 on Line 147 code: $query1 = $this->db->query("select sport_name from sport_tbl where sport_id=$item['sport_name'] order by sport_id limit 1");
|
0

Try This

public function getUserSport()
{
    $username = $this->session->userdata('username');
    $query = $this->db->get_where('user_sport_tbl',array('username' => $username));
    if(empty($query))
    {
        foreach($query->result_array() as $item)
        {
            $sportsId = $item[0]['sport_name'];
            $query1 = $this->db->query("SELECT sport_name FROM sport_tbl WHERE sport_id = $sportsId ORDER BY sport_id LIMIT 1");
            $result = $query1->result_array();

            $data[] = array_push($item, $result[0]['sport_name']);
        }
        return $data;
    }
}

Or can do with join query too (Not tested. If any error fix it. or post the message her)

public function getUserSport()
{
    $username = $this->session->userdata('username');
    $query = $this->db->query(
        "SELECT user_sport_tbl.sport_name, sport_tbl.sport_name
        FROM user_sport_tbl 
        INNER JOIN sport_tbl 
        ON user_sport_tbl.username = sport_tbl.sport_name
        WHERE user_sport_tbl.username = '$username' ORDER BY sport_tbl.sport_id LIMIT 1");
    $result = $query->result_array();

    return $data;
}

2 Comments

@ManishTiwari check this out
print_r($query->result_array()) and check

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.