简体   繁体   中英

get column with the same value on the database using codeigniter

Here are my resources first

在此处输入图像描述

As you can see on my database I have a Referal_Code and Referral_Link

Now what I am trying to here is I want to get all those Referral_Link that has the same value on the Referal_Code

I know I am no where near to what I am trying to do but I am trying my best really to get this done. Here what I have tried so far

On my model

public function get_same_referral_code()
{
   $this->db->select('referal_code');
   $this->db->where('referal_code', 'PoVsw0Epne');
   $query = $this->db->get('investor');
   if ($query->num_rows() > 0) {
       foreach ($query->result() as $row) {
            $data[] = $row;
       }
       return $data;
    }
}

and on my controller

public function get_all()
{
    $data = array();
    $data['title'] = "Rewards";
    $data["data"] = $this->investor->get_all_investor();
    $data["get_all_flevel"] = $this->investor->get_all_first_level();
    $data["referrals"] = $this->investor->get_same_referral_code();
    $this->template->load('default_layout','contents','rewards',$data);
}

and I am calling it on my view like this

<?php 

    echo ($referrals[0][id]);
    echo ($referrals[0][firstname]);
    echo ($referrals[0][referal_code]);
    echo ($referrals[0][referral_link]);

?>

What my expected result should be like this

在此处输入图像描述

Could someone help me.

EDIT

I've tried again this way to get what I want.

First I tried to get all the referal_code

public function get_same_referral_code()
    {
        $this->db->select('referal_code');
        $query = $this->db->get('investor');
        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $data[] = $row;
            }
            return $data;
        }
    }

and now I will going to use this method get_same_referral_code() to check if referral_link has the same value on the referal_code

public function get_same_referral_link()
    {
        $this->db->select('referral_link');
        $this->db->where('referral_link','get_same_referral_code();');
        $query = $this->db->get('investor');
        if($query->num_rows() > 0)
        {
            foreach($query->result() as $row)
            {
                $data[] = $row;
            }
            return $data;
        }
    }

but the problem is that I got this error now

Message: Invalid argument supplied for foreach()

i think sub query will help you

$SQL= "SELECT * FROM tableName WHERE referal_code EXISTS (SELECT DISTINCT Referral_Link FROM tableName)";
$query = $this->db->query($SQL);
return $query->result_array();

or

$SQL= "SELECT * FROM investor WHERE referral_link IN (SELECT DISTINCT referal_code FROM investor)";
$query = $this->db->query($SQL);
return $query->result_array();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM