I'm trying to make dropDownList in _form view.
This is the Controller part
public function actionCreate()
{
$modelCountry = Country::model()->findAll();
$this->render('create',array(
'model'=>$model,'modelCountry'=>$modelCountry,
));
}
Now in _form view I have a TextField which must be modified to dropDownList
<div class="row">
<?php echo $form->labelEx($model,'name_en'); ?>
<?php echo $form->textField($model,'name_en',array('size'=>60,'maxlength'=>255)); ?>
<?php echo $form->error($model,'name_en'); ?>
</div>
That means I must modify it to something like this
<?php echo $form->dropDownList($model,'name_en',array('!Variable!A!From_modelCountry!','!Variable!B!From_modelCountry!' )); ?>
I can use foreach loop and inside that loop create some DropDownList but maybe there is some better way to take data out of an array $modelCountry and put them straight to array of that dropDownList?
yes you can do that. In your controller you can write
$list=CHtml::listData($modelCountry,'id','name_en');
$this->render('create',array(
'model'=>$model,'modelCountry'=>$modelCountry,'list'=>$list
));
Now in your View you can write
<?php echo $form->dropDownList($model,'name_en',$list); ?>
You can use Chmlt::listData in your view but that should be a part of controller, thats what MVC is all about.
<?php echo $form->dropDownList($model,'name_en',CHtml::listData(Country::model()->findAll(),'id','name')); ?>
Related
I want to make text area in cactiveform in yii dynamically. but I am getting error i.e. "Property "Verse.translation" is not defined"
I have translation_text field, not translation field in my db. Secondly $trans['translation_text'] display the verse translation but when i keep it in textArea it is giving error. as i have described.
I have a code.
<?php foreach($model->verseTranslations as $trans) { ?>
<?php $model->translation = $trans['translation_text']; ?>
<?php echo $form->textArea($model,'translation',array('rows'=>6, 'cols'=>50)); ?>
<?php } ?>
But i do not know how to keep value $trans['translation_text'] in textArea.
Any help will be appreciated.
Thanks
Do it like this :
<?php foreach($model->verseTranslations as $trans) { ?>
<?php echo $form->textArea($model,'translation',array('value'=>$trans['translation_text'],'rows'=>6, 'cols'=>50)); ?>
<?php } ?>
And in your model as RobM said earlier, but don't forget to add a validator in you Verse class for 'translation' attribute ! :
class Verse extends CActiveRecord
{
public $translation;
public function rules()
{
return array(
array(
'translation',
'safe',
'on'=>'',
),
//others validators here
);
}
}
Just replace the second parameter in $form->textArea with $trans['translation_text'], so that it becomes:
<?php echo $form->textArea($model, $trans['translation_text'], array('rows'=>6, 'cols'=>50)); ?>
The second parameter is the value of the textArea, so the value of any variable here will show up as the default value of the text area element.
Add translation property to Verse class in models
class Verse extends CActiveRecord
{
public $translation;
I am doing the tree tutorial and would like to display the data in a view and wrap it in html etc. I am new to Cake PHP and this has been a bit of a pain. Below is an example of what I've tried. In short, I figure I could assign the output to a variable using set. I am doing everything wrong.
Controller
<?php
class CategoriesController extends AppController {
public function index() {
$this->set('output', $this->Category->generateTreeList(null, null, null, ' '));
}
}
?>
View
<?php foreach ($output as $data): ?>
<div><?php echo $data['Category']['name']; ?></div>
<?php endforeach; ?>
<?php unset($data); ?>
unlike other find methonds, generateTreeList doesen't return a named array but a plain array with numeric indexes
try print_r($output) and you'll see how the array is formatted and how you can use it in your view
to show your data, in your foreach cicle you just have to do this
echo $data;
I'm trying to create an edit form using Lithium to edit some MongoDB data. My data (produced by another tool) looks like this:
{
"thing_a" : "value_a",
"thing_b" : "value_b",
"settings" :
{
"sub_thing_a" : ["sub_value_a", "sub_value_b"]
}
}
The problem I'm having is with the array 'sub_thing_a' in 'settings'. I need to display a text box for each value so I can edit them and save them back. The ultimate aim here is to use some jQuery to add/delete text boxes to/from the form and then values from the array - but for now I'm trying to just get a simple version working that will let me edit the values and save them away.
My model is really simple:
<?php
namespace app\models;
class Test extends \lithium\data\Model {
protected $_meta = array('source' => 'test');
}
?>
and the controller likewise:
<?php
namespace app\controllers;
use app\models\Test;
class TestsController extends \lithium\action\Controller {
public function index() {
$tests = Test::all();
return compact('tests');
}
public function edit($id=null) {
if(isset($id)) {
$test = Test::find($id);
} else {
$test = Test::create();
}
if ($this->request->data) {
if ($test->save($this->request->data)) {
$this->redirect('/tests/index');
}
}
return compact('test');
}
}
?>
Problems start with the edit form - As I have it now, it will display the values of my array, but the data does not get written correctly. Any clues as to how I should approach this? (Note: As I mentioned earlier, I will need to produce a dynamic version of this that allows me to add/delete text boxes to/from the form, so I do need to be able to take some sort of control of the helper - in case there is some really easy 'convention' way of doing this.)
edit.html.php:
<?=$this->form->create($test); ?>
<?=$this->form->field('thing_a'); ?>
<?php foreach ($test->settings->sub_thing_a as $i=>$elem): ?>
<?=$this->form->field('settings.sub_thing_a',array('label'=>'thing', 'value'=>$test->settings->sub_thing_a[$i]));?>
<?php endforeach; ?>
<?=$this->form->submit('save'); ?>
<?=$this->form->end(); ?>
and index.html.php (for completeness)
<?php foreach($tests as $test): ?>
<h2><?=$this->html->link($test->thing_a,'/tests/edit/'.$test->_id); ?></h2>
<?php foreach($test->settings->sub_thing_a as $item): ?>
<h4><?=$item ?></h4>
<?php endforeach; ?>
<?php endforeach; ?>
Ok, so in the end, it was (of course) quite simple. in the edit.html.php file we can simply write:
<?=$this->form->field('settings[sub_thing_a][]',array('value'=>$test->settings->sub_thing_a[$i]));?>
The settings[sub_thing_a][] creates the array containing an array of the string values from the form.
My View :-
<html>
<?= link_tag(base_url().'css/simple.css'); ?>
<body>
<?php $this->load->helper('form'); ?>
<?php $this->load->view('commentform'); ?>
<?php $id=$this->uri->segment(3);?>
<?php echo $id;?>
</body>
</html>
i would like to use the variable $id in my controller.I'm using codeigniter by the way, and am a beginner. I would appreciate any help on this.
you should not call the $id from the View, you should get it at the controller level and pass it to the View.
as Bulk said. you URL will be something like that
www.mysite.com/thecontrollername/thefunction/id
for example your controller if home and there is a show_id function in it and your view is call show_id_view.php.
you will have your url like this: www.mysite.com/home/show_id/id
your function in home will read the id"
in your home controller:
function show_id(){
$id=$this->uri->segment(3);
$view_data['id'] = $id;
$this->load->view('show_id_view',$view_data);
}
in the view (show_id_view):
<?php echo $id ?>
nothing else..
hope this helps.
Well, ideally you wouldn't do it this way. You should assign the variable first in the controller and pass it to the view if you need to use it there.
$data['id'] = $this->uri->segment(3);
$this->load->view('view_file', $data);
$id would then be available in your view as well.
in my view i add link
<li><a href="<?php echo base_url()?>link/show_id/mantap"> coba </li>
in my link.php controler i add function show_id
function show_id(){
$id=$this->uri->segment(3);
$data['coba'] = $id;
$this->mobile->view('**daftarmember_form**',$data);
in my next view daftarmember_form.html
)
<?php echo $id;?>
they print mantap,
I have a problem with displaying a view. When I pass var to view, view doesn't render.
Controller:
public function indexAction()
{
$branchModel = new Application_Model_Branches();
$branches = $branchModel->getAllBranches();
$this->view->menu = $branches;
}
View (index.phtml):
<h2>Menu</h2>
<?php
$this->htmlList($this->menu);
?>
When I try debug $branches without assign it to view, all seems to be ok, but when I try push it to view,index.phtml don't appear.
Regards
You're just missing an echo in your code, the htmlList view helper returns a value - it doesn't echo it. Some examples of the various form view helpers can be seen here
<h2>Menu</h2>
<?php
echo $this->htmlList($this->menu);
?>
controller
$this->view->variableName = "Hello World!";//assign here
$this->view->assign('variableName1', "Hello new World!");//assign here
view
echo $this->variableName;//echo here
echo $this->variableName1;//echo here