在数组中使用foreach

时间:2014-08-13 23:52:38

标签: php codeigniter

我正在尝试使用codeigniter的活动记录类从我需要使用此代码插入表的数据库中获取值

$q = $this->db->get('center_number');
$cd = $this->db->get('running_tasks');`

$ask_for_permission = array(
    'messagefrom' => $le_message_from,
    'messageto' => foreach ($q->result() as $row)
    {
        $row->center_number;
    },
    'messagetext' => 'The dataset'. '' .foreach ($cd->result() as $row)
    {
        $row->task_name;
    }. ' is requesting permission to credit the account.Reply with yes to allow or no to decline.Anything else other than yes or no shall be ignored.'
);

我收到错误:

unexpected 'foreach'

如何从$ask_for_permission数组中获取数据库中的记录?

3 个答案:

答案 0 :(得分:0)

// First Generate the Array
$ask_for_permission = array(
   'messagefrom' => $le_message_from,
   'messageto' => '',
   'messagetext' => 'The dataset'. '' .foreach ($cd->result() as $row)
                    {
                    $row->task_name;
                    }. ' is requesting permission to credit the account.Reply with yes to allow or no to  
                    decline.Anything else other than yes or no shall be ignored.'
    );

// Second, populate it with all values of the $q->result() array
foreach ($q->result() as $row)
                    {
                    $ask_for_permission['messageto'] .= $row->center_number;
                    }

答案 1 :(得分:0)

试试这个

function msgTo() {
    $str = '' ;
    foreach ($r->result() as $row) {
        $str .= $row;
    }
    return $str;
}

并用msgTo()

替换你的foreach

答案 2 :(得分:0)

我这样解决了

    $q = $this->db->get('center_number');

    $le_message_from = '08009898';
    $ask_for_permission = array(
   'messagefrom' => $le_message_from,
   'messageto' => $q->row->center_number,           
   'messagetext' => 'The dataset'. ' '. $this->db->query("select task_name from running_tasks limit 1")->row()->task_name .' '.'is requesting permission to credit the account.Reply with yes to allow or no to  
                    decline.Anything else other than yes or no shall be ignored.'
    );

    $this->db->insert('messageout', $ask_for_permission);