Skip to content

Instantly share code, notes, and snippets.

@jimgwhit
jimgwhit / BlogController.php
Created July 29, 2021 18:58 — forked from tobysteward/BlogController.php
Laravel AJAX Pagination with JQuery
<?php
class BlogController extends Controller
{
/**
* Posts
*
* @return void
*/
public function showPosts()
@jimgwhit
jimgwhit / db.php
Created May 16, 2021 20:29 — forked from luckyshot/db.php
PHP/MySQL (PDO Database) method with named parameters
<?php
/*
PHP/MySQL (PDO) method with named parameters
---------------------------------------------
https://gist.github.com/luckyshot/9477105
Last updated: 12 Sep 17
$config = [
@jimgwhit
jimgwhit / Fetch_example.md
Last active June 23, 2021 20:02
Fetch simple example

Just test data and uptating one field using put:

    document.getElementById('submitBtn').addEventListener('click', submitPost);
    function submitPost(e) {
        e.preventDefault();
        const data = {petid: document.getElementById('petid').value,
            species: document.getElementById('species').value,
            _token: document.getElementsByName("_token")[0].value,
            _method: document.getElementsByName("_method")[0].value};
@jimgwhit
jimgwhit / checkbox_array.md
Last active June 6, 2022 19:12
Checkbox array

First, in html if a checkbox is not checked nothing is passed for that checkbox in the request.

For example if you had a form with name and likes, (pick from the following)

Name  ________________

I like:
On a PHP coding forum someone ask how to loop a certain array.
The data was in Json format (not shown), but this will give a good idea of how to handle looping an array.
I just named the Json data $mydata.
First json decode it:
```
$decoded = json_decode($mydata, true);
// I just put the json in the variable $mydata to work with.
@jimgwhit
jimgwhit / Json_response.md
Created June 30, 2020 01:47
Json response with Jquery

Just quick examples, so apply validation and authentication and authorization as needed.

For returning a single record:

    public function testc() {
        $dogid = Request::input('somevar');
        $dog = Dog::find($dogid)->toArray();
        return Response::json($dog);
    }
@jimgwhit
jimgwhit / Upload.md
Created June 14, 2020 19:49
Image upload
// In an update method
public function add()
{
    // Validate as needed
    $lid=DB::table('dc_dogs')->count();
    $lid=$lid+1;
    $file=Request::file('ufile');
    $file_name=$file->getClientOriginalName();
 $file_ext=$file-&gt;getClientOriginalExtension();

At times on a shared host you may not be able to use a symbolic link. I sometimes just display from a folder outside of web folder:

A bare basic example, this file just name it DisplayImage.php

<?php
$basedir = '/some_folder/images'; // on hard drive
$imagedir = $_GET['dir'];
$image = $_GET['img'];

Anyone new to RBAC I highly suggest using laravel's or Spatie https://github.com/spatie/laravel-permission

However being familiar with RBAC I use built in authentication but have custom authorization.

I use static helper classes, but instance will also work. And these are just simple examples of making sure a required role of a method matches with one of the logged in users role.

I have a role field in users table like:

   role 
entity code:
<?php
namespace App\Model\Entity;
use Cake\ORM\Entity;
use Cake\Utility\Inflector;
/**
* Pet Entity.
*/