Skip to content

Instantly share code, notes, and snippets.

View cp6's full-sized avatar
🏠
Working from home

corbpie cp6

🏠
Working from home
View GitHub Profile
@cp6
cp6 / pdo_insert_loop.php
Last active April 5, 2021 11:41
PHP PDO insert from a loop
<?php
$db = new PDO("mysql:host=127.0.0.1;dbname=test;charset=utf8mb4", 'root', '');
$db->beginTransaction();
$insert = $db->prepare("INSERT IGNORE INTO `objects` (`id`, `color`) VALUES (?, ?);");
$test_array = ['Red', 'Orange', 'Pink', 'Lime', 'Yellow', 'Gold', 'Green', 'Blue', 'Purple', 'Maroon', 'Silver', 'Aqua'];
$id = 0;
foreach ($test_array as $c) {
@cp6
cp6 / pdo_mysql_backup.php
Last active February 20, 2024 12:41
PHP PDO MySQL backup script with compression
<?php
$backup_config = array(
'DB_HOST' => '127.0.0.1',////Database hostname
'DB_NAME' => 'test_db',//Database name to backup
'DB_USERNAME' => 'root',//Database account username
'DB_PASSWORD' => '',//Database account password
'INCLUDE_DROP_TABLE' => false,//Include DROP TABLE IF EXISTS
'SAVE_DIR' => '',//Folder to save file in
'SAVE_AS' => 'test_db-',//Prepend filename
'APPEND_DATE_FORMAT' => 'Y-m-d-H-i',//Append date to file name
@cp6
cp6 / bar_chart.php
Last active January 27, 2022 06:28
PHP MySQL single chart.js bar chart
<?php
//Config options for chart data
$player_id = 202331;//Paul George player id
$season_type = 2;//2 is regular season, 3 is playoffs
//Design / Display options:
$bootstrap_css_url = 'https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.6.0/css/bootstrap.min.css';
$chartjs_js_url = 'https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.4/Chart.min.js';
$background_color = '#7bbbbb';//Page background color
@cp6
cp6 / pdo_php_guide.md
Created February 16, 2021 10:10
PHP PDO MySQL Cheat sheet guide

PHP PDO MySQL Cheat sheet guide

A guide on the basics for using PDO PHP for MySQL with pre-prepared statements.

Table of Contents

  1. Creating connection

    1a inline

@cp6
cp6 / api.php
Created February 7, 2021 02:16
Authenticating and protecting Ajax requests with PHP
<?php
if (session_status() == PHP_SESSION_NONE) {
session_start();//Start session if none exists/already started
}
if (isset($_POST['ajax_call']) || isset($_GET['ajax_call'])) {
$headers = getallheaders();
if (isset($headers['token'])) {
$header_token = $headers['token'];
if ($header_token != $_SESSION['token']) {
@cp6
cp6 / content.html
Last active January 5, 2021 12:37
Simple Bootstrap WordPress blog template files
<article>
<div class="container">
<div class="row">
<div class="col-lg-8 col-md-10 mx-auto">
<div class="post-heading">
<h1>Man must explore, and this is exploration at its greatest</h1>
<h2 class="subheading">Problems look mighty small from 150 miles up</h2>
<span class="meta">Posted by
<a href="#">Boss</a>
on January 5, 2021</span>
@cp6
cp6 / index.html
Last active January 4, 2021 05:23
Simple Bootstrap WordPress blog template files
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="description" content="">
<meta name="author" content="">
@cp6
cp6 / get_params.php
Created October 17, 2020 05:06
PHP build URL from GET paramaters
<?php
$params = $_GET;
$count = 0;
foreach ($params as $key => $value) {
if ($count === 0) {
echo "?{$key}={$value}";
} else {
echo "&{$key}={$value}";
}
//echo "$key : $value<br>";
@cp6
cp6 / form.html
Created October 7, 2020 12:21
HTML form inputs based on a select element
<div class="container">
<div class="row">
<div class="col-lg-12">
<div class="card">
<div class="card-header"><h2 class="text-center">Input based on select</h2></div>
<div class="card-body"><p class="lead text-center">Example of a select form with an input appearing
based on the selection.</p>
<p class="text-center">Change the select input below to see the changes</p>
<form id="submit-form" method="post" action="" role="form">
<div class="row">
@cp6
cp6 / calc_age.php
Created September 24, 2020 06:04
Calculate age PHP function
<?php
function ageForDate(string $dob, string $date = 'today', bool $years_only = true): string
{
$from = new DateTime($dob);
$to = new DateTime($date);
$interval = $from->diff($to);
if ($years_only) {
return $from->diff($to)->y;//years
} else {
return $from->diff($to)->y . '.' . $from->diff($to)->m;//years.months