Skip to content

Instantly share code, notes, and snippets.

@AucT
AucT / arrayToHtmlTable.php
Created December 31, 2020 14:46
Get html table from array of assoc arrays (simple result set from mysql tables)
<?php
function arrayToTable(array $rows, string $tableAttributes = ''): string
{
if (!$rows) {
return '';
}
$rowsHeaders = array_keys($rows[0]);
$table = "<table {$tableAttributes}><tr>";
foreach ($rowsHeaders as $rowsHeader) {
@AucT
AucT / IndexNow.php
Created November 14, 2021 09:30
indexnow php example bing
<?php
class IndexNow
{
const LOG = true;
public static function submit(array $urls): void
{
$data = [
@AucT
AucT / index.html
Created December 7, 2016 09:08
jquery Ajax example
<!doctype html>
<html class="no-js" lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="x-ua-compatible" content="ie=edge">
<title>Simple Ajax Send Form</title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1">
@AucT
AucT / textarea_ctr_enter_submit_form.js
Created April 25, 2022 10:16
Pressing ctr+enter inside textarea will requestSubmitForm.
document.body.addEventListener('keydown', function (e) {
if (e.key === 'Enter' && e.target.localName === 'textarea' && (e.metaKey || e.ctrlKey) && e.target.form) {
e.target.form.requestSubmit();
}
});
@AucT
AucT / echo_selected_checked_helper.php
Created November 28, 2022 11:53
php echo selected / checked if equal
<?php
function echoIfEqual($value1, $value2, $echoText) {
if ($value1 == $value2) echo $echoText;
}
function echoIfEqualChecked($value1, $value2) {
echoIfEqual($value1, $value2, 'checked');
}
function echoIfEqualSelected($value1, $value2) {
echoIfEqual($value1, $value2, 'selected');
@AucT
AucT / Simple API.md
Last active November 30, 2022 04:46

Simple API - a mix of rest and RPC

There are a lot of time when you need a ton of actions but rest API docs with noun doesn't help much, and implementing proper RPC style will need a lot of changes and will not work for simple HTML forms.

Simple API spec

  1. HTTP GET for queries. GET requests are made in REST style.
@AucT
AucT / str_contains.php
Created December 11, 2022 07:19
str_icontains and case-insensitive str_icontains
<?php
// Polyfill for PHP 4 - PHP 7, safe to utilize with PHP 8
if (!function_exists('str_contains')) {
function str_contains (string $haystack, string $needle)
{
return empty($needle) || strpos($haystack, $needle) !== false;
}
}
@AucT
AucT / rename_files.php
Created December 11, 2022 15:44
copy this script to hugo content/posts folder and run php rename_files.php and you will get nicely named files with date prefix
<?php
//copy this script to hugo content/posts folder and run php rename_files.php and you will get nicely named files with date prefix
if (!function_exists('str_ends_with')) {
function str_ends_with(string $haystack, string $needle): bool
{
$needle_len = strlen($needle);
return ($needle_len === 0 || 0 === substr_compare($haystack, $needle, -$needle_len));
}
}
@AucT
AucT / hugonew.ps1
Last active December 12, 2022 10:19
powershell script to create new post for hugo with good file name like 2022-12-12_my-awesome-title
## This is my file hugonew
##
## 1. Place this file where your helper scripts are. For example I have dropbox folder "my_scripts".
## Then add this folder to path variable.
## 2. cd to folder of your hugo blog and type:
## hugonew 'my awesome new nice title'
##
## this will create file 2022-12-12_my-awesome-new-nice-title.md in content/posts folder (if today is 2022-12-12)
## Some dummy ConvertTo-Slug function. scroll to bottom to see code.
@AucT
AucT / pluck.php
Created December 18, 2022 11:10
pluck array
<?php
function pluckObject($arrayOfObjects, string $value, ?string $key = null): array
{
$data = [];
if ($key) {
foreach ($arrayOfObjects as $object) {
$data[$object->{$key}] = $object->{$value};
}
} else {