Skip to content

Instantly share code, notes, and snippets.

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

Gabriel Guerra ogabrielguerra

🏠
Working from home
View GitHub Profile
@ogabrielguerra
ogabrielguerra / jsonAttributesToObj.php
Last active September 29, 2018 12:02
A PHP method to convert JSON attributes into object attributes.
function convertJsonToObjAttributes($json){
$keys = array_keys((array)$json);
$jsonSize = count($json);
for( $i=0; $i<$jsonSize; $i++ )
$this->{$keys[$i]} = $json[$keys[$i]];
}
@ogabrielguerra
ogabrielguerra / onScrollStop.js
Last active May 14, 2019 12:38
Execute an action when user stops scrolling. | JS
let scroller=(callback, interval)=>{
//Inits the var
let scrollStop;
//Adds the event listener
window.addEventListener('scroll', function () {
clearTimeout(scrollStop);
scrollStop = setTimeout(function () {
//Execute the callback passed as reference
@ogabrielguerra
ogabrielguerra / newObjFromState.js
Created May 25, 2019 00:07
REACT | Compose a new object from Component State
const states = this.state.proposalOptions;
let gathered = {};
Object.keys(states).map((key)=>{
gathered[key] = this.state.proposalOptions[key];
}
);
@ogabrielguerra
ogabrielguerra / ImageToBase64.js
Created June 23, 2019 23:06
Javascript function to convert images to base64
function getBase64Image(img) {
var canvas = document.createElement("canvas");
canvas.width = img.width;
canvas.height = img.height;
var ctx = canvas.getContext("2d");
ctx.drawImage(img, 0, 0);
var dataURL = canvas.toDataURL("image/png");
return dataURL.replace(/^data:image\/(png|jpg);base64,/, "");
}
@ogabrielguerra
ogabrielguerra / RadioGroup.js
Created July 21, 2019 17:06
React JS Component for handling radio button groups.
import React from "react";
class RadioGroup extends React.Component{
constructor(props){
super(props);
this.handleCheckboxChange = this.handleCheckboxChange.bind(this);
this.state = { selectedOption : this.props.checked }
}
@ogabrielguerra
ogabrielguerra / SiteImages.Class.php
Last active May 1, 2020 21:04
Get images from a dir and return HTML accordingly with passed template.
<?php
class SiteImages{
public string $path = '';
public string $template = '';
public function __construct(string $path, string $template){
$this->setPath($path);
$this->setTemplate($template);
@ogabrielguerra
ogabrielguerra / sortWords.php
Last active September 25, 2020 01:07
Sort Special Words
<?php
function sortWords(String $string){
$words = explode(' ', $string);
$numWords = count($words);
$englishWords = [];
$chineseWords = [];
for($i=0; $i<$numWords; $i++){
if(ctype_alnum($words[$i])){
@ogabrielguerra
ogabrielguerra / courses_manager_structure.sql
Created September 25, 2020 01:10
SQL Script - Courses Manager Structure
-- CREATE STRUCTURE
DROP TABLE IF EXISTS `course`;
CREATE TABLE `course` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`title` varchar(255) NOT NULL DEFAULT '',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
@ogabrielguerra
ogabrielguerra / course_manager_data.sql
Created September 25, 2020 01:12
SQL Script - Course Manager Data
-- INSERT DATA
INSERT INTO `course` (`id`, `title`)
VALUES
(1,'Cooking for Fun'),
(2,'Arduin and Automation'),
(3,'Education for Life'),
(4,'Modern PHP Definitive Guide');
INSERT INTO `course_application`
@ogabrielguerra
ogabrielguerra / course_manager_queries.sql
Created September 25, 2020 01:15
SQL Script - Course Manager Queries
-- COUNT APPLICATIONS BY USER
SELECT ca.id_user, u.name,
COUNT(ca.id_user) as num_applications
FROM course_application ca, user u
WHERE ca.id_user = u.id
GROUP BY ca.id_user, u.name;
-- COUNT COURSES BY USER OF ID 1