Sunday, December 28, 2008

How to fetch data in a dropdown box in add action

This is my code can anybody tell me how to fetch data in a drop down
box in add action i want to display data in dropdown box and when i
click on add button is save values in a database
code is follows my controller is

<?php
class EntriesController extends AppController {

var $name = 'Entries';
var $helpers = array('Html','Form');

function index()
{

$this->Entry->recursive = 1;
$this->set('entries', $this->Entry->findAll());

}
function view($id = null)
{
if (!$id)
{
$this->Session->setFlash('Invalid id for
Entry.');
$this->redirect('/entries/index');
}
$this->set('entry', $this->Entry->read(null, $id));

}

function add()
{
if (empty($this->data))
{
$this->render();
}
else
{
//$this->cleanUpFields();

if ($this->Entry->save($this->data))
{

$this->Session->setFlash('The
Entry has been saved');
$this->redirect('/entries/
index');
}
else
{
$this->Session->setFlash
('Please correct errors below.');
}
}
}
function edit($id = null)
{
if (empty($this->data))
{
if (!$id)
{
$this->Session->setFlash('Invalid id
for Entry');
$this->redirect('/entries/index');
}
$this->data = $this->Entry->read(null, $id);
} else
{
//$this->cleanUpFields();
if ($this->Entry->save($this->data))
{
$this->Session->setFlash('The
Entry has been saved');
$this->redirect('/entries/
index');
}
else
{
$this->Session->setFlash
('Please correct errors below.');
}
}
}
function delete($id = null) {
if (!$id) {
$this->Session->setFlash('Invalid id for
Entry');
$this->redirect('/entries/index');
}
if ($this->Entry->del($id)) {
$this->Session->setFlash('Record deleted
successfully');
$this->redirect('/entries/index');
}
}

}

?>

my model of entries is

<?php
class Entry extends AppModel {

var $name = 'Entry';
var $belongsTo=array('Section'=>array(
'className'=>'Section'
),
'Submenu'=>array('className'=>'Submenu')
);

}

?>
and my add view is here i want drop down box and fetch data from
section table in Section field i m using cake php 1.2

<h2>New Entry</h2>
<?php echo $form->create('Entry',array('action' => 'add','type' =>
'file'));?>
<div class="optional">
<?php echo $form->input('Section', array('options' => array
('Announcements','The Venue','About Melbourne')),array('error' =>
'false','value' => array(1,2,3) )); ?>
</div>
<div class="optional">
<?php echo $form->input('Submenu', array('options' => array
(1,2,3,4,5)),array('error' => false)); ?>
</div>
<div class="optional">
<?php echo $form->input('Listitem',array('label' => 'Listitem'),array
('error' => false));?>
</div>
<div class="optional">
<?php echo $form->input('Detail',array('label' => 'Detail'),array
('error' => false));?>
</div>
<!--<div class="optional">
<?php echo $form->labelTag('Entry/file', 'Attachment');?>
<?php echo $html->file('Entry/file');?>
<?php echo $html->tagErrorMsg('Entry/file', 'Please enter a file.');?>
--></div>
<div class="submit">
<?php echo $form->submit('Add');?>
</div>
</form>
<ul class="actions">
<li><?php echo $html->link('Home', '/entries/index')?></li>
</ul>

and my edit view is
<h2>Edit Entry</h2>
<?php echo $form->create('Entry',array('action' => 'edit'));?>
<div class="optional">
/* here instead of text box i wan't data in dropdown box */
<?php echo $form->input('Section.section',array('label'
=>'Section','size'=>'30'
,'type'=>'text'));?>
</div>
<div class="optional">
<?php echo $form->input('Submenu.submenu',array('label'
=>'Submenu','size'=>'30'
,'type'=>'text'));?>
</div>
<div class="optional">
<?php echo $form->input('Entry.Listitem',array('label'
=>'Listitem','size'=>'30'
,'type'=>'text'));?>
</div>
<div class="optional">
<?php echo $form->input('Entry.Detail',array('label'
=>'Detail','size'=>'30'
));?>

</div>
<?php echo $form->hidden('entry/id')?>
<div class="submit">
<?php echo $form->submit('Save');?>
</div>
</form>
<ul class="actions">
<li><?php echo $html->link('Delete','/entries/delete/' . $form->value
('Entry/id'), null, 'Are you sure you want to delete: id ' . $form-
>value('Entry/id'));?>

<li><?php echo $html->link('Back', '/entries/index')?></li>
</ul>

please help me it is very urgent
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
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?hl=en
-~----------~----~----~----~------~----~------~--~---

No comments: