PHP如何修复这个奇怪的UNDEFINED OFFSET错误?

时间:2013-04-06 22:42:13

标签: php codeigniter

好的,这是我的模型,它将所有内容返回给我的控制器并转发到我的视图。 我得到了正确的行输出,但我也得到了这个奇怪的未定义偏移误差。

模型

function getAllCustomersPendingInstall()
{
    $data=array();
    //Need to use sessions to display proper 
    //records for each user. Temp set id to user #7
    $id = 7;

    //query the db and return all record where SalesRepId == $id
    $query = $this->db->get_where('customers', array('SalesRepId' => $id));

        //check logic, if rows exist RETURN all rows, else
        //return message that no pending installs is available.
        if($query->num_rows >= 0) {
            foreach($query->result() as $row) {
                $data['cFirstName'][] = $row->customerFirstName;
                $data['cLastName'] [] = $row->customerLastName;
                $data['cId'][] = $row->customerId;
                $data['cStatus'][] = $row->customerStatus;
                $data['cDateSold'][] = $row->customerDateSold;
                $data['cCorp'][] = $row->customerCorp;
                $data['cAccount'][] = $row->customerAccount;
                $data['cAddress'] [] = $row->customerAddress;
                $data['cAptNum'][] = $row->customerCity;
                $data['cState'][] = $row->customerState;
                $data['cZip'][] = $row->customerZip;
                $data['cEmail'][] = $row->customerEmail;
                $data['cHomePhone'][] = $row->customerHomePhone;
                $data['cCellPhone'][] = $row->customerCellPhone;
                $data['cInstallDate'][] = $row->customerInstallDate;
                $data['cInstallTime'][] = $row->customerInstallTime;    
                $data['cServiceLevelSold'][] = $row->customerServiceLevelSold;
                $data['cWinBackFrom'][] = $row->customerWinBackFrom;
                $data['cSaleSource'][] = $row->customerSaleSource;
                $data['cTier'][] = $row->customerTier;      
                $data['mySalesRepId'][] = $row->SalesRepId;
            }
        } else {
            $data = "No pending installs available!";
            return $data;
        }   
//the following var_dump is only showing the last record.
//need to show all rows (which should be 2)
//var_dump($data); exit;
return $data;           
}

视图

    <?php $dataNumRows = count($optimum); ?>

    <?php for ($i = 0; $i < $dataNumRows; $i++) : ?>
        <?php echo $optimum['cFirstName'][$i]; ?>
        <?php echo $optimum['cLastName'][$i]; ?>
        <?php echo $optimum['cId'][$i]; ?>
        <?php echo $optimum['cStatus'][$i]; ?>
        <?php echo $optimum['cDateSold'][$i]; ?>
        <?php echo $optimum['cCorp'][$i]; ?>
        <?php echo $optimum['cAccount'][$i]; ?>
        <?php echo $optimum['cAddress'][$i]; ?>
        <?php echo $optimum['cAptNum'][$i]; ?>
        <?php echo $optimum['cState'][$i]; ?>
        <?php echo $optimum['cZip'][$i]; ?>
        <?php echo $optimum['cEmail'][$i]; ?>
        <?php echo $optimum['cHomePhone'][$i]; ?>
        <?php echo $optimum['cCellPhone'][$i]; ?>
        <?php echo $optimum['cInstallDate'][$i]; ?>
        <?php echo $optimum['cInstallTime'][$i]; ?> 
        <?php echo $optimum['cServiceLevelSold'][$i]; ?>
        <?php echo $optimum['cWinBackFrom'][$i]; ?>
        <?php echo $optimum['cSaleSource'][$i]; ?>
        <?php echo $optimum['cTier'][$i]; ?>    
        <?php echo $optimum['mySalesRepId'][$i]; ?>
        <br>
    <?php endfor; ?>

网页正确显示我的数据,但随后显示此未定义的输出错误

Tom Jones 1  4   02/11/2013  77  895482  1823 e15th St   Brooklyn    NY  11268   tones728@verizon.net    0000000000  0000000000  02/16/2013  1   11  1   1   1   7 
Alexa Planter 2  4   02/13/2013  77  632584  5847 w79th Ave  Brooklyn    NY  11258   aplanter222@gmail.com   0000000000  0000000000  02/22/2013  1   31  1   1   2   7 
A PHP Error was encountered

Severity: Notice

Message: Undefined offset: 2

Filename: views/welcome_message.php

Line Number: 29

A PHP Error was encountered

Severity: Notice

Message: Undefined offset: 2

Filename: views/welcome_message.php

Line Number: 30

.... repeated to line 49

以下是导致错误的行:

29| <?php echo $optimum['cFirstName'][$i]; ?>
30| <?php echo $optimum['cLastName'][$i]; ?>
..| <?php echo $optimum['cId'][$i]; ?>
<?php echo $optimum['cStatus'][$i]; ?>
<?php echo $optimum['cDateSold'][$i]; ?>
<?php echo $optimum['cCorp'][$i]; ?>
<?php echo $optimum['cAccount'][$i]; ?>
<?php echo $optimum['cAddress'][$i]; ?>
<?php echo $optimum['cAptNum'][$i]; ?>
<?php echo $optimum['cState'][$i]; ?>
<?php echo $optimum['cZip'][$i]; ?>
<?php echo $optimum['cEmail'][$i]; ?>
<?php echo $optimum['cHomePhone'][$i]; ?>
<?php echo $optimum['cCellPhone'][$i]; ?>
<?php echo $optimum['cInstallDate'][$i]; ?>
<?php echo $optimum['cInstallTime'][$i]; ?> 
<?php echo $optimum['cServiceLevelSold'][$i]; ?>
<?php echo $optimum['cWinBackFrom'][$i]; ?>
<?php echo $optimum['cSaleSource'][$i]; ?>
..| <?php echo $optimum['cTier'][$i]; ?>    
49| <?php echo $optimum['mySalesRepId'][$i]; ?>

2 个答案:

答案 0 :(得分:2)

在您的功能中,您必须计算结果..目前在您的视图中,您计算​​的字段不是结果..添加1个数据密钥计数并在视图中使用它。

................................................
                    $data['cTier'][] = $row->customerTier;      
                    $data['mySalesRepId'][] = $row->SalesRepId;
                }
                $data['counts'] = $query->num_rows;
            } else {
                $data = "No pending installs available!";
                return $data;
            }   
    //the following var_dump is only showing the last record.
    //need to show all rows (which should be 2)
    //var_dump($data); exit;
    return $data;           
    }

在视图

<?php $dataNumRows = isset($optimum['counts']) ? $optimum['counts']:0; ?>

答案 1 :(得分:0)

你的计数($ optimal)给你字段的数量(它是一个包含数组的散列图,如$ optimal = array(“x”=&gt; array(1,2),“y”=&gt; array(1) ,2))等等)不是列。您必须更改将项目附加到阵列的方式或更改计数器的工作方式。 count($ optimal [“Firstname”]可能会做到这一点)