Wednesday 3 August 2016

Export Excel using sql query

$select = $_REQUEST['sql'];

mysql_query('SET NAMES utf8;');
$export = mysql_query($select);
//$fields = mysql_num_rows($export); // thanks to Eric
$fields = mysql_num_fields($export); // by KAOSFORGE
//$report_title='<Row><Cell ss:StyleID="2"><Data ss:Type="String">dileep</Data></Cell><Row>';
$col_title='';
$data ='';
for ($i = 0; $i < $fields; $i++) {
    $col_title .= '<Cell ss:StyleID="2"><Data ss:Type="String">'.mysql_field_name($export, $i).'</Data></Cell>';
}

$col_title = '<Row>'.$col_title.'</Row>';

while($row = mysql_fetch_row($export)) {
    $line = '';
    foreach($row as $value) {
        if ((!isset($value)) OR ($value == "")) {
            $value = '<Cell ss:StyleID="1"><Data ss:Type="String"></Data></Cell>\t';
        } else {
            $value = str_replace('"', '', $value);
            $value = '<Cell ss:StyleID="1"><Data ss:Type="String">' . $value . '</Data></Cell>\t';
        }
        $line .= $value;
    }
    $data .= trim("<Row>".$line."</Row>")."\n";
}

$data = str_replace("\r","",$data);
date_default_timezone_set('Asia/Kolkata');
header("Content-Type: application/vnd.ms-excel;");
header("Content-Disposition: attachment; filename=export_".date('d-m-Y H:i:s').".xls");
header("Pragma: no-cache");
header("Expires: 0");

$xls_header = '<?xml version="1.0" encoding="utf-8"?>
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" xmlns:html="http://www.w3.org/TR/REC-html40">
<DocumentProperties xmlns="urn:schemas-microsoft-com:office:office">
<Author></Author>
<LastAuthor></LastAuthor>
<Company></Company>
</DocumentProperties>
<Styles>
<Style ss:ID="1">
<Alignment ss:Horizontal="Left"/>
</Style>
<Style ss:ID="2">
<Alignment ss:Horizontal="Left"/>
<Font ss:Bold="1"/>
</Style>

</Styles>
<Worksheet ss:Name="Export">
<Table>';

$xls_footer = '</Table>
<WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel">
<Selected/>
<FreezePanes/>
<FrozenNoSplit/>
<SplitHorizontal>1</SplitHorizontal>
<TopRowBottomPane>1</TopRowBottomPane>
</WorksheetOptions>
</Worksheet>
</Workbook>';

print $xls_header.$col_title.$data.$xls_footer;
exit;

Dynamic Box with JQuery

<script src="jquery-1.10.2.min.js" type="text/javascript" /></script>
<script>
$(document).ready(function(){
$(".addCF").click(function(){
$("#customFields").append('<tr valign="top"><td><select class="select" name="select[]"><option value="">-select-</option><option value="a">a</option><option value="b">b</option><option value="c">c</option></select> &nbsp; <input type="text" class="selval" name="selval[]" readonly /> &nbsp; <a href="javascript:void(0);" class="remCF">Remove</a></td></tr>');
});
$("#customFields").on('click','.remCF',function(){
$(this).parent().parent().remove();
});

});

$(document).on('change','.select',function() {
var selval = $(this).val();
$(this).parent('td').find('.selval').val(selval);
//alert(selval);
});
</script>



    <table class="form-table" id="customFields">
<tr valign="top">

<td>
<select class="select" name="select[]">
    <option value="">-select-</option><option value="a">a</option><option value="b">b</option><option value="c">c</option></select> &nbsp; <input type="text" class="selval" name="selval[]" readonly /> &nbsp;
<a href="javascript:void(0);" class="addCF">Add</a>
</td>
</tr>
</table>

Tuesday 2 August 2016

Yii CRUD Generator

This generator generates a controller and views that implement CRUD (Create, Read, Update, Delete) operations for the specified data model.
Model Class-      app\models\TahsileMaster
Search Model Class- app\models\TahsileMastersearch
Controller Class- backend\controllers\TahsileController
View Path-

Link for merg

Yii Set .htaccess for direct access project (frontend) & admin(backend)

Step 1.
Options +FollowSymlinks
RewriteEngine On

# deal with admin first
RewriteCond %{REQUEST_URI} ^/(admin) <------
RewriteRule ^admin/assets/(.*)$ backend/web/assets/$1 [L]
RewriteRule ^admin/css/(.*)$ backend/web/css/$1 [L]

RewriteCond %{REQUEST_URI} !^/backend/web/(assets|css)/  <------
RewriteCond %{REQUEST_URI} ^/(admin)  <------
RewriteRule ^.*$ backend/web/index.php [L]


RewriteCond %{REQUEST_URI} ^/(assets|css)  <------
RewriteRule ^assets/(.*)$ frontend/web/assets/$1 [L]
RewriteRule ^css/(.*)$ frontend/web/css/$1 [L]

RewriteCond %{REQUEST_URI} !^/(frontend|backend)/web/(assets|css)/  <------
RewriteCond %{REQUEST_URI} !index.php
RewriteCond %{REQUEST_FILENAME} !-f [OR]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ frontend/web/index.php

Note : if you are trying in local server then replace ^/ with ^/project_name/ where you see arrow sign. Remove those arrow sign <------ after setup is done.


Step 2.
create file Request.php in
common\components
and put below code:


<?php


namespace common\components;


class Request extends \yii\web\Request
{


   public $web;
   public $adminUrl;


   public function getBaseUrl()
   {
       return str_replace($this->web, "", parent::getBaseUrl()) . $this->adminUrl;
   }


   /*
     If you don't have this function, the admin site will 404 if you leave off
     the trailing slash.


     E.g.:


     Wouldn't work:
     site.com/admin


     Would work:
     site.com/admin/


     Using this function, both will work.
    */


   public function resolvePathInfo()
   {
       if ($this->getUrl() === $this->adminUrl)
       {
           return "";
       } else
       {
           return parent::resolvePathInfo();
       }
   }


}


Step 3.  put code
Installing component. Write below code in frontend/config/main.php and backend/config/main.phpfiles respectively.


//frontend, under components array
'request'=>[
    'class' => 'common\components\Request',
    'web'=> '/frontend/web'
],
'urlManager' => [
        'enablePrettyUrl' => true,
        'showScriptName' => false,
],

// backend, under components array
'request'=>[
    'class' => 'common\components\Request',
    'web'=> '/backend/web',
    'adminUrl' => '/admin'
],
'urlManager' => [
        'enablePrettyUrl' => true,
        'showScriptName' => false,
],
For Short URL:-
add rules in url manager
         'rules' =>
            [
                'country-add'=>'country/create',
                 'country-view/<id:\d>' => 'country/view', 
           ] 

Setup VueJS in Laravel

Setup VueJS in Laravel 1. You will need to install javascript dependencies by running the following command on your terminal.      npm i...