douyue1926
douyue1926
2013-04-06 22:42

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

已采纳

Ok here is my model, which returns everything to my controller and forwarded to my view. I get the correct row output but I also get this strange undefined offset error.

model

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;           
}

view

    <?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; ?>

The webpage is displaying my data correctly but then it shows this undefined output error

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

Here is the lines causing the error:

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条回答

  • dongtui0650 dongtui0650 8年前

    At your function you must count results.. Currently at your view you count fields not results.. Add 1 data key counts and use it in the view..

    ................................................
                        $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;           
        }
    

    at view

    <?php $dataNumRows = isset($optimum['counts']) ? $optimum['counts']:0; ?>
    
    点赞 评论 复制链接分享
  • duanquan1207 duanquan1207 8年前

    your count($optimum) gives you the count of the fields (it's a hashmap containing arrays like $optimum = array("x" => array(1,2), "y" => array(1,2)) and so on) not the columns. You either have to change the way you append items to your array or change the way the counter works. count($optimum["Firstname"] might do the trick)

    点赞 评论 复制链接分享

相关推荐