You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

从MySQL批量生成电费账单时视图循环报错求助

解决CodeIgniter批量生成电费账单时的" Trying to get property 'designation' of non-object"错误

错误信息

Severity: Notice
Message: Trying to get property 'designation' of non-object
错误文件:facture/multiple_fact.php第44行

错误根源分析

  1. 控制器中$data['facture']每次循环被覆盖,未累加为账单数组集合,导致视图遍历的是单个元素而非批量数据
  2. 视图错误地将数组元素当作对象访问($fact->designation),但控制器传递的是关联数组
  3. 部分数据库查询可能无结果,导致变量未初始化,最终传递给视图的数组存在缺失键
  4. 视图误用set_value()函数(该函数用于表单验证回显,此处直接输出变量即可)
  5. 控制器循环内重复加载视图,逻辑错误

一、修复控制器代码

public function facturation()
{
    $data = array();
    $data['facture'] = array(); // 初始化批量账单数组
    $this->db->select('*');
    $this->db->from('client');
    $query = $this->db->get();

    foreach ($query->result() as $clients) {
        $query_date = date('Y-m-d');
        $date_act = date('Y-m-01', strtotime($query_date));

        $number = round(microtime(true)*100);
        $code = 'FHT-'.$number;

        // 初始化所有变量,避免查询无结果时出现未定义错误
        $index_act2 = 0;
        $index_react2 = 0;
        $date_p2 = '';
        $fact_ene = 0;
        $fact_puis = 0;
        $p_max = 0;
        $p_sous = 0;
        $clientid = $clients->id_client;
        $client = '';
        $mois_conc = '';
        $index_act = 0;
        $index_react = 0;
        $date_p1 = '';
        $fact_index = 0;
        $coef_mult = 0;
        $tva = 0;

        // 查询当前月份参数
        $this->db->select('
            prise_releve.client_id,
            index_actif,
            puissance_max,
            puissance_souscrit,
            index_reactif,
            facteur_u_energie,
            facteur_u_puissance,
            date_prise,
            MONTH(date_prise) as mois_concerne,
            designation
        ');
        $this->db->where('date_prise', $date_act);
        $this->db->where('prise_releve.client_id', $clients->id_client);
        $this->db->from('prise_releve');
        $this->db->join('client','client.id_client = prise_releve.client_id');
        $this->db->join('contrat','client.id_client = contrat.client_id');
        
        $query_prise = $this->db->get(); // 重命名变量,避免覆盖原客户查询结果
        if ($query_prise->num_rows() > 0) {
            foreach ($query_prise->result() as $row) { 
                $index_act2 = $row->index_actif;
                $index_react2 = $row->index_reactif;
                $date_p2 = $row->date_prise;
                $fact_ene = $row->facteur_u_energie;
                $fact_puis = $row->facteur_u_puissance;
                $p_max = $row->puissance_max;
                $p_sous = $row->puissance_souscrit; 
                $clientid = $row->client_id;
                $client = $row->designation;
            }
        }

        // 查询上月数据
        $newdate = date("Y-m-d", strtotime ('-1 month' , strtotime($date_act))) ;
        $this->db->select('index_actif, index_reactif, date_prise');
        $this->db->where('date_prise', $newdate);
        $this->db->where('client_id', $clients->id_client);
        $this->db->from('prise_releve');
        $this->db->join('client','client.id_client = prise_releve.client_id');

        $query2 = $this->db->get();
        if ($query2->num_rows() > 0) {
            foreach ($query2->result() as $row) { 
                $index_act = $row->index_actif;
                $index_react = $row->index_reactif;
                $mois_conc = utf8_encode(ucwords(strftime("%B", strtotime($row->date_prise))));
                $date_p1 = $row->date_prise; 
            }
        }

        // 查询FI参数(简化写法)
        $query3 = $this->db->get_where('parametrage', ['libelle' => "FI"]);
        if ($query3->num_rows() > 0) {
            $fact_index = $query3->row()->valeur;
        }

        // 查询CM参数
        $query4 = $this->db->get_where('parametrage', ['libelle' => "CM"]);
        if ($query4->num_rows() > 0) {
            $coef_mult = $query4->row()->valeur;
        }

        // 查询TVA参数
        $query5 = $this->db->get_where('parametrage', ['libelle' => "TVA"]);
        if ($query5->num_rows() > 0) {
            $tva = $query5->row()->valeur;
        }

        // 计算账单
        $constant = 0.65;
        $p_exp = ($fact_index*$constant*($fact_puis/100)*$coef_mult);
        $e_exp = ($fact_index*($fact_ene/100));
        $e_exp = number_format($e_exp, 5, '.', ',');
        $p_exp = number_format($p_exp, 5, '.', ',');

        $cons_act = (($index_act2-$index_act)*$coef_mult);
        $cons_react = (($index_react2-$index_react)*$coef_mult);
        $cons_act11 = $index_act2-$index_act;
        $cons_react11 = $index_react2-$index_react;
        $cons_act = number_format($cons_act, 2, '.', ',');
        $cons_react = number_format($cons_react, 2, '.', ',');

        $cos_phi= ($cons_act11/(sqrt(($cons_act11*$cons_act11)+($cons_react11*$cons_react11))));
        $cons_phi = number_format($cos_phi, 2, '.', ' ');

        $pena = 0;
        if($cons_phi >= 0.95){
            $pena = 0;
        } elseif($cons_phi>= 0.85 && $cons_phi< 0.95){
            $pena = (0.5*(0.95-$cons_phi));
        } elseif($cons_phi >= 0.60 && $cons_phi< 0.85){ // 修复语法错误
            $pena = (0.5+1*(0.85-$cons_phi));
        } elseif($cons_phi>= 0.55 && $cons_phi< 0.60){
            $pena = (0.3+2*(0.60-$cons_phi));
        } elseif($cons_phi>= 0.5 && $cons_phi< 0.55){
            $pena = (0.4+2.5*(0.55-$cons_phi));
        } elseif($cons_phi< 0.50){
            $pena = (0.5+3*(0.50-$cons_phi));
        }

        $pf = 0;
        if($p_max > 1.05*$p_sous){
            $pf = ($p_max+(0.5*($p_max-$p_sous)));
        } elseif($p_max>= 0.85*$p_sous && $p_max<= 1.05*$p_sous){
            $pf = $p_max;
        } elseif($p_max< 0.85*$p_sous){
            $pf = 0.85*$p_sous;
        }

        $mnt_redev1 = ($pf*$p_exp);
        $cons_act1 = (($index_act2-$index_act)*$coef_mult);
        $mnt_redev = number_format($pf*$p_exp, 2, '.', ',');
        $mnt_energie1 = ($cons_act11*$e_exp);
        $mnt_energie = number_format($mnt_energie1*$coef_mult, 2, '.', ',');
        $mnt_energie2 = $mnt_energie1*$coef_mult;
        $mnt_total = $mnt_redev1+$mnt_energie2;
        $mnt_total2 = $mnt_redev1+$mnt_energie2;
        $total_tva = (($mnt_total2*$tva)/100)+$mnt_total2;

        // 将当前客户账单添加到批量数组(而非覆盖)
        $data['facture'][] = array(    
            "index_act"=>$index_act,
            "index_react"=>$index_react,
            "index_act2"=>$index_act2,
            "index_react2"=>$index_react2,
            "fact_ue"=>$fact_ene,
            "fact_up"=>$fact_puis,
            "p_max"=>$p_max,
            "p_sous"=>$p_sous, // 修复原代码赋值错误
            "fact_index"=>$fact_index,
            "coef_mult"=>$coef_mult,
            "p_exp"=>$p_exp,
            "e_exp"=>$e_exp,
            "cons_act"=>$cons_act,
            "cons_react"=>$cons_react,
            "cos_phi"=>$cons_phi,
            "pf"=>$pf,
            "pena"=>$pena,
            "mnt_redev"=>$mnt_redev,
            "mnt_energie"=>$mnt_energie,
            "mnt_total"=>$mnt_total,
            "mois_concerne"=>$mois_conc,
            "tva"=>$tva,
            "mnt_tva"=>$total_tva,
            "date_p1"=>$date_p1,
            "date_p2"=>$date_p2,
            "client"=>$client,
            "clientId"=>$clients->id_client,
            "code"=>$code
        );
    }

    // 循环结束后统一加载视图
    $data['titre'] = "Facturation";
    $data['innerdata'] = 'admin/facture/multiple_fact';
    $this->load->view('admin/template', $data);
}

二、修复视图代码

<div class="right_col" role="main">
    <div class="x_panel">
        <div class="col-md-12">
            <div class="x_title">
                <h2>Facturation multiple</h2>
                <ul class="nav navbar-right panel_toolbox">
                    <div class="col-auto">
                        <a href="<?= base_url('cclient/list') ?>" class="btn btn-sm btn-primary btn-icon-split">
                            <span class="icon">
                                <i class="fa fa-list"></i>
                            </span>
                            <span class="text text-light">
                                Liste client
                            </span>
                        </a>
                    </div>
                </ul>
                <div class="clearfix"></div>
            </div>
        </div>

        <div class="x_content">
            <form action="<?= base_url('cfacturation/save'); ?>" method="post" id="form" class="">
                <div><?php $this->load->view('_partials/messages.php') ?></div>
                <div class="row">
                    <div class="col-md-12">
                        <table class="table">
                            <tr>
                                <th>Client</th>
                                <th>Mois</th>
                                <th>Montant facture</th>
                                <th>Energie consommée</th>
                            </tr>

                            <?php if (!empty($facture)): ?>
                                <?php foreach($facture as $key => $fact): ?>
                                    <tr>
                                        <td><input type="text" readonly value="<?= $fact['client']; ?>" class="form-control" name="facture[<?= $key; ?>][client]" /></td>
                                        <td><input type="text" readonly value="<?= $fact['mois_concerne']; ?>" class="form-control" name="facture[<?= $key; ?>][mois]" /></td>
                                        <td><input type="text" readonly value="<?= $fact['mnt_total']; ?>" class="form-control" name="facture[<?= $key; ?>][mnt_total]" /></td>
                                        <td><input type="text" readonly value="<?= $fact['mnt_energie']; ?>" class="form-control" name="facture[<?= $key; ?>][mnt_energie]" /></td>
                                    </tr>
                                <?php endforeach; ?>
                            <?php else: ?>
                                <tr>
                                    <td colspan="4" class="text-center">暂无账单数据</td>
                                </tr>
                            <?php endif; ?>
                        </table>
                    </div>

                    <div class="col-md-12">
                        <hr>
                        <button type="submit" class="btn btn-success">Enregistrer</button>
                        <button type="reset" class="btn btn-danger">Reset</button>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>

额外修复说明

  1. 控制器修复了语法错误:elseif($cons_phi = 0.60) 改为 elseif($cons_phi >= 0.60)
  2. 修正了控制器中p_sous的赋值错误(原代码误写为$p_max)
  3. 视图修复了单引号语法错误:$fact->mnt_redev') 中的多余单引号
  4. 表单name属性改为数组形式,支持批量提交账单数据
  5. 增加了空数据判断,避免遍历空数组导致的异常

内容的提问来源于stack exchange,提问作者wams

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.29 22:02:14