简体   繁体   中英

PHP - CI : How to Combine 2 arrays from few tables in databases?

I am trying to combine 2 arrays which I made it from few tables in my database. I am coding this using CodeIgniter and I am still a newbie in this field.

I tried to make this procedure in 1 model (create_table), where there are 2 functions that creates the arrays that I want to combine (moneymarketp1 and moneymarketp2). Then I made a third coding (gabMM1toMM2) which combines the two arrays from the different function.

The problem is when I tried to load it from my controller, it gives a null value and there are error massages that say "undefined Index".

I am still not clear why this happen. Where did I do wrong? Maybe I can have some input.

This my model:

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']
                );
            }

        }
    }


}

}

This my Controller:

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


}

Thanks for your input !!

I think your problem is you forgot to return the combined array. Add return $mmgab to your gabMM1toMM2 method.

public function gabMM1toMM2() {

    ...
    ...
    ...

    //Return the combined array
    return $mmgab;
}

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