for循环中的代码点火器连接

时间:2017-01-27 09:40:20

标签: codeigniter-3

我有一个像这样的codeigniter代码。

for ($i=0;$i<count($acbox);$i++)
{
    $query=$this->db->get('test');
    $queryresult=$query->row_array();
    $emailid=$emailid. ',' . $queryresult['email_id'];
}

这里我在for循环中连接所有电子邮件ID。我正在寻找的是避免连接任何电子邮件,如果它已经存在于$ emailid变量中。

Right now i am getting output like this.
$emailid=test@test.com,test@test.com,test@test.com,hai@hai.com,hai@hai.com
Expected output
$emailid=test@test.com,hai@hai.com

请帮助

2 个答案:

答案 0 :(得分:0)

如果您尝试将emails放入变量测试此解决方案中:

$this->db->select('email_id');         //select only field that we need it
$this->db->group_by('email_id');       //this is important to avoid from duplicating
$this->db->order_by('email_id','asc'); //sort options
$this->db->limit(100);                 //select only 100 rows (optional)

$results = $this->db->get('test')->result();

$emailid = "";
foreach($results as $row)
{
    $emailid = $emailid . ',' . $row->email_id;
}

答案 1 :(得分:0)

示例:

$data ='';
$query=$this->db->get('table_name');
foreach($query->result() as $row)
{
    $data .=$row->email.',';

}
return $data;