严重性:通知消息:试图获取非对象代码点火器的属性

时间:2018-11-19 19:09:31

标签: php mysql codeigniter codeigniter-3

我的控制器:

function generateallmonthreport()
    {
        $month = $this->input->post('month');
        $year = $this->input->post('year');
        $familyid = $this->session->userdata('family_id');

        $fulldata['dates'] = $this->Itemreport_model->getreportallmonth($month,$year);
        $fulldata['members1'] = $this->Itemreport_model->getindependentmembers($familyid);
        $fulldata['members2'] = $this->Itemreport_model->getindependentmembers($familyid);
        $fulldata['members3'] = $this->Itemreport_model->getindependentmembers($familyid);
        $fulldata['members4'] = $this->Itemreport_model->getindependentmembers($familyid);
        $fulldata['members5'] = $this->Itemreport_model->getindependentmembers($familyid);
        $fulldata['year'] = $year;
        $fulldata['month'] = $month;

        $this->load->view('header');
        $this->load->view('itemreports/allmonthreportpage', $fulldata);
        $this->load->view('footer');
    }

我的模型:

function getreportallmonth($month,$year)
{
    $sql = "SELECT DISTINCT `item_date` FROM `items` WHERE MONTH(`item_date`)='$month' AND YEAR(`item_date`)='$year'";
    $query = $this->db->query($sql);
    return $query->result();
}

function getindependentmembers($familyid)
{
    $sql = "SELECT * FROM `familymember` WHERE `family_id`='$familyid' AND `dependency`='1'";
    $query = $this->db->query($sql);
    return $query->result();
}

function getcountofexpensetype($memberid,$year,$month)
{
    $sql = "SELECT COUNT(DISTINCT `item_expensetype`) AS spancount FROM `items` WHERE `item_purchaseby`='$memberid' AND MONTH(`item_date`)='$month' AND YEAR(`item_date`)='$year' AND `item_expensetype` IS NOT NULL";
    $query = $this->db->query($sql);
    return $query->result();
}

function getexpensetype($memberidforexpensetype,$year,$month)
{
    $sql = "SELECT DISTINCT `item_expensetype` as expensetype FROM `items` WHERE `item_purchaseby`='$memberidforexpensetype' AND MONTH(`item_date`)='$month' AND YEAR(`item_date`)='$year' AND `item_expensetype` IS NOT NULL";
    $query = $this->db->query($sql);
    return $query->result();
}

function getallitems($thisdate)
{
    $sql = "SELECT `item_name`,`item_price`,`item_purchasetype` FROM `items` WHERE `item_date`='$thisdate'";
    $query = $this->db->query($sql);
    return $query->result();
}

function getexpensetypefinal($memberidforexpensetypeprice,$year,$month)
{
    $sql = "SELECT DISTINCT `item_expensetype` as expensetype FROM `items` WHERE `item_purchaseby`='$memberidforexpensetypeprice' AND MONTH(`item_date`)='$month' AND YEAR(`item_date`)='$year' AND `item_expensetype` IS NOT NULL";
    $query = $this->db->query($sql);
    return $query->result();
}

我的视图:

<table border="2" id="tab" style="width:100%" class="text-center m-auto table table-bordered table-hover">

                    <tr>
                        <td rowspan="2">Date</td>
                        <td rowspan="2">Items</td>

                        <!-- php code for getting all individual member header for individual expense types -->

                        <?php foreach ($members1 as $members1) { ?>
                                <?php
                                    $this->load->model('Itemreport_model');
                                    $memberid = $members1->member_id;
                                    $countforcolspan = $this->Itemreport_model->getcountofexpensetype($memberid,$year,$month);
                                ?>

                                <td colspan="<?php foreach ($countforcolspan as $countforcolspan) { echo $countforcolspan->spancount+1; } ?>">
                                    <?php echo $members1->membername."'s Individual Expenses"; ?>
                                </td>

                        <?php } ?>

                        <td rowspan="2">Total Expense On Common</td>

                        <!-- php code for getting all individual member header -->
                        <?php foreach ($members2 as $members2) { ?>
                            <td rowspan="2"><?php echo $members2->membername."'s Total Expense (Individual + Common)"; ?></td>
                        <?php } ?>

                        <td rowspan="2">Total Expense Of Family</td>
                    </tr>

                    <tr>
                        <?php
                            foreach ($members3 as $members3) {
                                $memberidforexpensetype = $members3->member_id;
                                $expensetype = $this->Itemreport_model->getexpensetype($memberidforexpensetype,$year,$month);
                                foreach ($expensetype as $expensetype) { ?>
                                    <td><?php echo $expensetype->expensetype; ?></td>
                                <?php } ?>
                                <td>Total</td>
                            <?php }
                        ?>
                    </tr>

                    <tbody id="tbodydata">

                        <?php foreach ($dates as $dates) { ?>
                            <tr>

                                <td><?php $thisdate = $dates->item_date; echo $thisdate; ?></td>

                                <td><?php
                                    $allitems = $this->Itemreport_model->getallitems($thisdate);
                                    foreach ($allitems as $allitems) {
                                        echo $allitems->item_name; ?> (<?php echo $allitems->item_price; ?>)
                                    <?php }
                                ?></td>

                                <?php foreach ($members4 as $members4) {
                                    $memberidforexpensetypeprice = $members4->member_id;
                                    $countforcolspan = $this->Itemreport_model->getexpensetypefinal($memberidforexpensetypeprice,$year,$month);
                                 } ?>
                            </tr>
                        <?php } ?>

                    </tbody>

代码似乎可以正常工作,但是给了我尝试获取非对象属性的通知

错误:

  

遇到PHP错误   严重程度:注意   消息:试图获取非对象的属性   文件名:itemreports / allmonthreportpage.php行号:68

     

回溯:   文件:D:\ wampserver \ www \ home \ application \ views \ itemreports \ allmonthreportpage.php   线:68   功能:_error_handler

     

文件:D:\ wampserver \ www \ home \ application \ controllers \ Itemreport.php   线:91   功能:查看

     

文件:D:\ wampserver \ www \ home \ index.php   线:315   功能:require_once

我无法发现代码的不正确部分。.请帮助.. !!

1 个答案:

答案 0 :(得分:0)

如注释中所示,不能像foreach ($members4 as $members4)中那样使用保存数组的变量来保存值。它会覆盖$members4中保存的值,这会使循环执行您意想不到的事情。

要解决此问题,只需使用其他变量,例如$member,如下所示:

 <?php foreach ($members4 as $member) {
     $memberidforexpensetypeprice = $member->member_id;
     $countforcolspan = $this->Itemreport_model->getexpensetypefinal($memberidforexpensetypeprice,$year,$month);
 } ?>