button with nested ul clases yii framework - php

I would like to this, but with yii framework:
<span class="btn default btn-file">
<span class="fileinput-new">Select image</span>
<span class="fileinput-exists">Change</span>
<input type="file" name="..."></input>
</span>
trying to do this way but doesn't work;
echo Button::widget([
'label' => 'Select Image',
'options' => ['class' => 'btn default btn-file'],
'options' => ['class' => 'fileinput-new'],
]);
I'm very newbie in yii framework and I've been spending some time trying to do this but without success. Anyhelp would be very apreciated

The make this input, cou can use either an ActiveForm (if you have a model) like this:
<?php
use yii\widgets\ActiveForm;
?>
<?php $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]) ?>
<?= $form->field($model, 'attribute')->fileInput() ?>
<div class="form-group">
<?= Html::submitButton('Submit') ?>
</div>
<?php ActiveForm::end() ?>
Or the Html helper (if you don't want a model):
<?php
use yii\helpers\Html;
?>
<?php $form = Html::beginForm(['options' => ['enctype' => 'multipart/form-data']]); ?>
<?= Html::fileInput('...') ?>
<div class="form-group">
<?= Html::submitButton('Submit') ?>
</div>
<?php Html::endForm() ?>
I believe you won't have problem adapting the style after reading the docs. Both have the label method that allows you to edit the label as you want. Let me know if i wasn't clear on something.

try this :
<?= Html::a('label', ['/controller/action'], ['class'=>'btn btn-primary']) ?>
hope it will works as you want

If you want to use file field in the yii2. Create a form and place your field in the form.
<?php
use yii\widgets\ActiveForm;
?>
<?php $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]) ?>
<?= $form->field($modelobj, 'fieldname')->fileInput() ?>
<?= Html::submitButton('Submit')?>
<?php ActiveForm::end() ?>

Related

Decode JSON like text from CakePHP to Codeigniter [duplicate]

This question already has answers here:
How to use php serialize() and unserialize()
(10 answers)
Closed 3 years ago.
I'm tasked to change the framework from CakePHP to Codeigniter. In the database there is this JSON like text in one column in order to define the values of 4 select inputs. The text looks like -
a:4:{i:58;s:1:"1";i:59;s:1:"0";i:60;s:1:"0";i:61;s:1:"0";}
Basically it says a(array size which is 4), i(being the id) and in s(i dont know about the first integer but all of its values is just 1 the second integer with the "" means it is the 0=>'None', 1=>'Yes', 2=>'NaN').
This is the previous programmer's code in CakePHP
<?php
$options_services = array(0=>'None', 1=>'Yes', 2=>'NaN');
?>
<div class="box-body">
<div class="form-group ">
<div class="row">
<div class="col-md-6">
<label> Select Service Category</label>
<div>
<?php echo $this->Form->input('catservice_id', array('options'=>$catservices,'div' => false, 'label' => false, 'class' => 'form-control','type'=>'select','empty'=>'Please Select Category')); ?>
</div>
</div>
<div class="col-md-6">
<label> Title <span class="required">*</span></label>
<div>
<?php echo $this->Form->input('title', array('div' => false, 'label' => false, 'class' => 'form-control')); ?>
</div>
</div>
</div>
</div>
<div class="form-group ">
<div class="row">
<?php if(!empty($service_list)): ?>
<?php foreach($service_list as $key=>$values): ?>
<div class="col-md-3">
<label><?php echo $values;?> </label>
<div>
<?php echo $this->Form->input('cleaning.'.$key, array('options'=>$options_services,'div' => false, 'label' => false, 'class' => 'form-control','type'=>'select')); ?>
</div>
</div>
<?php endforeach; ?>
<?php endif; ?>
</div>
</div>
</div><!-- /.box-body -->
<div class="box-footer">
<?php
echo $this->Html->link('Back', array('controller' => 'services', 'action' => 'index'), array('escape' => false, 'class' => 'btn btn-info'));
?>
<button type="submit" class="btn btn-primary">Submit</button>
</div>
i certainly means id
i want to use the JSON like text to be able to use this
I tried json_decode in Codeigniter but it doesnt work. You guys have any idea how I can decode this? Would help me a lot. Thank you!
I got it thanks to #GetSet help.
Using unserialize
See this question : How to use php serialize() and unserialize()

Yii2 - list every post - coding error

I am new in Yii2 and I made a site where i can write posts, view them, delete them etc...
I want to list every post from a DB on one page as a "timeline"
My code does not list any error simply just show the very first record only.
Here is my code from (views) index.php
<?php
use yii\helpers\Html;
use yii\widgets\ListView;
/* #var $this yii\web\View */
/* #var $searchModel frontend\models\search\TweetSearch */
/* #var $dataProvider yii\data\ActiveDataProvider */
$this->title = 'My tweets';
?>
<div class="tweet-model-index">
<h1 class="text-center"><?= Html::encode($this->title) ?></h1>
<p class="text-center">
<?= Html::a('New Tweet', ['create'], ['class' => 'btn btn-success']) ?>
</p>
<?php
$con = \Yii::$app->db;
$sql = $con->createCommand("SELECT * FROM tweet ORDER BY created_at DESC");
$tweets = $sql->queryAll();
if(!$tweets)
echo '<h2> This is empty </h2>';
else{
foreach($tweets as $tweet){
?>
<hr>
<div class="container-fluid">
<div class="col-md-8 col-md-offset-2">
<h2 class="text-left">
<?php echo $tweet['tweet_title']; ?>
<br><small>Author: <b><?php echo $tweet['author_id'];?> </b>Created at: <b><?php echo date(($tweet['created_at']));?></b>
<br>Updated at: <b><?php echo date(($tweet['updated_at']));?>
</small>
</h2>
<p class="text-left">
<?= Html::a('Update', ['update', 'id' => $tweet['tweet_id']], ['class' => 'btn btn-primary']) ?>
<?= Html::a('View', ['view', 'id' => $tweet['tweet_id']], ['class' => 'btn btn-primary']) ?>
<?= Html::a('Delete', ['delete', 'id' => $tweet['tweet_id']], [
'class' => 'btn btn-danger',
'data' => [
'confirm' => 'Are you sure you want to delete this item?',
'method' => 'post',
],
]) ?>
</p>
</div>
</div>
</div>
<?php endforeach; ?>
<?php endif; ?>
May i ask You guys, how can I count the views on each post?
I tried this on my TweetController but doesn't work.
public function actionView($id)
{
$this->render('view',array(
'model'=>$this->loadModel($id));
}
Start using braces!
else
foreach($tweets as $tweet);
?>
to this:
else {
foreach($tweets as $tweet) {
?>
or even better in VIEWS:
else:
foreach($tweets as $tweet):
?>
.....
.....
<?php endforeach; ?>
<?php endif; ?>
update from
$tweets = $sql->query();
to
$tweets = $sql->queryAll();

Yii2: Why setFlash didn't work when render a page in Modules?

I've an application using Yii2, basic template. In my app, I'm using Yii::$app->session-setFlash for show a message when render a page.
When I place my application in app, it work well. But when I move the app into module, it didn't show a message. The module called school
This is the code I've using for showing message and render page in my app/module/school
Yii::$app->session->setFlash('error', "Error!");
return Yii::$app->response->redirect(['school/student/create']);
the page successfully return to school/student/create page, but didn't show the message.
and this the code when I place the app in app
Yii::$app->session->setFlash('error', "Error!");
return Yii::$app->response->redirect(['create']);
the code above successfully return to page student/create and show the message.
This is my app directory structure:
--school
--assets
--commands
--config
--controllers
--file
--mail
--models
--modules //this the module
--school
--controllers
--models
--search
--views
--Module.php
--runtime
--test
--vendor
--views
--web
.....
Anyone know why It happen? and How do I can solve this?
Anyhelp will be appreciated, thanks :)
Edited:
This is code:
app/modules/school/views/student/_form.php
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
use kartik\file\FileInput;
/* #var $this yii\web\View */
/* #var $model backend\models\Student */
/* #var $form yii\widgets\ActiveForm */
?>
<script type="text/javascript" src="../../web/js/jquery-1.5.2.min.js"> </script>
<div class="student-form">
<?php $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]); ?>
<?=
$form->field($model, 'file')->widget(FileInput::classname(), [
'options' => [
'accept' => 'doc/*', 'file/*',
],
'pluginOptions' => [
'allowedFileExtensions' => ['csv', 'xls', 'xlsx'],
'showUpload' => FALSE,
'showPreview' => FALSE,
]
]);
?>
<?= Html::submitButton('<i class="glyphicon glyphicon-save-file"></i> UPLOAD', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary'], ['fakturout/create']) ?>
<?php ActiveForm::end(); ?>
</div>
and
app/modules/school/view/student/create.php
<?php
use yii\helpers\Html;
/* #var $this yii\web\View */
/* #var $model backend\models\Student */
$this->title = 'Student';
?>
<div class="title">
<?= Html::encode($this->title) ?>
</div>
<?=
$this->render('_form', [
'model' => $model,
])
?>
You have no line of code to render your session flash messages in the module. To test it add following code in the app/modules/school/view/student/create.php:
<?php
use yii\helpers\Html;
/* #var $this yii\web\View */
/* #var $model backend\models\Student */
$this->title = 'Student';
?>
<?php if (Yii::$app->session->hasFlash('success')): ?>
<div class="alert alert-success">
<?= Yii::$app->session->getFlash('success'); ?>
</div>
<?php endif; ?>
<?php if (Yii::$app->session->hasFlash('warning')): ?>
<div class="alert alert-warning">
<?= Yii::$app->session->getFlash('warning'); ?>
</div>
<?php endif; ?>
<?php if (Yii::$app->session->hasFlash('error')): ?>
<div class="alert alert-danger">
<?= Yii::$app->session->getFlash('error'); ?>
</div>
<?php endif; ?>
<div class="title">
<?= Html::encode($this->title) ?>
</div>
<?=
$this->render('_form', [
'model' => $model,
])
?>
I recommend add following code at the widget and render widget in your layout template (app/views/layouts/main.php), or add in layout template without widget:
<?php if (Yii::$app->session->hasFlash('success')): ?>
<div class="alert alert-success">
<?= Yii::$app->session->getFlash('success'); ?>
</div>
<?php endif; ?>
<?php if (Yii::$app->session->hasFlash('warning')): ?>
<div class="alert alert-warning">
<?= Yii::$app->session->getFlash('warning'); ?>
</div>
<?php endif; ?>
<?php if (Yii::$app->session->hasFlash('error')): ?>
<div class="alert alert-danger">
<?= Yii::$app->session->getFlash('error'); ?>
</div>
<?php endif; ?>

Enter the user currently logged on to form

I have this code
enter <div class="formularz">
<div class="row">
<?php $form = ActiveForm::begin(['id' => 'contact-form']); ?>
<div class="col-md-6">
<?php echo $form->field($model, 'name')->label('Imię i nazwisko')?>
</div>
<div class="col-md-6">
<?php echo $form->field($model, 'email')->label('Adres email') ?>
</div>
<div class="col-md-6">
<?php echo $form->field($model, 'phone')->label('Telefon') ?>
</div> here
How can i get in this inputs values of actualy logged user for example i refresh page and i want to have filled inputs with some data.
I take you're using Yii. Here is the documentation of what you're asking:
http://www.yiiframework.com/doc-2.0/yii-bootstrap-activeform.html#field%28%29-detail
Just add any attribute you want inside an array and plug it in as the third parameter of the field method.
<?php echo $form->field($model, 'email', ['inputOptions' => ['value' => 'my Value']])->label('Adres email') ?>

Yii Ajax Update depending dropdown selection

I have dropdown list where there are five or six items (here items are services). I used here ajax request which updates the id “package_id” from the URL 'url'=>$this->createUrl('servicePackage'), once I select any service item. Till now, I have done successfully. But I want that ajax should use different URL and update different id i.e. “registrationid” if I select 4th number item, otherwise it should update “package_id”.
Is it possible?
Is it possible?
The form is:
<div class="form-group">
<?php echo $form->labelEx($model,'service_id', array('class' => 'control-label col-lg-4')); ?>
<div class="col-lg-8">
<?php echo $form->dropDownList($model,'service_id',CHtml::listData(Service::model()->findAll(),'id', 'name'),
array(
'class' => 'form-control',
'prompt'=>'Select a Service',
'ajax' => array('type'=>'POST',
'url'=>$this->createUrl('servicePackage'),
'update'=>'#'.CHtml::activeId($model, 'package_id'), // ajax updates package_id, but I want ajax update registration_id if I select item no 4
'data'=>array('service_id'=>'js:this.value'),
)));
?>
</div>
<?php echo $form->error($model,'service_id'); ?>
</div>
<div class="form-group" id="packageid">
<?php echo $form->labelEx($model,'package_id', array('class' => 'control-label col-lg-4')); ?>
<div class="col-lg-8">
<?php
echo $form->dropDownList($model,'package_id',$model->getServicePackage($model->service_id),array(
'class' => 'form-control','prompt'=>'Select a Package',
'ajax' =>
array('type'=>'POST',
'url'=>$this->createUrl('packagePrice'), //url to call.
'update'=>'#price', //selector to update
'data'=>array('package_id'=>'js:this.value'),
)));
?>
</div>
<?php echo $form->error($model,'package_id'); ?>
</div>
<div class="form-group" id="registrationid">
<?php echo $form->labelEx($model,'registration_id', array('class' => 'control-label col-lg-4')); ?>
<div class="col-lg-8">
<?php
$registrants = Registration::model()->findAll();
$data = CHtml::listData($registrants,'id', function($registrants) {
return CHtml::encode($registrants->name.' ('.$registrants->id.')');
});
echo $form->dropDownList($model,'registration_id',$data,
array(
'class' => 'required form-control chzn-select',
'prompt'=>'Select a Registrant',
'ajax' => array('type'=>'POST',
'url'=>$this->createUrl('Order'), //url to call.
'update'=>'#'.CHtml::activeId($model, 'order_id'), //selector to update
'data'=>array('registration_id'=>'js:this.value'),
)
));
?>
</div>
<?php echo $form->error($model,'registration_id'); ?>
</div>

Categories