注意(8):未定义的索引:id

时间:2012-06-05 19:15:01

标签: php html arrays indexing echo

我正在努力让视图发挥作用。我的模特是:

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

我收到“Notice(8):未定义索引:id [APP / View / LocalClocks / index.ctp,第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-&gt; _schema = array()代码呢,我也需要它。

我正在使用cakephp,我是新手。另外,我所拥有的代码来自我正在开展的同一个大项目的不同部分。

由于

2 个答案:

答案 0 :(得分:2)

像这样:

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

IIRC,cakephp将Model名称作为数组中的第一个元素,因此您需要先访问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'
    )
)

答案 1 :(得分:1)

您可以将视图更改为:

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

此方法使用提取,该提取从数组键/值分配变量。这不是最理想的,但可行。