【问题标题】:Notice (8): Undefined index: id注意(8):未定义索引:id
【发布时间】:2012-06-05 19:15:01
【问题描述】:

我正在尝试让视图正常工作。我的模型是:

class LocalClock extends AppModel 
{
    public $useDbConfig = 'default';
    public $useTable = 'local_clocks';
    //public $useTable = false;
public function setStatType( $type )
{
    //$this->table = 'local_clocks';
    $this->_schema = array(
        'col1' => array('type' => 'int')
        ,'col2' => array('type' => 'string')
        ,'col3' => array('type' => 'string')
        ,'col4' => array('type' => 'string')
        ,'col5' => array('type' => 'string')
        ,'col6' => array('type' => 'string')
        ,'col7' => array('type' => 'string')
        ,'col8' => array('type' => 'string')
        ,'col9' => array('type' => 'string')
        ,'col10' => array('type' => 'string')
        ,'col11' => array('type' => 'string')
        ,'col12' => array('type' => 'string')
        ,'col13' => array('type' => 'string')
        ,'col14' => array('type' => 'string')
    );   
}

}

控制器是这样的:

class LocalClocksController extends AppController 
{
    public $scaffold = 'admin';     // allow basic view/edit for administrators
    public $components = array('RequestHandler');   // enable checking for incoming request attributes

   public $helpers = array('Html', 'Form');
public function index() 
{
if ($this->RequestHandler->accepts('xml'))
{
    $this->LocalClock->table = 'local_clocks';
    $this->LocalClock->getDataSource()->tableFields['local_clocks'] = array( "id", "name", "auto_offset", "utc_offset_sec", "in_month", "in_week", "in_day", "in_hour", "out_month", "out_week", "out_day", "out_hour", "offset_sec");
        // xml handler
    $this->set('localClocks', $this->LocalClock->find('all'));
    $this->set('_serialize', array('local_clocks'));
    }
    elseif ($this->RequestHandler->accepts('json'))
    {
    $this->LocalClock->getDataSource()->tableFields['local_clocks'] = array( "id", "name", "auto_offset", "utc_offset_sec", "in_month", "in_week", "in_day", "in_hour", "out_month", "out_week", "out_day", "out_hour", "offset_sec");
        $this->set('localClocks', $this->LocalClock->find('all'));
        $this->set('_serialize', array('local_clocks'));
    }
elseif( $this->RequestHandler->accepts('html'))
{
    $this->LocalClock->table = 'local_clocks';
    $this->LocalClock->getDataSource()->tableFields['local_clocks'] = array( "id", "name", "auto_offset", "utc_offset_sec", "in_month", "in_week", "in_day", "in_hour", "out_month", "out_week", "out_day", "out_hour", "offset_sec");
        $this->set('localClocks', $this->LocalClock->find('all'));
}       
}

}

这是视图:

<p><marquee><u>Local Clocks</u></marquee></p>

<table>
<thead><tr>
<th>Id</th>
<th>Name</th>
<th>Auto Offset</th>
<th>UTC Offset Sec</th>
<th>In Month</th>
<th>In Week</th>
<th>In Day</th>
<th>In Hour</th>
<th>Out Month</th>
<th>Out Week</th>
<th>Out Day</th>
<th>Out Hour</th>
<th>Offset Sec</th>
<th>Actions</th>
</tr></thead>
<tbody>

<?php 
foreach($localClocks as $LocalClock) { ?>

<tr>
<td><?php echo $LocalClock['id']; ?></td>

<td><input type="button" value="View"/><input type="button" value="Edit"/><input type="button" value="Delete"/></td>
</tr>

<?php } ?>

</tbody>
</table>
<?php debug($LocalClock); ?>

我收到“通知 (8): Undefined index: id [APP/View/LocalClocks/index.ctp, line 37]”错误

这是 debug($LocalClock) 的输出(视图文件中 foreach 循环的 LocalClock

array(
    'LocalClock' => array(
        'id' => '4',
        'name' => 'New Test Clock',
        'auto_offset' => false,
        'utc_offset_sec' => '9',
        'in_month' => '8',
        'in_week' => '7',
        'in_day' => '6',
        'in_hour' => '5',
        'out_month' => '4',
        'out_week' => '3',
        'out_day' => '2',
        'out_hour' => '1',
        'offset_sec' => '0'
    )
)

任何有关如何正确显示 id 的帮助都会很棒。 我还尝试显示其他属性,例如自动偏移、utc_offset_sec、in_month 等,但为简单起见,我将这些属性排除在视图文件之外。

另外,在顶部的模型文件中,我真的需要函数 setStateType($type) 吗? 如果我确实需要这个函数,那么 $this->_schema = array() 代码呢,我也需要那个吗?

我正在使用 cakephp,而且我是新手。此外,我拥有的代码来自我正在从事的同一个大项目的不同部分。

谢谢

【问题讨论】:

    标签: php html arrays indexing echo


    【解决方案1】:

    像这样:

    $LocalClock['LocalClock']['id'];
    

    IIRC,cakephp 将模型名称作为数组中的第一个元素,因此您需要首先访问 LocalClock

    您的调试显示:

    array(
        'LocalClock' => array(
    //This One ^
            'id' => '4',
            'name' => 'New Test Clock',
            'auto_offset' => false,
            'utc_offset_sec' => '9',
            'in_month' => '8',
            'in_week' => '7',
            'in_day' => '6',
            'in_hour' => '5',
            'out_month' => '4',
            'out_week' => '3',
            'out_day' => '2',
            'out_hour' => '1',
            'offset_sec' => '0'
        )
    )
    

    【讨论】:

      【解决方案2】:

      您可以将视图更改为:

      <p><marquee><u>Local Clocks</u></marquee></p>
      
      <table>
      <thead><tr>
      <th>Id</th>
      <th>Name</th>
      <th>Auto Offset</th>
      <th>UTC Offset Sec</th>
      <th>In Month</th>
      <th>In Week</th>
      <th>In Day</th>
      <th>In Hour</th>
      <th>Out Month</th>
      <th>Out Week</th>
      <th>Out Day</th>
      <th>Out Hour</th>
      <th>Offset Sec</th>
      <th>Actions</th>
      </tr></thead>
      <tbody>
      
      <?php 
      foreach($localClocks as $LocalClock) { 
      extract($LocalClock);
      ?>
      
      <tr>
          <td><?php echo $LocalClock['id']; ?></td>
      
          <td><input type="button" value="View"/><input type="button" value="Edit"/><input type="button" value="Delete"/></td>
      </tr>
      
      <?php } ?>
      
      </tbody>
      </table>
      <?php debug($LocalClock); ?>
      

      此方法使用提取,它从数组键/值中分配变量。这不是最理想的,但会起作用。

      【讨论】:

        猜你喜欢
        • 2015-12-01
        • 2013-11-27
        • 2015-06-02
        • 2021-09-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-05-25
        相关资源
        最近更新 更多