Skip to content

Instantly share code, notes, and snippets.

@ricardosiri68
Last active August 29, 2015 14:23
Show Gist options
  • Select an option

  • Save ricardosiri68/7f99b299dd4dcbb9870a to your computer and use it in GitHub Desktop.

Select an option

Save ricardosiri68/7f99b299dd4dcbb9870a to your computer and use it in GitHub Desktop.
/*jslint nomen: true, debug: true, evil: true, vars: true, browser: true,
devel: true */
/*global */
var CountDown = function(options){
this.button = document.getElementById(options.button);
this.counter = document.getElementById(options.counter);
this.button.addEventListener('click', this.count_down.bind(this), false);
this.xhr = new XMLHttpRequest();
this.xhr.addEventListener(
'readystatechange',
this.load_count.bind(this),
false
);
this.get_count();
};
CountDown.prototype = {
get_url: '/balance.php?action=1',
update_url: '/balance.php?action=2',
get_count: function(){
this.xhr.open('GET', this.get_url);
this.xhr.send();
},
load_count: function(e){
var xhr = e.target;
if (xhr.status === 200 && xhr.readyState === 4) {
this.counter.innerText = xhr.responseText;
}
},
count_down: function(){
this.xhr.open('GET', this.update_url);
this.xhr.send();
}
};
(function(){
var main = function(){
var count_down = new CountDown({
button: 'count',
counter: 'count'
});
};
window.addEventListener('load', main, false);
}());
<?php
require_once 'connect_db.php';
function get_balance() {
$conn = connect_db();
$query = 'SELECT cantidad FROM balance LIMIT 1';
$result = mysql_query($query, $conn);
if (! mysql_num_rows($result)) {
header("HTTP/1.0 500 Internal Server Error");
throw new Exception(
'No se encuentra ningun balance en la base de datos'
);
}
$row = mysql_fetch_assoc($result);
return $row['cantidad'];
}
function update_balance() {
$conn = connect_db();
$query = 'UPDATE balance SET cantidad = cantidad - 200';
$result = mysql_query($query, $conn);
if (! $result) {
header("HTTP/1.0 500 Internal Server Error");
throw new Exception('No fue posible actualizar el balance');
}
return get_balance();
}
function reset_balance() {
$conn = connect_db();
$query = 'UPDATE balance SET cantidad = 100000';
$result = mysql_query($query, $conn);
if (! $result) {
header("HTTP/1.0 500 Internal Server Error");
throw new Exception('No se encontro ningun balance para resetear');
}
return get_balance();
}
$action = $_GET['action'];
switch ($action) {
case 1:
echo get_balance();
break;
case 2:
echo update_balance();
break;
case 3:
echo reset_balance();
break;
default:
header("HTTP/1.0 500 Internal Server Error");
throw new Exception('accion invalida !!!');
}
?>
[db]
; configuracion de la conexion a la base de datos
; user: usuario, pass: contraseña, host: servidor (localhost), name: nombre de
; la base de datos
user =
pass =
host =
name =
<?php
function connect_db() {
$config = parse_ini_file('config.ini', true);
$db_config = $config['db'];
$conn = mysql_connect(
$server = $db_config['host'],
$username = $db_config['user'],
$password = $db_config['pass']
);
if (! $conn) {
die('No se pudo conectar a la base de datos: ' . mysql_error());
} else {
if (! mysql_select_db($db_config['name'], $conn)) {
header("HTTP/1.0 500 Internal Server Error");
throw new Exception('La base de datos ' . $db_config['name'] . ' no existe');
}
}
return $conn;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>La campora sisters</title>
<link rel="stylesheet" type="text/css" href="style.css" />
<script language="javascript" type="text/javascript" src="app.js"></script>
</head>
<body>
<button id="count" data="100000"></button>
</body>
</html>
CREATE TABLE balance (cantidad int);
INSERT INTO balance (cantidad) VALUES (100000);
#count{
padding: 10px;
font-size: 50px;
font-family: monospace;
border: 1px #444 solid;
border-radius: 10px;
background-color: #ccc;
color: #444;
cursor: pointer;
outline: 0;
}
#count:hover{
background-color: #888;
color: #555;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment