Yii2. Represent an image. - yii2

I use Yii2 and Windows 10 OS. My images are stored in #root/uploads folder.
I am able to upload them via alias like this:
in config\bootstrap.php:
Yii::setAlias('#root', realpath(dirname(__FILE__).'/../../'));
in upload method:
$this->imageFile->saveAs(Yii::getAlias('#root') .'/uploads/' . $this->imageFile->baseName . '.' . $this->imageFile->extension);
Then I try to see an image in view:
<?php echo Html::img('../../uploads/ring.jpg',['class' => 'img-center', 'width'=>150, 'heigth'=>150, 'alt'=>'no image']) ?>
But it is not there... What I missed? Is there some restrictions for the folder?

do the same with getAlias
<?php echo Html::img( Yii::getAlias('#root') .'/uploads/ring.jpg',
['class' => 'img-center', 'width'=>150, 'heigth'=>150, 'alt'=>'no image']) ?>
for debugging you can try creating a proper alias for your uploads directory
Yii::setAlias('#uploads', 'your_absolute_path_uploads');
var_dump( Url::to('#uploads') );
<?php echo Html::img( Yii::getAlias('#uploads') .'/ring.jpg',
['class' => 'img-center', 'width'=>150, 'heigth'=>150, 'alt'=>'no image']) ?>
if don't work ..be sure of your web server / apache / htaccess / redir configuration.
try using
use yii\helpers\Url;
<?php echo Html::img( Url::to['../../uploads/ring.jpg'],
['class' => 'img-center', 'width'=>150, 'heigth'=>150, 'alt'=>'no image']) ?>
Or set a proper absolute alias. This a sample for www.oracle.com .. but you should adapt to your need
Yii::setAlias('#oracle', 'http://www.oracle.com');
echo Url::to('#oracle'); // get URL from alias http://www.bsourcecode.com

Related

Html::img tag not working in yii2

I am new to yii2 and I am trying to add images to my website with the help of Html::img tag but I keep getting broken links on the website. I am using wamp. This is my code:
<?= Html::img('#web/images/logo.png', ['alt' => 'My logo']) ?>
Thanks for the help.
You should conver to an url your alias
use yii\helpers\Url;
...
<?= Html::img( Url::to('#web/images/logo.png'), ['alt' => 'My logo']) ?>
Try this
<?php $domain = yii\helpers\Url::base(true);
echo Html::img($domain.'/images/logo.png', ['alt' => 'My logo'])
?>

I want to extract URL contain in my Wordpress post

I have around 1K posts in my blog. Every post contain URL something like "www.example.com/abcd.html" or "www.example.com/1234.html".
I want to extract those URL from every posts into a file.
Is there any query to do this job done?
Thanks,
Extracting URL within wordpress not easy part to extract. Why to more efforts if there are plugins available.
Install the following plugins and export them in CSV
https://wordpress.org/plugins/export-all-urls/
This will display all post link with title
<?php query_posts('posts_per_page=-1'); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php
$content = $urls = wp_extract_urls( get_the_content() );
echo "<pre>";
print_r($content);
echo "<pre>";
endwhile;
endif; ?>

How to use datepicker in yii2 basic?

I want to use yii2 datepicker but I'm having trouble to implement this. It does not show the date picker and I don't know what is missing in my code. I'm still new in this yii
<?php
use yii\helpers\Html;
use yii\bootstrap\ActiveForm;
?>
<div class="row">
<div class="col-lg-6">
<div class="myproj-index">
<?php $form = ActiveForm::begin(['layout' => 'horizontal']); ?>
<? //$form->field($model, 'periodfrom')
echo DatePicker::widget([
'model' => $model,
'attribute' => 'periodfrom',
'language' => 'en',
'dateFormat' => 'yyyy-MM-dd',
]);
?>
<div class="form-group">
<?= Html::submitButton('Submit', ['class' => 'btn btn-primary']) ?>
</div>
<?php ActiveForm::end(); ?>
</div>
</div>
</div>
Edit
I downloaded this file here jui
I found out that in my file I have no jui folder under yiisoft folder. My question in appasset how can I declare this files ?
Answer
I fixed it, I downloaded the jquery-ui files then added them to the bower folder in yii2.
First install this extension through composer.
run: php composer.phar require --prefer-dist yiisoft/yii2-jui "*" in your project directory.
update composer.
In your view file use use yii\jui\DatePicker;.
You probably did not install Datepicker in the vendor package.
Thanks.
Do you have use .....\DatePicker; ?
Also are you including the jui asset and everything, take a look at the source, do you have jui and all the JS included?
Edit:
Have you done a "composer update" lately?
Use this code if you are using kartik datetime picker
<?= $form->field($model, 'date')->widget(DatePicker::classname(),['options' => ['placeholder' => 'Enter event time ...'],'pluginOptions' => [....]]) ?>

DropDownList yii 2.0 example

I am using yii 2.0 Framework.
How i can make options from my database.
I found this, but it's yii 1.1:
<?php echo CHtml::dropDownList('listname', $select,
array('M' => 'Male', 'F' => 'Female'));
I want to pass it to form:
<?php $form->dropDownList() ?>
How i can fill my dropdownlist from my database table?
If you use ActiveForm widget use this:
<?php
$items = ArrayHelper::map(Model::find()->all(), 'id', 'name');
$form->field($model, 'attribute')->dropDownList($items)
?>
Use yii\helpers\Html it contains Html::dropDownList().
echo Html::dropDownList('listname', $select, ['M'=>'Male', 'F'=>'Female']);
Check Yii Framework 2.0 API
Controller
public function actionSomething() {
$sexes = ['M'=>'Male', 'F'=>'Female'];
$this->render('yourView', ['sexes'=>$sexes]);
}
View
<?php
::
echo Html::dropDownList('listname', $select, $sexes);
::
?>
Yes if you use ActiveForm widget , u dont have to change anything in the controller , in views, in the form, add this where u want the dropdown
use yii\helpers\ArrayHelper;
<?php
$city = \app\models\City::find()->all();
$listData=ArrayHelper::map($city,'cityId','cityName');
?>
<?= $form->field($model, 'cityId')->dropDownList($listData,['prompt'=>'Choose...']) ?>
<?= $form->field($model, 'name_of_field')->dropdownList(['1' => 'aaa', '2' => 'bbb'], ['prompt' => '---Select Data---']) ?>

Different sidebars for different pages on Wordpress

I'm trying to design a theme on WordPress (version 3.3.2) but I am having a few problems in having the sidebar display a different set of widgets on certain pages.
I have tried several online tutorials and this one in particular http://www.rvoodoo.com/projects/wordpress/wordpress-tip-different-sidebars-on-different-pages/ but unfortunately there is no change in the sidebar when I move to a different page
I registered two sidebars on my functions.php as shown below, one which I would consider as main, and the other as a custom sidebar, and I also added different widgets to these sidebars.
<?php register_sidebar( //register sidebar
array(
'name' => 'Right-side',
'before_widget' => '<div class="rightwidget">',
'after_widget' => '</div>',
'before_title' => '<h3 class="widgettitle">',
'after_title' => '</h3>',
));
register_sidebar( //register second sidebar
array(
'name' => 'Second-right',
'before_widget' => '<div class="rightwidget">',
'after_widget' => '</div>',
'before_title' => '<h3 class="widgettitle">',
'after_title' => '</h3>',
));
?>
Following that, I created the files sidebar.php and sidebar-second.php to be able to call them.
sidebar.php
<div id="sidebar">
<?php if (!function_exists('dynamic_sidebar') || !dynamic_sidebar('Right-side')) : ?>
<h3 class="widget-title">No widget added</h3>
<p> Please add some widgets</p>
<?php endif; ?>
</div><!--ends sidebar-->
sidebar-second.php
<div id="sidebar">
<?php if (!function_exists('dynamic_sidebar') || !dynamic_sidebar('Second-right')) : ?>
<h3 class="widget-title">No widget added</h3>
<p> Please add some widgets</p>
<?php endif; ?>
</div><!--ends sidebar-->
And then I added replace my <?php get_sidebar() ; ?> statement with following conditional
<?php if( is_page('225') ) : ?>
<?php dynamic_sidebar('second'); ?>
<?php else : ?>
<?php get_sidebar() ; ?>
<?php endif ; ?>
However only the widgets added to the sidebar.php are displayed on every page. Any help on how I could change this, or pointers on what I could be doing wrong. Thanks.
I looks like you are using <?php dynamic_sidebar('second'); ?> where you should be using <?php get-sidebar('second'); ?>
What get_sidebar('foo') does is look for a file named 'sidebar-foo.php' in the root directory of your theme and includes it. dynamic_sidebar('bar'), on the other hand, looks for a sidebar registered with:
<?php register_sidebar( array( 'name' => 'bar' ... ) ) ?>
Hope this helps!
Why don't you use the Custom Sidebar Plugin?
It's really easy to use and does not need any coding
You have two sidebars. when you want to use the sidebar-second file on a particular page, instead of calling
<?php get_sidebar('second') ; ?>
TRY
<?php get_sidebar('sidebar-second.php') ; ?>
this should do the trick