Please help with this code:
<ul class="activity-projects">
<?php foreach ($ownProjects as $userProject) : ?>
<li>
<a class="<?php echo ($userProject->id == $project_id) ? 'active' : '';?>"
href="<?php echo Yii::app()->createUrl('user/activity', array(
'user_id'=>$user->id,
'project_id'=>$userProject->id,
'date'=>$date)
); ?>"><?php echo $userProject->name; ?></a>
</li>
<?php endforeach; ?>
<?php foreach ($projects as $userProject) : ?>
<li>
<a class="<?php echo ($userProject->project_id == $project_id) ? 'active' : '';?>"
href="<?php echo Yii::app()->createUrl('user/activity', array(
'user_id'=>$user->id,
'project_id'=>$userProject->project->id,
'date'=>$date)
); ?>"><?php echo $userProject->project->name; ?></a>
</li>
<?php endforeach; ?>
</ul>
How to change it to dropdown list, using CHtml::dropDownList. Thanks for watching!
First, you need to define a key-value array like this:
$options = array();
<?php foreach ($ownProjects as $userProject)
array_push($options, array($userProject->id => $userProject->name));
?>
echo CHtml::dropDownList('seletName', '1', $options);
This will be produced an html <select> tag with "seletcName" name. And also option with value "1" will be selected option. You can use your desired value for first and second parameters.
Also you can use CActiveForm#dropDownList for this purpose.
In your form, use the form dropDownList() function.
<?php echo $form->dropDownList(
$model,
'project_id',
CHtml::listData(OwnProjects::model()->findAll(),
'id', 'name'),
array('empty' => '(Select project)','class'=>"form-control")
);
?>
From your example, it looks like OwnProjects is not a model on its own, but a subset of a model. You can customise the query
<?php echo $form->dropDownList(
$model,
'project_id',
CHtml::listData(OwnProjects::model()->findAllByAttributes(array('user_id'=> Yii:app()->user->id),
'id', 'name'),
array('empty' => '(Select project)','class'=>"form-control")
);
?>
This solution finally helps me:
<?php $options = array();
foreach ($projects as $userProject) :
$options[$userProject->id] = $userProject->project->name;
endforeach;
echo CHtml::dropDownList('selectName', '1', $options);
?>
Related
I need give some hyperlink to my items category in codeigniter.
$this->data['itemdata'] = array(
array(
'title' => 'Printers / Accessories',
'items' => ['Printers', 'Printer Cartridges', 'Compatible Toners'],
'brands' => ['hp', 'cannon', 'brother', 'toshiba', 'sharp']
),
how can I create it?
Updated
view file
<div class="items">
<ul class="floated">
<?php foreach ($item['items'] as $key => $value): ?>
<li><?php echo $value; ?></li>
<?php endforeach; ?>
</ul>
</div>
You have to do it something like below:-
<li><?php echo $value;?></li>
Note:- I hope through URL you want to send user to some fruitful link. So change controller/function/uri to corresponding values.
Reference:-CodeIgniter - Correct way to link to another page in a view
Note:- if you want to remove link then do like below:-
<li><?php echo $value;?></li>
I have small problem with my PHP code. I must insert to database multiple values but I dont have any idea how I can do this.
My View:
<?php foreach($myKidsGroupID as $row): ?>
<label><input type="checkbox" name="user_my_group_msg" value="<?php echo $row->id; ?>" class="my_group_msg pull-right"><?php echo $row->firstname; ?> <?php echo $row->lastname; ?></label><br>
<?php endforeach; ?>
And my Model looks like this:
...
elseif($checked_my_group == 1) {
foreach ( ?? ) {
$new_mail = array(
'to_user' => $this->input->post('user_my_group_msg'),
);
$this->db->insert('mailbox', $new_mail);
}
}
...rest code....
Inside my View I display all users as checkbox but If I select two persons I must INSERT two query to database. Anyone can help me?
simple thing is
use serialize function don't use foreach
serialize $this->input->post('user_my_group_msg')
try it once it will help you your View code will look as like
<?php foreach($myKidsGroupID as $row): ?>
<label><input type="checkbox" name="user_my_group_msg" value="<?php echo $row->id; ?>" class="my_group_msg pull-right"><?php echo $row->firstname; ?> <?php echo $row->lastname; ?></label><br>
<?php endforeach; ?>
Model will be corrected like this
<?php
elseif(sizeof($this->input->post('user_my_group_msg'))>=1) {
foreach ( $this->input->post('user_my_group_msg') as $value ) {
$new_mail = array(
'to_user' => $value,
);
$this->db->insert('mailbox', $new_mail);
}
}
.....rest code
?>
I am facing hard times working with yii framework,The latest I ecounterd is while populating data from database in a drop down.i have tried few things including codes from the site,but no use,I have the following codes,Have a model named as PostJob.PHP.
Do i have to write a seprate function in model.?
view: postjob.php
<div class="row">
<?php echo $form->labelEx($model,'Category'); ?>
<?php echo CHtml::dropDownList('category', $model->title, $list, array('empty' => '(Select a Category')); ?>
<?php echo $form->error($model,'category'); ?>
</div>
and in view section , I have given :
<?php
$list = CHtml::listData(PostJob::model()->findAll(array('jobpost','description')),'id','title');
?>
geting nothing.Pls Help.
you can do this:
<?php $list = CHtml::listData(PostJob::model()->findAll(),'id','title'); ?>
<?php echo $form->labelEx($model,'Category'); ?>
<?php echo $form->dropDownList($model , 'category', $list, array('empty' => '(Select a Category')); ?>
<?php echo $form->error($model,'category'); ?>
I am doing a yii web application
i have a drop down list that should be dependent on another , i use ajax however it doesnt work.
ive seen the yii tutorial for dependent drop downs and searched everywhere.
http://www.yiiframework.com/wiki/24
this is my main drop down list:
<div class="row">
<?php echo $form->labelEx($model, 'sourceID'); ?>
<?php
echo $form->dropDownList($model, 'sourceID', CHtml::listData(Sources::model()->findAll(), 'sourceID', 'name'), array('empty' => 'select source'), array(
'ajax' => array(
'type' => 'POST',
'url' => CController::createUrl('reservations/atest'),
'update' => '#meal'
)
)
);
?>
<?php echo $form->error($model, 'sourceID'); ?>
</div>
this is the dependent drop down list :
<div class="row">
<?php echo $form->labelEx($model, 'meal'); ?>
<?php echo $form->dropDownList($model, 'meal', array()); ?>
<?php echo $form->error($model, 'meal'); ?>
</div>
this is my controller action:
public function actionAtest() {
$data = Sources::model()->findAll();
$data = CHtml::listData($data, 'sourceID', 'name');
foreach ($data as $value => $name) {
echo CHtml::tag('option', array('value' => $value), CHtml::encode($name),true);
} }
also, i added the action to the access rules.
any help is appreciated ,
thank you in advance.
You placed the ajax option after the htmlOptions. Here is the modified code
<div class="row">
<?php echo $form->labelEx($model, 'sourceID'); ?>
<?php
echo $form->dropDownList($model, 'sourceID', CHtml::listData(Sources::model()->findAll(), 'sourceID', 'name'), array('empty' => 'select source','ajax' => array(
'type' => 'POST',
'url' => CController::createUrl('reservations/atest'),
'update' => '#meal'
)
)
);
?>
<?php echo $form->error($model, 'sourceID'); ?>
</div>
And instead of using forms dropdownlist use CHtml::dropDownList for the dependent drop-down.
echo CHtml::dropDownList('meal','', array());
You can also use CActiveForm::dropDownList but in that case you have to use CHtml::resolveNameId in the update option of ajax
I am trying to use a foreach statement to display a list ul>li. In case the list has no elements how can i handle that criteria.
<ul>
<?php foreach($person['CastsMovie'] as $cast): ?>
<li><?php echo $this->Html->link($cast['Movie']['name'], array('controller' => 'movies', 'action' => 'view', $cast['movie_id'], 'admin' => true), array('escape' => false)); ?> (<?php echo date("Y", strtotime($cast['Movie']['MovieDetail']['release_date'])); ?>)</li>
<?php endforeach; ?>
</ul>
Check whether it is empty or not:
if ( !empty( $person ) ) {
foreach ( $person as $cast ) {
echo "<li>$cast</li>";
}
} else {
echo "<li>This play has no cast members.</li>";
}
Actually, you need to surround the UL with a check.
Otherwise you end up with an empty "ul" tag which is unnecessary:
<?php if (!empty($person['CastsMovie'])) { ?>
<ul>
<?php foreach($person['CastsMovie'] as $cast) { ?>
<li><?php echo $this->Html->link($cast['Movie']['name'], array('controller' => 'movies', 'action' => 'view', $cast['movie_id'], 'admin' => true), array('escape' => false)); ?> (<?php echo date("Y", strtotime($cast['Movie']['MovieDetail']['release_date'])); ?>)</li>
<?php } ?>
</ul>
<?php } ?>
this means: you check if there are any list elements (in your case CastsMovies to this person) and if so you display the list (ul + li). if not the complete list will be omitted (not just the child li elements).
FIRST, in whatever "controller" you are using, prepare your data, to make it fit for the template.
foreach($person['CastsMovie'] as $key => $cast) {
$cast['html_link'] = $this->Html->link($cast['Movie']['name'],
array('controller' => 'movies',
'action' => 'view',
$cast['movie_id'],
'admin' => true),
array('escape' => false)
);
$cast['html_date'] = date("Y", strtotime($cast['Movie']['MovieDetail']['release_date']));
$person['CastsMovie'][$key] = $cast;
}
Then make your template a sane and readable
<?php if($person['CastsMovie']: ?>
<ul>
<?php foreach($person['CastsMovie'] as $cast): ?>
<li>
<?=$cast['html_link']?> (<?=$cast['html_date']?>)
</li>
<?php endforeach ?>
</ul>
<?php endif ?>