Menus

Sunday, 6 March 2016

Installing Yii





Yii 2

The latest version of Yii 2 is 2.0.7, released on February 14, 2016. Yii 2.0 is a complete rewrite of Yii on top of PHP 5.4.0. It is aimed to become a state-of-the-art of the new generation of PHP framework. Yii 2.0 is not compatible with 1.1.
Yii2 Website.
Yii2 Documentation.

Installing Yii

  1. Installing via Composer
  2. Installing from an Archive File
  3. Other Installation Options
  4. Verifying the Installation
  5. Configuring Web Servers

You can install Yii in two ways, using the Composer package manager or by downloading an archive file. The former is the preferred way, as it allows you to install new extensions or update Yii by simply running a single command.


Steps - Install from an Archive File
  • Yii 2 with advanced application template       (Download one of the following archive files, and then extract it to a Web-accessible folder (eg /var/www/html).)
  • Yii Application Initialization.     eg var/www/html/yii2.7# php init
Open your browser goto  http://localhost/yii2.7/frontend/web/index.php
Get result is


Database Configuration
  1. Preparing the Database
  2. Configuring a DB Connection
  3. Creating an Active Record
  4. Creating an Action
  5. Creating a View

* Database configuration placed in the common/config directory of application folder.
/application/common/config/main_local.php

Example of set database configuration 
'db' => [
            'class' => 'yii\db\Connection',
            'dsn' => 'mysql:host=localhost;dbname=students',
            'username' => 'root',
            'password' => 'root123',
            'charset' => 'utf8',
        ],

More Details

Migrate Database
yii basic working tables add to own database
This will create tables needed for the application to work.

Command for migration
php yii migrate

Video Installing the Advance Template in Yii



GETTING STARTED
After you install the application, you have to conduct the following steps to initialize
the installed application. You only need to do these once for all.
1. Run command `init` to initialize the application with a specific environment.
2. Create a new database and adjust the `components['db']` configuration in `common/config/main-local.php` accordingly.
3. Apply migrations with console command `yii migrate`. This will create tables needed for the application to work.
4. Set document roots of your Web server:
- for frontend `/path/to/yii-application/frontend/web/` and using the URL `http://frontend/`
- for backend `/path/to/yii-application/backend/web/` and using the URL `http://backend/`
To login into the application, you need to first sign up, with any of your email address, username and password.Then, you can login into the application with same email address and password at any time.


Thursday, 10 December 2015

Array of database result to mannually display in table


Generate report of table format.

Code placed on controller action
Objective of this report for generating month wise admission count each year.

               $sql = $sql = "SELECT YEAR( date_of_join ) as eyear , MONTH( date_of_join ) as emonth , COUNT(  `admission_number` ) as totalstudents FROM  `students` WHERE YEAR(  `date_of_join` ) BETWEEN 2013 AND 2015 GROUP BY YEAR( date_of_join ) , MONTH( date_of_join ) ORDER BY YEAR( date_of_join ) , MONTH( date_of_join ) ASC ";                        
               
              $models = Students::findBySql($sql)->all();


Above this query generating result as 


eyearemonthtotalstudents
20131195
20132147
20133171
20134206
20135303
20136286
20137186
20138181
20139231
201310278
201311398
201312296
20141179
2014269
20143117
20144134
20145235
20146422
20147212
20148271
20149378
201410451
201411339
201412289
20151188
20152220
20153198
2015484
20155165
20156402
$model array represent the above result.

I want to expect result is 


COMPARISON REPORT
LOGICLAB DISTANCE EDUCATION
Year/MonthJanFebMarAprMayJunJulAugSepOctNovDecTotal
20131951471712063032861861812312783982962878
2014179691171342354222122713784513392893096
20151882201988416540226517421011700
Total56243648642470311106636266117307375867674



Answer 

One Solution


<?php

use yii\helpers\Html;
use yii\helpers\ArrayHelper;

$this->title = 'Comparison Report';
$this->params['breadcrumbs'][] = ['label' => 'Comparison Report Criteria', 'url' => ['studentreport/comparisonrpt']];
$this->params['breadcrumbs'][] = $this->title;

//set heading. 
$caption = 'COMPARISON REPORT';
$caption .=  '<BR>'.Yii::$app->params['center_name'];
$caption .=  ' '.Yii::$app->params['center_caption'];

?>
          
<!-- Main content -->
<section class="invoice">
            
     <table  class="table" >           
                <tr style= "background-color: #bfcfd2" ><td colspan="14"><b><?=$caption?></b></td></tr>                    
                <tr style= "background-color: #F2F2F2"><td>Year/Month</td>
                        <td>Jan</td>
                        <td>Feb</td>
                        <td>Mar</td>
                        <td>Apr</td>
                        <td>May</td>
                        <td>Jun</td>
                        <td>Jul</td>
                        <td>Aug</td>
                        <td>Sep</td>
                        <td>Oct</td>
                        <td>Nov</td>
                        <td>Dec</td>
                        <td>Total</td> </tr>
                        
 <?php                
                //total colums print with show university
                $dataList=ArrayHelper::map($model, 'eyear', 'eyaer');
                foreach ($dataList as $key => $value) {
                    echo '<tr><td>'.$value.'</td>';
                    for($i=1;$i<=12;$i++){
                        echo '<td id='.$value.$i.'>0</td>';
                    }
                    echo '<td id='.$value.'>0</td></tr>';  //for print row toatal.
                }
                    
                 //bootam row.
                echo '<tr style= "background-color: #4CAF50"><td>Total</td>';
                for($i=1;$i<=12;$i++){
                    echo '<td id='.$i.'>0</td>';
                }   
                echo '<td id="gtotal">0</td></tr>';
                                
                //1.isplay University counts, 2.row total, 3.column total, 4. Grand total. 
                foreach ($model as $key => $value) {                    
                    echo "<script> document.getElementById('".$value['eyear'].$value['emonth']."').innerHTML = ".$value['totalstudents']." </script>";                    
                    echo "<script> "                    
                    . "document.getElementById('".$value['eyar']."').innerHTML = parseInt(document.getElementById('".$value['eyear']."').innerHTML) + ". $value['totalstudents'].";"
                    . "document.getElementById('".$value['emonth']."').innerHTML = parseInt(document.getElementById('".$value['emonth']."').innerHTML) + ". $value['totalstudents'].";"
                    . "document.getElementById('gtotal').innerHTML = parseInt(document.getElementById('gtotal').innerHTML) + ". $value['totalstudents']        
                    . "</script>";                                        
                }
                ?>
                </table>            
        </section>