PHP - CI:如何从数据库中的几个表组合2个数组?

时间:2014-12-04 10:30:17

标签: php mysql arrays codeigniter

我正在尝试将我从数据库中的几个表中创建的2个数组组合起来。我使用CodeIgniter进行编码,我仍然是这个领域的新手。

我尝试在1个模型(create_table)中创建此过程,其中有2个函数创建我想要组合的数组(moneymarketp1和moneymarketp2)。然后我做了第三个编码(gabMM1toMM2),它结合了不同函数的两个数组。

问题是当我尝试从我的控制器加载它时,它给出一个空值,并且有错误按摩说“未定义索引”。

我仍然不清楚为什么会这样。我哪里做错了?也许我可以有一些意见。

这是我的模特:

class create_table extends CI_Model{
public function __construct() {
    parent::__construct();
    $this->load->database();
}


public function moneymarketp1() {
    $query = $this->db->query('SELECT a.counterparty, b.plafond, c.maturity, '
            . 'b.1wk_rate, b.1mo_rate,b.3mo_rate,'
            . 'IFNULL(d.available,(b.plafond-0)) AS unused_facility '
            . ' FROM riskassessment.mm_counterparty_desc a'
            . ' INNER JOIN riskassessment.mm_plafon_counterparty b ON b.counterparty=a.code'
            . ' INNER JOIN riskassessment.mm_bank_line c on c.code=a.code AND c.update_date=b.date'
            . ' LEFT JOIN riskassessment.mm_borrowing_outstanding_counterparty_idr d on d.counterparty=a.code AND d.instrument="Call Money"'
            );
    $datatemp = array();
    foreach ($query->result_array() as $row) {
        $datatemp[]=array(
            'counterparty'=>$row['counterparty'],
            'plafond'=>$row['plafond'],
            'maturity'=>$row['maturity'],
            '1wk_rate'=>$row['1wk_rate'],
            '1mo_rate'=>$row['1mo_rate'],
            '3mo_rate'=>$row['3mo_rate'],
            'unused_facility'=>$row['unused_facility']    
        );
    }
    return array('moneymarketp1'=>$datatemp);



}

public function moneymarketp2() {
    $query = $this->db->query('SELECT a.code,a.counterparty, '
            . 'CASE WHEN e.ssclass=0101001 THEN e.face_value ELSE 0 END AS outstanding_idr,'
            . 'CASE WHEN e.ssclass=0102001 THEN e.face_value ELSE 0 END AS outstanding_usd,'
            . 'CASE WHEN e.ssclass=0101001 THEN e.maturity ELSE 0 END AS facility_maturity,'
            . 'CASE WHEN e.ssclass=0101001 THEN e.rate ELSE 0 END AS facility_rate,'
            . '(e.maturity - curdate()) as tenor,'
            . 'CASE WHEN a.code=f.counterparty THEN f.current_rate ELSE 0 END AS avg_current_rate,'
            . 'g.1mo_rate AS avg_1mo_rate, g.3mo_rate AS avg_3mo_rate'
            . ' FROM riskassessment.mm_counterparty_desc a'
            . ' INNER JOIN riskassessment.mm_borrowing e ON e.counterparty=a.code AND e.input_date = (SELECT e1.input_date
                                                                                                    FROM riskassessment.mm_borrowing e1
                                                                                                    WHERE e1.counterparty = e.counterparty
                                                                                                    ORDER BY e1.input_date DESC
                                                                                                    LIMIT 1)'
            . ' INNER JOIN riskassessment.mm_weighted_av_rate_current f ON f.counterparty=a.code'
            . ' INNER JOIN riskassessment.mm_weighted_av_rate_weeklymonthly g ON g.counterparty=a.code'
            );
    $datatemp = array();
    foreach ($query->result_array() as $row) {
        $datatemp[]=array(
            'code'=>$row['code'],
            'counterparty'=>$row['counterparty'],
            'outstanding_idr'=>$row['outstanding_idr'],
            'outstanding_usd'=>$row['outstanding_usd'],
            'facility_maturity'=>$row['facility_maturity'],    
            'facility_rate'=>$row['facility_rate'],    
            'tenor'=>$row['tenor'],    
            'avg_current_rate'=>$row['avg_current_rate'],    
            'avg_1mo_rate'=>$row['avg_1mo_rate'],    
            'avg_3mo_rate'=>$row['avg_3mo_rate']    
        );
    }
    return array('moneymarketp2'=>$datatemp);
}

public function gabMM1toMM2() {
    $mm1 = $this->moneymarketp1();
    $mm2 = $this->moneymarketp2();
    $mmgab = array();
    foreach ($mm1 as $value1) {
        foreach ($mm2 as $value2) {
            if($value1['counterparty']===$value2['counterparty']){
                $mmgab[]=array(
                    'counterparty'=>$value1['counterparty'],
                    'plafond'=>$value1['plafond'],
                    'maturity'=>$value1['maturity'],
                    '1wk_rate'=>$value1['1wk_rate'],
                    '1mo_rate'=>$value1['1mo_rate'],
                    '3mo_rate'=>$value1['3mo_rate'],
                    'unused_facility'=>$value1['unused_facility'], 
                    'outstanding_idr'=>$value2['outstanding_idr'],
                    'outstanding_usd'=>$value2['outstanding_usd'],
                    'facility_maturity'=>$value2['facility_maturity'],    
                    'facility_rate'=>$value2['facility_rate'],    
                    'tenor'=>$value2['tenor'],    
                    'avg_current_rate'=>$value2['avg_current_rate'],    
                    'avg_1mo_rate'=>$value2['avg_1mo_rate'],    
                    'avg_3mo_rate'=>$value2['avg_3mo_rate']
                );
            }

        }
    }


}

}

这是我的控制器:

public function showMoneyMarket_gabtable() {
    $this->load->model('create_table');
    $table1 = $this->create_table->gabMM1toMM2();
     echo json_encode($table1);


}

感谢您的投入!!

1 个答案:

答案 0 :(得分:1)

我认为您的问题是您忘记返回组合数组。将return $mmgab添加到您的gabMM1toMM2方法。

public function gabMM1toMM2() {

    ...
    ...
    ...

    //Return the combined array
    return $mmgab;
}