Wednesday 26 November 2014

resize image in php

First you enable following extension in php.ini file  if exist else install php imagick in 
you server :-

set in php.ini file :-

extension=php_imagick.dll


<?php

$thumb = new Imagick();

$thumb->readImage(product_image1);
$thumb->resizeImage(100,100,Imagick::FILTER_LANCZOS,1);
$thumb->writeImage('new100x100.jpeg');


$thumb->readImage(product_image1);
$thumb->resizeImage(200,200,Imagick::FILTER_LANCZOS,1);
$thumb->writeImage('new200x200.jpeg');
$thumb->destroy();

?>

Saturday 22 November 2014

Add calendra in text box jquery

<link rel="stylesheet" href="http://code.jquery.com/ui/1.9.2/themes/base/jquery-ui.css" />
<script src="http://code.jquery.com/jquery-1.8.3.js"></script>
<script src="http://code.jquery.com/ui/1.9.2/jquery-ui.js"></script>

<script>
var j = jQuery.noConflict();
    j(function() {
        j( "#datepicker" ).datepicker();
    });
</script>

HTML:- <input type="text" id="datepicker" />

form value hold after page redirect in laravel



return Redirect::to('admin/employee/addnew')->withInput()->withErrors($validation);

how to change server php version through htaccess in laravel

paste following code in your root htaccess file :-

AddHandler application/x-httpd-php54 .php
suPHP_ConfigPath /opt/php54/lib

how to define error pages in laravel

First you open app/start/global.php file

find below function :-

App::error(function(Exception $exception, $code)
{
    Log::error($exception);

And last set following condition :-

if ( ! in_array($code,array(401,403,404,500))){
       return;
    }

    $data = array('code'=> $code);

    switch ($code) {

case 401:

         return Response::view('errors.err401', $data, $code); ///set path of app/views/errors/err401.blade.php

       break;     

case 402:

         return Response::view('errors.err402', $data, $code);

       break;      

   case 403:

         return Response::view('errors.err403', $data, $code);

       break;

       case 404:

         return Response::view('errors.err404', $data, $code);

       break;

   }
  
   });

how to use session in laravel

1. save value in session :-
Session::put('key', 'value');

2. get value of session:-

Session::get('key', 'value');

3. remove value of perticuler key :-

Session::forget('key', 'value');

4. Destroy session value :-

Session::flush();

how to call view page in controler in laravel


return View::make('pages.index');  //path :- views/pages/index.blade.php

how to set logout in laravel


 Auth::logout(); //(logout function)

            
  Session::flush(); // if above function give error then user this function

(Destroy all sessions)

Laravel default User login Code

Use following code in your post controller :-


if (Auth::attempt(array('email'=>Input::get('email'), 'password'=>Input::get('password'))))
             {

            return Redirect::to('admin/dashboard')->with('message', 'You are now logged in!');
         }
       
        else {
           
                   return Redirect::to('admin')->with('message', 'Your username/password combination was incorrect');
       
             }

how to check user login or not in laravel

if(Auth::check()){
       
         return View::make('user-login');
       
        }
        else {
        return Redirect::to('notlogin');
        }

get all information of online user in laravel


{{Auth::user()->user_id}},

{{Auth::user()->user_name}}

how to encode password for insert in db in laravel


Hash::make(Input::get('password'));

how to get all validation error message together in laravel

<span>
@foreach($errors->all(':message') as $message)
{{ $message }}
@endforeach
</span>

show validation error messages in view page in laravel

 Following code paste ( after or before) on  you form field where you want show message :-

 @if ($errors->has('fieldname')) <p class="">{{ $errors->first('fieldname') }} @endif

file upload in laravel

 Use this code in your controller :-

$fileName = Input::file('file')->getClientOriginalName();

$destinationPath = base_path().'/public/upload/';

Input::file('file')->move($destinationPath, $fileName);

form in laravel format

{{ Form::open(['url' => '/admin', 'method' => 'post']) }}

{{Form::text('username', null,array('placeholder' => 'Username','class' => 'form-control','id' => 'input-username'))}}

{{Form::password('password', null,array('placeholder' => 'Password','class' => 'form-control','id' => 'input-password'))}}

{{Form::submit('Login', array('name' => 'sub','class' => 'btn btn-primary'))}}

{{ Form::close() }}

controller value pass in view page in laravel

First you pass value in controller :-

return View::make('admin_pages.dashboard')->with('message',$message);

and get this messages on view page :-

<span>@if ($alert = Session::get('message'))
  
        {{ $alert }}
  
@endif </span>

Thursday 20 November 2014

how to set form validation in laravel

 In your controller set this code after form post :-


 $rules = array(
        'categoryname'                => 'required|alpha',
        'childrenacceptjesus'         => 'required|numeric',
        'file'                        => 'required|max:10000',
        'childrengraduated'           => 'required|numeric|max:450',
        'email'                       => 'required|email|unique:users',
        'username'                    => 'required|min:6|unique:users',
        'password'                    => 'required|confirmed|min:6'
        'pda'                         => 'required|unique:forms',
        'password'                    => 'required|min:8|confirmed',
        'password_confirmation'       => 'required|min:8',

    );

$data = Input::all();

$validation = Validator::make ($data, $rules);

        if ($validation->passes())
        {

                         //true

                } else

                      {
                           //false
                      }

how to set image path in laravel

 Normally :-

<img src="rootlaravel/asset/css/images/top.png">

in laravel :-

<img src="{{ asset('asset/css/images/top.png'); }}">

set path in laravel


{{ URL::to( 'admin/logout') }}


<a href="{{ URL::to( 'admin/logout') }}">Logout</a>

how to call css and js in laravel

 Path start from root directory :-

//like (laravel/admin_asset/css/style.css)

{{ HTML::style('admin_asset/css/style.css'); }}

{{ HTML::script('admin_asset/js/jquery.js'); }}

how to define dynamic title in laravel

First set section name (title) and value (Home page) in view pages  :-

@section('title')
Home page(define head title here)
@stop


And In  layout page call the title section :-
 <title>@yield('title')</title>

Remember which tab was active after refresh.

<link rel="stylesheet" href="http://code.jquery.com/ui/1.11.2/themes/smoothness/jquery-ui.css">
<script src="http://code.jquery.com/jquery-1.10.2.js"></script>
<script src="http://code.jquery.com/ui/1.11.2/jquery-ui.js"></script>
<script>

$(function() {
 
    var index = 'key';
  
    var dataStore = window.sessionStorage;
 
    try {
     
        var oldIndex = dataStore.getItem(index);
    } catch(e) {
     
        var oldIndex = 0;
    }
    $('#tabs').tabs({
      
        active : oldIndex,
      
        activate : function( event, ui ){
         
            var newIndex = ui.newTab.parent().children().index(ui.newTab);
         
            dataStore.setItem( index, newIndex )
        }
    });
    });
</script>

 /*-------------------------------------------------------------------------------------------------------------------------*/

<div id="tabs">
<ul>
<li><a href="#tabs-1">Lalit1</a></li>
<li><a href="#tabs-2">Lalit2</a></li>
<li><a href="#tabs-3">Lalit13</a></li>
</ul>
<div id="tabs-1">
<p>Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).</p>


</div>
<div id="tabs-2">
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. .</p>
</div>
<div id="tabs-3">
<p>It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English.</p>
</div>
</div>






Monday 17 November 2014

how to get date difference in javascript

<script>

function calcDate() {
var today = new Date();
var past = new Date(2010,05,01);
    var diff = Math.floor(today.getTime() - past.getTime());
    var day = 1000 * 60 * 60 * 24;

    var days = Math.floor(diff/day);
    var months = Math.floor(days/31);
    var years = Math.floor(months/12);

    var message = past.toDateString();
    message += " was "
    message += days + " days "
    message += months + " months "
    message += years + " years ago \n"

  alert(message);
  return false;
    }
</script>

Thursday 13 November 2014

detect copy, paste and cut behavior with jQuery

 <script type="text/javascript" src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
        <style type="text/css">
        span{
            color:green;
            font-weight:bold;
        }
        </style>

 <label>Text Box : </label>
            <input id="textA" type="text" size="50" value="Copy, paste or cut any text here" />



<script type="text/javascript">
        $(document).ready(function() {
            $("#textA").bind({
                copy : function(){
                $('#message').text('copy behaviour detected!');
                },
                paste : function(){
                return false;  /*you can't paste in textbox*/
                //$('#message').text('paste behaviour detected!');
                },
                cut : function(){
                $('#message').text('cut behaviour detected!');
                }
            });
        });
        </script>

Saturday 8 November 2014

Drag and drop div into another div and change order through ajax

First create two tables table_name(fields = 'id','name','sort_order') and second is table_name(fields = 'id','t_id','name','sort_order').
create db connection in db file:-

in db.php file :-

<?php
mysql_connect("localhost", "root", "") or die ("Error connecting to mysql");
mysql_select_db("db_name");
?>
/*------------------------------------------------------------------------------------------*/
in index.php file :-

<?php
include("db.php");
?>
<!doctype html>

<html lang="en">
<head>
    <title>jQuery UI Sortable - Connect lists</title>
    <link rel="stylesheet" href="http://code.jquery.com/ui/1.9.2/themes/base/jquery-ui.css" />
    <script src="http://code.jquery.com/jquery-1.8.3.js"></script>
    <script src="http://code.jquery.com/ui/1.9.2/jquery-ui.js"></script>

    <style>
    .connectedSortable{min-height:100px;min-width:400px;list-style-type: none; margin: 0; padding: 0 0 2.5em; float: left; margin-right: 10px; }
   
    .connectedSortable li { margin: 0 5px 5px 5px; padding: 5px; font-size: 1.2em; width: 390px; }
    .sec h3 {
    background: none repeat scroll 0 0 #40E0D0;
    font-size: 22px;
    height: 47px;
    margin-left: 10px;
    padding: 0 7px;
    width: 372px;
    text-align:center
}
.sec {
    background-color: #9ACD32;
    float: left;
    margin-left: 10px;
    width: 413px;
}
.trans {
    -ms-transform: rotate(5deg);
    -webkit-transform: rotate(5deg);
    transform: rotate(5deg);
}
    </style>
    <script>
    $(function() {
        $( ".connectedSortable" ).sortable({
 cursor: "move",
            connectWith: ".connectedSortable",
            receive: function(event,ui){
                 console.log("old ui id = "+ui.sender.attr("id")+" new ul id = "+this.id+" li id "+$(ui.item).attr("id"));  
                                
                 if(ui.sender.attr("id") != this.id)
                 {

                  $(ui.item).removeClass("trans");
                  $.ajax({
                    type: "POST",
                    url: "logic.php",
                  data: { start: ui.sender.attr("id"), drop: this.id , id:$(ui.item).attr("id") }
                })/*.done(function( msg ) {
                              alert( "Data Saved: " + msg );
                            })*/;
                
                 }
                 
               
             } ,
           
    start: function( event, ui ) {
   
    ui.helper.addClass("trans");
   
   
   
    },
    stop: function( event, ui ) {
   
    $(this).find('li,div').removeClass("trans");
   
   
   
    }
    }).disableSelection();
       
        $('.connectedSortable , .team_sec ').sortable({
   
    update: function (event, ui) {
        var data = $(this).sortable('serialize');
  
   $.ajax({
                    type: "POST",
                    url: "order_change.php",
                  data: {id:$(ui.item).attr("id") , prev:ui.item.prev().attr('id'),team:this.id,data: $(this).sortable('serialize') }
                })/*.done(function( msg ) {
                              alert( "Data Saved: " + msg );
                            });*/
    }
});




       
    });
   
   
    </script>
</head>
<body>
<div class="team_sec">
<?php
$team_res = mysql_query("select * from team order by sort_order");
while($team = mysql_fetch_assoc($team_res))
{
?>

<div class="sec" id="sec_<?php echo $team['id'];?>">
<h3><?php echo $team['name'];?></h3>

<ul id="<?php echo $team['id'];?>" class="connectedSortable">

<?php
$ids = $team['id'];
$task_res = mysql_query("select * from task where t_id = $ids order by sort_order");
while($task = mysql_fetch_assoc($task_res))
{
?>
    <li id="<?php echo $task['id'];?>_<?php echo $team['id'];?>" value="<?php echo $team['id'];?>" class="ui-state-default"><?php echo $task['id'];?>__<?php echo $task['name'];?></li>
 <?php
 }
 ?>
</ul></div>
<?php } ?>
</div>
</body>
</html>

/*------------------------------------------------------------------------------------------*/

creat a php file for ajax request like logic.php :-

<?php
include("db.php");

$arid = explode('_',$_POST['id']);
$id = $arid[0];
$tid = $_POST['drop'];
$task_res = mysql_query("update task set t_id = '$tid' where id = $id");

?>
/*------------------------------------------------------------------------------------------*/
creat another php file for second ajax request like order_change.php :-

<?php
include("db.php");

$secid = explode('_',$_POST['id']);
$arr = explode('&',$_POST['data']);
if($secid[0]=="sec")
{

$count = 1;
foreach($arr as $ordr)
{

$ordid = explode('=',$ordr);
 $o_id = $ordid[1];
$task_res = mysql_query("update team set sort_order = '$count' where id = $o_id");
$count++;
}

}else
 {

 $arid = $_POST['team'];

$count = 1;
foreach($arr as $ordr)
{

$ordid = explode('[',$ordr);
 $o_id = $ordid[0];
$task_res = mysql_query("update task set sort_order = '$count' where t_id = $arid and id = $o_id");
$count++;
}
}
?>
/*-----------------------------------------end----------------------------------------------*/

Thursday 6 November 2014

manage multiple category tree in single table in php

 Create a table with catgory_id,name,parent,sort_order field

<?php
$g_link = mysql_connect( 'localhost', 'root', '');
        mysql_select_db('your_database_name');
       
       
$result = mysql_query("SELECT category_id,name,parent,sort_order FROM category ORDER BY parent,sort_order,name");
 
 
$category = array('categories' => array(),'parent_cats' => array());
 
        while ($row = mysql_fetch_assoc($result)) {
            
            $category['categories'][$row['category_id']] = $row;
            
            $category['parent_cats'][$row['parent']][] = $row['category_id'];
        }
        ?>


/*This function used for show add category (in select box for select parent category) in menu*/ 


<select name="cat">
<option value="0"></option>
<?php

function buildCategory($parent, $category,$name) {
            $html = "";
            if (isset($category['parent_cats'][$parent])) {
                
                foreach ($category['parent_cats'][$parent] as $cat_id) {
                    if (!isset($category['parent_cats'][$cat_id])) {
                        $html .= "<option value='".$category['categories'][$cat_id]['category_id']."'>" . trim($name."->". $category['categories'][$cat_id]['name'],"->") . "</option>";
                    }
                    if (isset($category['parent_cats'][$cat_id])) {
                        $html .= "<option value='".$category['categories'][$cat_id]['category_id']."'>" . trim($name."->".$category['categories'][$cat_id]['name'],"->") . "</option>";
                        $html .= buildCategory($cat_id, $category,trim($name."->".$category['categories'][$cat_id]['name'],"->"));
                        $html .= "</option>";
                    }
                }
                
            }
            return $html;
        }
        echo buildCategory(0, $category ,'');
?>
</select>
<?php
        echo '<br><br>';
        /*--------------------------------end-------------------------------------------------------------------------------------------------------------------------*/

/*This function used for show all categories in menu*/ 


function buildCategory1($parent, $category) {
            $html = "";
            if (isset($category['parent_cats'][$parent])) {
                $html .= "<ul>\n";
                foreach ($category['parent_cats'][$parent] as $cat_id) {
                    if (!isset($category['parent_cats'][$cat_id])) {
                        $html .= "<li>\n  <a href='#'>" .$category['categories'][$cat_id]['name'] . "</a>\n</li> \n";
                    }
                    if (isset($category['parent_cats'][$cat_id])) {
                        $html .= "<li>\n  <a href='#'>" .$category['categories'][$cat_id]['name'] . "</a> \n";
                        $html .= buildCategory1($cat_id, $category);
                        $html .= "</li> \n";
                    }
                }
                $html .= "</ul> \n";
            }
            return $html;
        }
        echo buildCategory1(0, $category);
/*--------------------------------end-------------------------------------------------------------------------------------------------------------------------*/

?>