Saturday, September 24, 2011

Undefined index Error

I am are building a Hotel application

When I try to list all hotels using Pages controller, and result.ctp
(Viewing page) I am getting an error as:

Undefined index: id [APP/views/pages/result.ctp, line 43]

I am using the same code that works in the baked hotels controller.

Please check the code for Result.ctp:

<div class="hotels index">
<h2><?php __('Hotels');?></h2>
<table cellpadding="0" cellspacing="0">
<tr>
<th><?php echo $this->Paginator->sort('id');?></th>
<th><?php echo $this->Paginator->sort('name');?></th>
</tr>
<?php
$i = 0;
foreach ($hotels as $hotel):
$class = null;
if ($i++ % 2 == 0) {
$class = ' class="altrow"';
}
?>
<tr<?php echo $class;?>>
<td><?php echo $hotel['Hotel']['id']; ?>&nbsp;</td>
<td><?php echo $hotel['Hotel']['name']; ?>&nbsp;</td>
<td>
<?php echo $this->Html->link($hotel['Template']['name'],
array('controller' => 'templates', 'action' => 'view',
$hotel['Template']['id'])); ?>
</td>
<td><?php echo $hotel['Hotel']['terms_of_service']; ?>&nbsp;</td>
<td class="actions">
<?php echo $this->Html->link(__('View', true), array('action' =>
'view', $hotel['Hotel']['id'])); ?>
<?php echo $this->Html->link(__('Edit', true), array('action' =>
'edit', $hotel['Hotel']['id'])); ?>
<?php echo $this->Html->link(__('Delete', true), array('action' =>
'delete', $hotel['Hotel']['id']), null, sprintf(__('Are you sure you
want to delete # %s?', true), $hotel['Hotel']['id'])); ?>
</td>
</tr>
<?php endforeach; ?>
</table>
<p <?php
echo $this->Paginator->counter(array(
'format' => __('Page %page% of %pages%, showing %current% records out
of %count% total, starting on record %start%, ending on %end%', true)
));
?> </p>

<div class="paging">
<?php echo $this->Paginator->prev('<< ' . __('previous', true),
array(), null, array('class'=>'disabled'));?>
| <?php echo $this->Paginator->numbers();?>
|
<?php echo $this->Paginator->next(__('next', true) . ' >>', array(),
null, array('class' => 'disabled'));?>
</div>
</div>

pages_controller.php


<?php //Added for routing static pages like Home, About, Contact Us -
Reference #4 -
class PagesController extends AppController{
public $uses = array('City','Hotel');

function beforeFilter() {
parent::beforeFilter();
$this->Auth->allow(array('*'));
}

public function display() {

$this->Hotel->recursive = 0;
$this->set('hotels', $this->paginate());

// $this->set('citynew', $this->data['City']['city_id']);
//$h = array();
//$h = $this->Hotel->find('list');
//$this->set(compact('h'));
// $this->set('h',$this->Hotel->query('Select * from hotels'));
// $this->set('citynew', $this->data['City']['city_id']);
// $this->set('hotels',$this->Hotel->query("Select a.name from
hotels a where a.city_id='" . $citynew . "'"));


//$sql=("Select a.name from hotels a where a.city_id='" .
$citynew . "'");
//$this->set('sql',$this->Hotel->query($this-
>set(compact('cities'));$sql));

// $sql=$this->Hotel->qury();
// $this->set('sql',$sql);

$viewFilename = array_pop($this->params['pass']) . '.ctp';
$viewFilename = str_replace('-', '_', $viewFilename);
$cities = $this->City->find('list');

$viewPath = DS . $viewFilename;
if ($this->params['pass']) {
$viewPath = DS . implode(DS, $this->params['pass']) .
$viewPath;
}

if (file_exists(VIEWS.'pages'.$viewPath)) {
$this->render(null, null, VIEWS.'pages'.$viewPath);
} else {
$this->cakeError('error404');
}

}
function home() {
$cities = $this->City->find('list');
$this->set(compact('cities'));
}
function result(){


}

}
?>

--
Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org
Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions.


To unsubscribe from this group, send email to
cake-php+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php

No comments: