Skip to content

Instantly share code, notes, and snippets.

@Nodws
Last active Feb 11, 2022
Embed
What would you like to do?
<?php
$cache = 'data.txt';
if(date("Ymd", filemtime($cache)) < date("Ymd") || filesize($cache) < 1):
$data = get_data(); // do your thing
$file = fopen($cache,'w+');
$text = is_array($data) ? json_encode($data) : $data;
fwrite($file, $text);
fclose($file);
else:
$file = fopen($cache,'w+');
$data = fread($file, filesize($cache));
if($data[0] == '{');
$data = json_decode($data, true);
endif;
<?php
function cacher($file,$contents){
$cachefile = $file.'.txt';
$cachetime = 60 * 60 * 24; // 24 hours
if ((time() - $cachetime < filemtime($cachefile)) && !isset($_GET['nocache'])) {
include($cachefile);
echo "<!-- ".date('jS F Y H:i', filemtime($cachefile))." -->";
echo "<!-- next ".date('jS F Y H:i', filemtime($cachefile) + $cachetime)." -->";
}
else{
$fp = fopen($cachefile, 'w');
fwrite($fp, $contents);
fclose($fp);
echo $contents;
echo "<!-- ".date('jS F Y H:i', filemtime($cachefile))." -->";
}
}
<?php
$secciones = ['mapa' ,'compara' ,'grafica' ,'tabla' ,'estado' ,];
$timeline = [2019,2020,2021,2022];
$u = explode('/', trim($_SERVER['QUERY_STRING'],'/'));
/*
/mapa/f1/2018
/mapa/f1-2/2018
/estado/MX33/f1/2018
/estado/MX33/f1-2/2018
/compara/00-33-22/2018
/grafica/asc/2018
/tabla/desc/2019
/tabla
*/
$p[ver] = isset($secciones[$u[0]]) ? $u[0] : $secciones[0];
$p[embed] = end($u)=='embed' ?? true;
$year = in_array(end($u), $timeline) ?? end($u);
$p[year] = $year ?? end($timeline);
$ord = ['asc', 'des', 'az', 'za'];
$p[gorder] = $ord[0];
$p[order] = $ord[2];
$p[estados] = ['01','32'];
$p[efact] = $p[estado] = 'MX00';
switch($p[ver]):
case "mapa":
$p[efact] = (strstr($u[1],'MX')) ? $u[1] : $p[estado];
$p[fact] = (strstr($u[1],'-')) ? explode('-',$u[1])[0] : $u[1];
$p[sub] = (strstr($u[1],'-')) ? explode('-',$u[1])[1] : '';
break;
case "estado":
$p[estado] = substr(strtolower($u[1]), 0, 2) == 'mx' ? $u[1] : '';
$p[fact] = (strstr($u[2],'-')) ? explode('-',$u[2])[0] : $u[2];
$p[sub] = (strstr($u[2],'-')) ? explode('-',$u[2])[1] : '';
break;
case "compara":
$p[estados] = (strstr($u[1],'-')) ? explode('-',$u[1]) : $p[estados];
break;
case "grafica":
$p[gorder] = in_array($u[1], $ord) ? $u[1] : $p[gorder]; //asc des az za
break;
case "tabla":
$p[order] = in_array($u[1], $ord) ? $u[1] : $p[order];
break;
endswitch;
print_r($p);
<?php
$args = explode('/', trim($_SERVER['QUERY_STRING'],'/'));
$i = 0;
foreach ($args as $k => $v) {
$i++;
if($i%2 == 0)
$p[$args[$i - 2]] = $v;
if( $v == 'embed')
$p[$v] = $v;
}
print_r($p);
<?php
function routes($url){
$u = explode('/', trim($_SERVER['QUERY_STRING'],'/'));
$sep = '.'; //sub parameter separator
switch($u[0]){
case 'special': $u[1] = isset($u[1]) ? explode($sep, $u[1])) : null; break;
}
$_SERVER['U'] = $u;
return $u;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment