How to make 3 column layout layout in yii 2 - yii

In yii1 i was able to create additional layouts like column1, column2 on one page. The old method in yii 1 doesnt work.
how am i able to achieve this with yii2? say the page is divided into 3 columns one showing in, out and missed.
www\center\protected\views\layout\column1.php
<?php /* #var $this Controller */ ?>
<?php $this->beginContent('//layouts/main'); ?>
<div id="content">
<?php echo $content; ?>
</div><!-- content -->
<?php $this->endContent(); ?>
www\center\protected\views\layout\column2.php
<?php /* #var $this Controller */ ?>
<?php $this->beginContent('//layouts/main'); ?>
<div class="span-19">
<div id="content">
<?php echo $content; ?>
</div><!-- content -->
</div>
<div class="span-5 last">
<div id="sidebar">
<?php
$this->beginWidget('zii.widgets.CPortlet', array(
'title'=>'Operations',
));
$this->widget('zii.widgets.CMenu', array(
'items'=>$this->menu,
'htmlOptions'=>array('class'=>'operations'),
));
$this->endWidget();
?>
</div><!-- sidebar -->
</div>
<?php $this->endContent(); ?>
How can it be achieved in Yii 2.0 ?

You can use layout in yii2 by doing this:
1) set your view in layout at #app/views/layouts or if you in module in moduleBasePath - views/layouts
2)
<?php
use yii\helpers\Html;
/* #var $this yii\web\View */
/* #var $content string */
?>
<?php $this->beginPage() ?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8"/>
<?= Html::csrfMetaTags() ?>
<title><?= Html::encode($this->title) ?></title>
<?php $this->head() ?>
</head>
<body>
<?php $this->beginBody() ?>
<header>My Company</header>
<div class="span-19">
<?= $content ?>
</div>
<div class="span-5 last">
// Your code
</div>
<footer>© 2014 by My Company</footer>
<?php $this->endBody() ?>
</body>
</html>
<?php $this->endPage() ?>
3) You can access layout by adding
public $layout = 'newLayout';
In Your controller, or you can use it specific action like
$this->layout = 'newLayout';
Good luck

Related

Bootstrap col-6 col-md-4 dont work on phone

Hi first time posting here so be gentle :D
I'm working on my responsive design and this happen.
There is my code :
<div class="col-6 col-md-4 expertise">
<img src="<?= $value["skill_image"] ?>">
<h5>
<?= $value["skill_name"] ?>
</h5>
<?php
for ($i = 0; $i < $value["skill_level"]; $i++) {
?>
<span class="fa fa-star checked"></span>
<?php
}
for ($i; $i < 5; $i++) {
?>
<span class="fa fa-star"></span>
<?php
}
?>
</div>
So i expect my column to be half width on phone device but that didn't work.
Screen of what is happening
Any idea ?
If you want to have a look on the website : https://portfolio-sigier.com/
Thanks for all your answers in advance :)
You need a viewport metatag that defines the width and scale. Add the following to your head.
<meta name="viewport" content="width=device-width, initial-scale=1">

PHP variable auto update failure

I have this code that I want that $X to be updated in the form, so when I press the +1 button it just keep going adding 1 to the form
<html>
<head>
<title>7mada</title>
</head>
<body>
<?php $X = 7 ;?>
<form method="POST" >
<table border="0" width="60%">
<tr> <td width ="30%"> Number: </td><td><input type="test" name="newname" value="<?php echo $X ;?>" readonly></td></tr>
</table>
<input name="save" type="submit" value="+1"/>
</form>
<?php
if($_POST){
if($_POST['save'] == "+1"){
$_POST['newname']++;
echo $_POST['newname'];
$X = $_POST['newname'];
}
}else{echo "say smth";}
?>
</body>
</html>
thanks.
Hey Samy this is the answer for your question :D :D
It completely ignores HTML output, therefore PHP can't interact with events that should be javascript.
you have two options:
1-Do it in Javascript, Live
2-Do it in PHP, requires refresh
If you want to do it in JavaScript, you'd do this:
<html>
<body>
<button onclick="add()">Add</button>
<div id="showIt"><script type="text/javascript">document.write(x);</script></div>
</body>
<head>
<title></title>
<script type="text/javascript">
var x = 0;
function add() {
x++;
document.getElementById('showIt').innerHTML = x;
}
</script>
</head>
</html>
or in PHP:
<?php
session_start();
$_SESSION['x'] = ((isset($_SESSION['x'])) ? $_SESSION['x'] : 0);
if(isset($_GET['add'])){
$_SESSION['x']++;
}
?>
<html>
<head>
<title></title>
</head>
<body>
<form action="<? echo $_SERVER['PHP_SELF']; ?>" method="get">
<input type="submit" name="add" value="add" />
</form>
<?php
echo $_SESSION['x'];
?>
</body>
</html>
I hope this helps you,
Mahmoud Elsaadany

how get parameters from compose() in view Yii-2?

how get parameters from compose() in view Yii-2?
I try:
/controllers/SiteController
Yii::$app->mailer->compose('layouts/html.php', ['model' => 'trtrtrtrt',])
->setFrom('ergegergerger#gmail.com')
->setTo('ergergergegerg#mail.ru')
->setSubject('TEST')
->send();
mail/layouts/html.php
<?php
use yii\helpers\Html;
use yii\mail\BaseMailer;
/* #var $this \yii\web\View view component instance */
/* #var $message \yii\mail\MessageInterface the message being composed */
/* #var $content string main view render result */
?>
<?php $this->beginPage() ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=<?= Yii::$app->charset ?>" />
<title><?= Html::encode($this->title) ?>Тестовое письмо</title>
<?php $this->head() ?>
</head>
<body>
<?php $this->beginBody() ?>
<div style="background-color: green;">Приветик !</div>
<?= Html::encode($model) ?>
<?php $this->endBody() ?>
</body>
</html>
<?php $this->endPage() ?>
i get error "Undefined variable: model" - <?= Html::encode($model) ?>
how get parameters from compose()?
You are calling a parameter in the layout file. But the parameter model is available only in the view.
So you have to change the code:
mail/layouts/html.php
<?php
use yii\helpers\Html;
use yii\mail\BaseMailer;
/* #var $this \yii\web\View view component instance */
/* #var $message \yii\mail\MessageInterface the message being composed */
/* #var $content string main view render result */
?>
<?php $this->beginPage() ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=<?= Yii::$app->charset ?>" />
<title><?= Html::encode($this->title) ?>Тестовое письмо</title>
<?php $this->head() ?>
</head>
<body>
<?php $this->beginBody() ?>
<div style="background-color: green;">Приветик !</div>
<?= $content ?>
<?php $this->endBody() ?>
</body>
</html>
<?php $this->endPage() ?>
mail/emailview.php
<?= Html::encode($model) ?>
php code
Yii::$app->mailer->compose('emailview.php', ['model' => 'trtrtrtrt',])
->setFrom('ergegergerger#gmail.com')
->setTo('ergergergegerg#mail.ru')
->setSubject('TEST')
->send();

Yii, new controller render does not have the same layout like default

i dont get why the new controller im making which is rendering a file in an action does not seem to have the same layout in content like yii generates automatically, the site/index, just like contact and about have their content a bit centered in the content, like some padding or margin, but when i make a new controller, it does look the same, looks a bit ugly being stick to the line where the content begins.
heres the new controller im making:
<?php
class ToolsController extends CController
{
// -----------------------------------------------------------
// Uncomment the following methods and override them if needed
/*
public function filters()
{
// return the filter configuration for this controller, e.g.:
return array(
'inlineFilterName',
array(
'class'=>'path.to.FilterClass',
'propertyName'=>'propertyValue',
),
);
}
public function actions()
{
// return external action classes, e.g.:
return array(
'action1'=>'path.to.ActionClass',
'action2'=>array(
'class'=>'path.to.AnotherActionClass',
'propertyName'=>'propertyValue',
),
);
}
*/
public function actionIndex()
{
$this->render('index');
}
}
and its just rendering this:
<h1>hello</h1>
im missing something?
sorry heres the layout:
<?php /* #var $this Controller */ ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="language" content="en" />
<!-- blueprint CSS framework -->
<link rel="stylesheet" type="text/css" href="<?php echo Yii::app()->request->baseUrl; ?>/css/dist/css/bootstrap.css">
<!--[if lt IE 8]>
<link rel="stylesheet" type="text/css" href="<?php echo Yii::app()->request->baseUrl; ?>/css/ie.css" media="screen, projection" />
<![endif]-->
<link rel="stylesheet" type="text/css" href="<?php echo Yii::app()->request->baseUrl; ?>/css/main.css" />
<link rel="stylesheet" type="text/css" href="<?php echo Yii::app()->request->baseUrl; ?>/css/form.css" />
<title><?php echo CHtml::encode($this->pageTitle); ?></title>
</head>
<body>
<div class="container" id="page">
<div id="header">
<div id="logo"><?php echo CHtml::encode(Yii::app()->name); ?></div>
</div><!-- header -->
<div id="mainmenu">
<?php $this->widget('zii.widgets.CMenu',array(
'htmlOptions'=>array('class'=>'nav nav-pills'),
'items'=>array(
array('label'=>'Home', 'url'=>array('/site/index')),
array('label'=>'About', 'url'=>array('/site/page', 'view'=>'about')),
array('label'=>'Contact', 'url'=>array('/site/contact')),
array('label'=>'Tools', 'url'=>array('/tools/index')),
),
)); ?>
</div><!-- mainmenu -->
<?php if(isset($this->breadcrumbs)):?>
<?php $this->widget('zii.widgets.CBreadcrumbs', array(
'links'=>$this->breadcrumbs,
)); ?><!-- breadcrumbs -->
<?php endif?>
<?php echo $content; ?>
<div class="clear"></div>
<div id="footer">
Copyright © <?php echo date('Y'); ?> by My Company.<br/>
All Rights Reserved.<br/>
<?php echo Yii::powered(); ?>
</div><!-- footer -->
</div><!-- page -->
</body>
</ht
ml>
Quick fix is:
public $layout='//layouts/myLayout';
Pretty sure you can also set it in the Base Controller also

Multiple Google map with php loop

I am trying to dispaly multiple maps with a php loop but i cant get it to work here is the code stripped down...
<!doctype html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<title>Google Maps JavaScript API Example</title>
<script src="http://maps.google.com/maps?file=api&v=2&sensor=true&key=ABQIAAAA5iXFaQoABBiRl7JNx5HFuxSa5RD4FXABRIVtLveK1-E6gmai7BR1Y63hhzwAO9ZPoNDgLDBQ_Z6B4Q" type="text/javascript"></script>
</head>
<body onload="load()" onunload="GUnload()">
<?php
if($_POST['select'] == "place"){
$posts = $facebook->api("/search?q=".urlencode($_POST['search'])."&type=".urlencode($_POST['select'])."&center=".urlencode($_POST['center'])."&distance=".urlencode($_POST['distance'])."");
//print_r($posts);
$num = "1";
$num2 = "1";
foreach($posts as $v) {
foreach($v as $v2) { ?>
<div>
<script type="text/javascript">
//<![CDATA[
function load() {
if (GBrowserIsCompatible()) {
var map = new GMap2(document.getElementById("map-<?php echo $num++; ?>"));
map.setCenter(new GLatLng(<?php echo $v2['location']['latitude']; ?>, <?php echo $v2['location']['longitude']; ?>), 13);
}
}
//]]>
<div id="map-<?php echo $num2++; ?>" style="width: 500px; height: 300px"></div>
<p><?php echo $v2['location']['latitude']; ?></p>
<p><?php echo $v2['location']['longitude']; ?></p>
</div>
<?php
}
}
}
}
?>
</body>
</html>
I have tried to put the javascript within the loop but this doesnt work.
Has anyone got any suggestions at the moment it will only show the last map within the loop???
Thanks
I can tell you why it doesn't work. You're redefining the same load() function each time through the loop. So when document loads, and load() gets called, it just displays the last map. By the way, you should also be using google map V3 api instead of V2, but that's a different topic.
Something like this should fix this. First, put this in the <head> section:
<script type="text/javascript">
var loaders = [];
function load() {
for (var i = 1; i < loaders.length; i++) {
loaders[i]();
}
}
</script>
Then change your loop to this:
foreach($posts as $v) {
foreach($v as $v2) { ?>
<div>
<script type="text/javascript">
//<![CDATA[
loaders[<?php echo $num; ?>] = function() {
if (GBrowserIsCompatible()) {
var map = new GMap2(document.getElementById("map-<?php echo $num++; ?>"));
map.setCenter(new GLatLng(<?php echo $v2['location']['latitude']; ?>, <?php echo $v2['location']['longitude']; ?>), 13);
}
};
//]]>
<div id="map-<?php echo $num2++; ?>" style="width: 500px; height: 300px"></div>
<p><?php echo $v2['location']['latitude']; ?></p>
<p><?php echo $v2['location']['longitude']; ?></p>
</div>
<?php
}
}