I would like to link to a different controller, passing in multiple parameters.
Documentation:
http://www.yiiframework.com/wiki/48/by-example-chtml/
<?php echo CHtml::link('Link Text',array('controller/action',
'param1'=>'value1',
'param2'=>'value2',
'param3'=>'value3')); ?>
<?php echo CHtml::link('Link Text',
Yii::app()->createUrl('controller/action',array(
'param1'=>'value1',
'param2'=>'value2',
'param3'=>'value3')); ?>
Related
I have many images in backends web folder, I want to use those images in frontend how to get those files?
I need to display 1 (first) image which path is saved in database like Img1.jpg;img2.jpg;
<?php
foreach (explode(';',rtrim($row['images'],';'),1) as $key_img => $value_img)
{
?>
<?php echo Html::img('#backend/web'.'/'.$value_img);?>
<?php
}
?>
<?= \yii\helpers\Html::img('#backend/web/images/your-image.jpg') ?>
The src parameter, containing the backend alias, will be processed by Url::to()
Check the docs for details on Html::img().
Simply try:
<?= \yii\helpers\Html::img( Yii::getAlias('#backend'). '/web/images/your-image.jpg') ?>
I tried to make a dropdown list in yii2 using this link : How to make a drop down list in yii2?
my code is :
<?php use yii\helpers\ArrayHelper;
use app\models\Product;
?>
<?= $listdata=ArrayHelper::map(Product::find()->all(),'id','name'); ?>
<?= $form->field($model, 'parent_id')-> dropDownList($listdata); ?>
but I have a problem in line of using ArrayHelper
the problem is:
PHP Notice – yii\base\ErrorException
Array to string conversion.......! I tested the below code :
$listData=ArrayHelper::map(Product::find()->asArray()->all(),'id','name');
but it dos not solved and has the same error!
whats the problem? can somebody help me?
You are trying to echo an array, change <?= to <?php in:
<?= $listdata=ArrayHelper::map(Product::find()->all(),'id','name'); ?>
Try like this
<?php
use yii\helpers\ArrayHelper;
use app\models\Product;
?>
<?= $form->field($model, 'parent_id')->dropDownList(
ArrayHelper::map(Product::find()->all(),'id','name'),
['prompt'=>'Select '])
?>
I want to create dropdownbox in Yii framework which have name is Category,I copy in the stackoverflow but it has error,
<form>
<?php
$list = CHtml::listData(Categories::model()->findAll(array('order' => 'cate_name')), 'id', 'cate_name');
echo $form->dropDownList("Category", 'cate_name', $list);
?>
</form>
here is error:
Undefined variable: form
Your form should looks like this:
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'your-form',
'enableAjaxValidation'=>false,
)); ?>
<?php
$list = CHtml::listData(Categories::model()->findAll(array('order' => 'cate_name')), 'id', 'cate_name');
echo $form->dropDownList("Category", 'cate_name', $list);
?>
<?php $this->endWidget(); ?>
NOTE: replace form tag with form widget
Instead of using html form tag, you have to use yii form widget.
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'form_id',
)); ?>
For tutorial: http://www.yiiframework.com/doc/guide/1.1/en/form.view
How do I change the header after login, or is using another header even the right way to do it? Meaning there would be two different headers(guest/user). I've searched for it, mostly about redirects of the entire page, not what I am looking for.
Sorry for the noob question. :/
Make two files in your view with guest_header.php and user_header.php when user is logged in user user_header.php as your header file in your layout or simply use guest_header.php
and in your column layout use it like below
when user is logged in
<?php if(Yii::app()->user->id): ?>
<?php $this->beginContent('//layouts/user_header.php'); ?>
when user is guest
<?php else: ?>
<?php $this->beginContent('//layouts/guest_header.php'); ?>
<?php endif; ?>
Added this line
<?php $this->endContent(); ?>
<?php $this->beginContent('//layouts/main'); ?>
<?php echo $content; ?>
</div><!-- content -->
<?php $this->endContent(); ?>
hopefully a quick question here, have not been able to find out the answer on google. Anyway I have the following File upload field.
<div class="row">
<?php echo CHtml::form('', 'post', array('enctype' => 'multipart/form-data')); ?>
<?php echo $form->labelEx($model, 'doc'); ?>
<?php echo CHtml::activeFileField($model, 'doc'); ?>
<?php echo $form->error($model, 'doc'); ?>
</div>
The question I have is how to change the default text? On the page in question the upload field comes up with "No file chosen" beside the button when the page loads. I wish to change this text, anyone tell me how? Thanks.
I quick & dirty fix is this:
<?php echo CHtml::activeFileField($model, 'type',array('style'=>'width:100px;', 'onchange' => 'this.style.width = "100%";')); ?>