Shubham Sharma, Author at Codeexecute

Install Laravel Latest Version with Composer

Laravel is open source and most popular PHP frameworks for rapid web applications development. laravel provides great features like authentication techniques , Artisan , Routing , Eloquent ORM, Query Builder , Blade Templating , Security and many other features thats why its the most popular PHP framework among developers.

In this tutorial we will learn How to install Laravel latest version with composer. so keep follow the below steps .

Installation

We need composer to install laravel. If you have already installed composer then skip this step and if you want to install composer then click here and follow steps.

After the composer installation, run the following command to install laravel installer.

composer global require laravel/installer

after this command to scaffold a new application run the following command

laravel new myproject

Alternatively you can also create a new project using create-project command. to create a new application through create-project , run the following command :-

composer create-project --prefer-dist laravel/laravel myproject

Our new laravel project is created and to start the server, run the following command

php artisan serve

this command will start server at http://127.0.0.1:8000. You can navigate to http://127.0.0.1:8000 or if you installed laravel into xampp then you can also navigate to http://localhost/YourProjectName/myproject/public :).

 

Import large csv data into Mysql within seconds.

Here is the PHP script that will import your large CSV file data within seconds .

Steps
1. Copy the below code and paste into your file and save your file with “.php” extension or you can download file directly from here.
2. Copy your csv file and paste into the same folder where your php file exists.
3. Now run your php file in browser.
4. Browser will show a form with some fields . you have to fill your database and table details and in “Name of the file” field you have to fill your csv file name.
5. Click on submit button.

It will take some seconds and your all csv data will import into your mysql table.

yourphpfile.php

<?php 
if(isset($_POST['username'])&&isset($_POST['mysql'])&&isset($_POST['db'])&&isset($_POST['username']))
{
$sqlname=$_POST['mysql'];
$username=$_POST['username'];
$table=$_POST['table'];
if(isset($_POST['password']))
{
$password=$_POST['password'];
}
else
{
$password= '';
}
$db=$_POST['db'];
$file=$_POST['csv'];
$cons= mysqli_connect("$sqlname", "$username","$password","$db") or die(mysql_error());
$result1=mysqli_query($cons,"select count(*) count from $table");
$r1=mysqli_fetch_array($result1);
$count1=(int)$r1['count'];
//If the fields in CSV are not seperated by comma(,)  replace comma(,) in the below query with that  delimiting character 
//If each tuple in CSV are not seperated by new line.  replace \n in the below query  the delimiting character which seperates two tuples in csv
// for more information about the query http://dev.mysql.com/doc/refman/5.1/en/load-data.html
mysqli_query($cons, '
    LOAD DATA LOCAL INFILE "'.$file.'"
        INTO TABLE '.$table.'
        FIELDS TERMINATED by \',\'
        LINES TERMINATED BY \'\n\'
')or die(mysql_error());
$result2=mysqli_query($cons,"select count(*) count from $table");
$r2=mysqli_fetch_array($result2);
$count2=(int)$r2['count'];
$count=$count2-$count1;
if($count>0)
echo "Success";
echo "<b> total $count records have been added to the table $table </b> ";
}
else{
echo "Mysql Server address/Host name ,Username , Database name ,Table name , File name are the Mandatory Fields";
}
?>



<html>
<head>
<title> csv2 sql</title>
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css">
</head>
<body>
<br>

</br>
<form class="form-horizontal" action="" method="post">
    <div class="form-group">
        <label for="mysql" class="control-label col-xs-2">Mysql Host name</label>
        <div class="col-xs-3">
        <input type="text" class="form-control" name="mysql" id="mysql" placeholder="">
        </div>
    </div>
    <div class="form-group">
        <label for="username" class="control-label col-xs-2">Username</label>
        <div class="col-xs-3">
        <input type="text" class="form-control" name="username" id="username" placeholder="">
        </div>
    </div>
    <div class="form-group">
        <label for="password" class="control-label col-xs-2">Password</label>
        <div class="col-xs-3">
        <input type="text" class="form-control" name="password" id="password" placeholder="">
        </div>
    </div>
    <div class="form-group">
        <label for="db" class="control-label col-xs-2">Database name</label>
        <div class="col-xs-3">
        <input type="text" class="form-control" name="db" id="db" placeholder="">
        </div>
    </div>
    
    <div class="form-group">
        <label for="table" class="control-label col-xs-2">table name</label>
        <div class="col-xs-3">
        <input type="name" class="form-control" name="table" id="table">
        </div>
    </div>
    <div class="form-group">
        <label for="csvfile" class="control-label col-xs-2">Name of the file</label>
        <div class="col-xs-3">
        <input type="name" class="form-control" name="csv" id="csv">
        </div>
        eg. yourfile.csv  (Your file must be in same folder where this script exist)
    </div>
    <div class="form-group">
    <label for="login" class="control-label col-xs-2"></label>
    <div class="col-xs-3">
    <button type="submit" class="btn btn-primary">Upload</button>
    </div>
    </div>
</form>
</div>

</body>
</html>
 

Create Custom Helper and call from view in cakephp 3.x

To create a custom helper in cakephp first create a file in Helper folder and Helper classes should be suffixed with ‘Helper’.

example- (YourProject/src/view/Helper/CustomHelper.php)

CustomHelper.php is our new helper with Helper suffix.

Now in CustomHelper.php you can create function like this example

<?php
namespace App\View\Helper;

use Cake\View\Helper;

class CustomHelper extends Helper {


	
	public function yourhelperfunction()
	{		
		//your function code here
	}
}
?>

How to call custom helper function in view file

To call custom helper function from view file use

  $getresponse = $this->Custom->yourhelperfunction();
 

Call Controller function from webroot file in Cakephp 3

In our example we call AppController function from webroot file. you can call any controller function from webroot file.

To Call Controller function from webroot file in Cakephp create a new file on webroot and paste this code-

<?php
namespace App\Controller;
use App\Controller\AppController;

if (!defined('DS')) {
define('DS', DIRECTORY_SEPARATOR);
}

require_once(dirname(dirname(__FILE__)).DS.'config'.DS.'bootstrap.php');
require_once(dirname(dirname(__FILE__)).DS.'src'.DS.'Controller'.DS.'AppController.php');

$user = new AppController();
$responseofappfunction = $user->yourAppControllerfunctionName();

?>
 

How to disable cut copy paste on webpage using jQuery

Here is the jQuery code to prevent users to cut copy and paste from our website.

1.  disable cut copy and paste on a specific element like a textbox

Step 1 . First we create a div and assign a unique id to this div .

<div id="targetElement">
this is our div content .
</div>

Step 2  We are using jQuery so we need to call jQuery library before our jquery code. in this tutorial we are using google cdn jquery library.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script>

 

Step 3. Now paste this code below your jquery library and this jquery code will prevent cut copy and paste from your specific element.

<script type="text/javascript">
$(document).ready(function(){
$('#targetElement').bind("cut copy paste",function(e) {
e.preventDefault();
});
});
</script>

here is the full source code

<div id="textField1">
this is our demo text.
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('#textField1').bind("cut copy paste",function(e) {
e.preventDefault();
});
});
</script>

2. Disable cut copy paste on webpage
We can disable cut copy and paste from our complete webpage by using this jQuery code.

$(document).ready(function () {
//Disable cut copy paste
$('body').bind('cut copy paste', function (e) {
e.preventDefault();
});
});

3. Disable right click on webpage to prevent cut copy and paste.
We can also disable right click on our webpage to prevent cut copy and paste from our webpage.to disable right click on webpage use this jQuery code.

//Disable mouse right click
$("body").on("contextmenu",function(e){
return false;
});

4. Disable cut copy paste using html
Yes, We can also use html and modify body tag to prevent cut copy and paste.

<body oncopy="return false" oncut="return false" onpaste="return false">

To disable mouse right click with html use modify you body tag like this.

<body onContextMenu="return false">

Thank You.