Results: 1580
Notes
  • Newest first
  • Oldest first
  • Newest first(All)
  • Oldest first(All)
Calculate the similarity between two strings and return the matching characters:
<?php
echo similar_text("Hello World","Hello Peter");
?>
by გიორგი ბაკაშვილი
4 years ago
0
PHP
String
PHP official doc
3
copy text to clipboard with break lines
If we need to copy text to clipboard with break lines, than we need to use
textarea
instead of using
input
element
function copy(part1, part2) {

    // Creates textarea element 
    var textarea = document.createElement('textarea');

    // Puts the text into the textarea with breaklines
    textarea.value = part1 + "\n\n" + part2

    // Adds the element to the DOM
    document.body.appendChild(textarea);

    // Selects the text that we want to copy
    textarea.select();

    // Copies the selected text clipboard
    var result = document.execCommand('copy');

    // Removes the element from the DOM because we no longer need it
    document.body.removeChild(textarea);
    return result;
}
by Valeri Tandilashvili
4 years ago
0
JavaScript
3
upload multiple files at once and validate file mimes on front / back
In order to upload several files at once we need to specify
multiple
attribute and append
[]
to input name
<input type="file" multiple="multiple" name="files[]" placeholder="Attach file"/>
To validate show errors on client side we have to append input name with
.*
Complete example of the
form-group
<div class="form-group">
    <label for="files">Attach File</label> &nbsp;
    <i id="add_new_file" class="ki ki-plus text-success pointer" title="Add New File"></i>
    <input type="file" class="form-control mb-2 {{ $errors->has('files.*') ? 'is-invalid' : '' }}" multiple="multiple" name="files[]" placeholder="Attach file"/>
    @if($errors->has('files.*'))
        <div class="invalid-feedback">
            <strong>{{ $errors->first('files.*') }}</strong>
        </div>
    @endif
</div>
Server side validation of mime types
public function rules()
{
    return [
        'comment' => 'sometimes|nullable|min:2|max:255',
        'files.*' => 'sometimes|nullable|mimes:pdf,docx,xlsx,pptx,rar,zip,png,jpg',
    ];
}
It's necessary the validator key
files
to be appended with the same symbols
.*
by Valeri Tandilashvili
4 years ago
0
Laravel
validation
3
recommended
getBoundingClientRect
method
let element = document.getElementById("some-id"); // for jQuery $("#some-id")[0];

let elementRect = element.getBoundingClientRect();
returns
DOMRect
Object
tooltipRect: DOMRect
bottom: -5
height: 20
left: 1093.5625
right: 1149.59375
top: -25
width: 56.03125
x: 1093.5625
y: -25
__proto__: DOMRect
bottom: -5   //invoked
height: 20   //invoked
left: 1093.5625   //invoked
right: (...)
top: (...)
width: (...)
x: (...)
y: (...)
by გიორგი უზნაძე
4 years ago
0
JavaScript
DOM
3
copy text to clipboard from Dom Element
<div id="some-id">Text to copy</div>
copyToClipboard('some-id');
function copyToClipboard(containerid) {
  if (document.selection) {
    let range = document.body.createTextRange();
    range.moveToElementText(document.getElementById(containerid));
    range.select().createTextRange();
    document.execCommand("copy");
  } else if (window.getSelection) {
    let range = document.createRange();
    range.selectNode(document.getElementById(containerid));
    window.getSelection().removeAllRanges();
    window.getSelection().addRange(range);
    document.execCommand("copy");
    window.getSelection().removeAllRanges();
  }
}
by გიორგი უზნაძე
4 years ago
0
JavaScript
DOM
3
The function gets data table, passes to recursive function to get associative array back. Encodes the array to JSON and returns back to user:
public function getCategoryTreeJSON()
{
    $query = "  SELECT
                    c1.id,
                    c1.name,
                    c1.parent_id,
                    (
                        SELECT
                            COUNT(c2.id) 
                        FROM
                            categories AS c2 
                        WHERE
                            c2.parent_id = c1.id
                    )
                    AS children_count 
                FROM
                    categories AS c1";

    $categoryDetails = json_decode(json_encode(DB::select($query)), true);
    
    $array_tree = $this->getCategoryTreeArray($categoryDetails);

    return json_encode($array_tree);
}
Recursive function that returns multi level associative array. Base case: when
children_count
is equal to zero.
public function getCategoryTreeArray($items, $parent_id = 0)
{
    $branch = [];
    $array_JSON = [];  

    foreach ($items as $item) {
        if ($item['parent_id'] == $parent_id) {

            if ($item['children_count'] > 0) {
                
                $array_JSON[] = [
                    'id'=>$item['id'],
                    'title'=>$item['name'],
                    'subs'=>$this->getCategoryTreeArray($items, $item['id'])
                ];
                
            } else {
                
                $array_JSON[] = [
                    'id'=>$item['id'],
                    'title'=>$item['name']
                ];
            }
            
        }
    }
    return $array_JSON;
}
by Valeri Tandilashvili
4 years ago
0
MySQL
PHP
Tree Selector
3
define a route to this controller method
laravel 8
You can define a route to this controller method like so:
use App\Http\Controllers\UserController;

Route::get('/users', [UserController::class, 'index']);
Route::get('/user/{id}', [UserController::class, 'show']);
Resource Controllers
resourceful route to the controller:

use App\Http\Controllers\PhotoController;
Route::resource('photos', PhotoController::class);
------------------------------------------------------------------------------------
laravel  7, 6, 5
You can define a route to this controller method like so:
Route::get('users', 'UserController@index');
   Route::get('user/{id}', 'UserController@show');
Resource Controllers
resourceful route to the controller:
Route::resource('photos', 'PhotoController');
========================================= This single route declaration creates multiple routes to handle a variety of actions on the resource. The generated controller will already have methods stubbed for each of these actions. Remember, you can always get a quick overview of your application's by running the
route:list
Artisan command.
Actions Handled By Resource Controller

Verb	URI	Action	Route Name
GET	/photos	index	photos.index
GET	/photos/create	create	photos.create
POST	/photos	store	photos.store
GET	/photos/{photo}	show	photos.show
GET	/photos/{photo}/edit	edit	photos.edit
PUT/PATCH	/photos/{photo}	update	photos.update
DELETE	/photos/{photo}	destroy	photos.destroy
by გიორგი ბაკაშვილი
4 years ago
0
Laravel
Controllers
3
After the command
git commit
->
i
- to jump into insert mode ->
esc
- takes us out of insert mode ->
:w
- to save written comment ->
:q
- to quit ------------------------
:w
and
:p
can be combined into
:wq
by Valeri Tandilashvili
4 years ago
0
Git
Git & GitHub Crash Course For Beginners
3
Lists logged in user's posts on dashboard
$user_id = auth()->user()->id;
$user = User::find($user_id);
return view('dashboard')->with('posts', $user->posts);
by Valeri Tandilashvili
4 years ago
0
Laravel
models
Laravel From Scratch
3
git blame
Displays the
author and last commit information
for each line in a file, helping track the origin of changes. Create a file and add some content:
echo "Line 1" > myfile.txt
echo "Line 2" >> myfile.txt
echo "Line 3" >> myfile.txt
Add and commit the file:
git add myfile.txt
git commit -m "Initial commit"
Make changes to the file and commit them
Make changes to the file and commit them
git blame myfile.txt
02fa3e76 (Luka 2023-06-01 20:07:40 +0400 1) Line 1
02fa3e76 (Luka 2023-06-01 20:07:40 +0400 2) Line 2
02fa3e76 (Luka 2023-06-01 20:07:40 +0400 3) Line 3
91354ada (Luka 2023-06-01 20:08:10 +0400 4) Line 4
91354ada (Luka 2023-06-01 20:08:10 +0400 5) Line 4
7ba66ba6 (Luka 2023-06-01 20:08:23 +0400 6) Line 5
by Luka Tatarishvili
1 year ago
0
Git
commands
3
Results: 1580