Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /********************************/
- define('DB_DSN', 'mysql:dbname=testdb;host=127.0.0.1');
- define('DB_USER', '');
- define('DB_PASS', '');
- define('BASE_URL', '/index.php');
- define('DB_TBL_CATEGORIES', 'tbl_categories');
- define('DB_TBL_PRODUCTS', 'tbl_products');
- error_reporting(E_ALL);
- /********************************/
- class Actions
- {
- private $_db;
- public function __construct(PDO $db){
- $this->_db = $db;
- }
- public function index(){
- $sth = $this->_db->prepare(
- 'SELECT id, name FROM '.DB_TBL_CATEGORIES.
- ' ORDER BY name ASC');
- $sth->execute();
- $categories = $sth->fetchAll(PDO::FETCH_ASSOC);
- $products = array();
- if($categories){
- $sth = $this->_db->prepare(
- 'SELECT id, name FROM '.DB_TBL_PRODUCTS.
- ' WHERE category_id=? ORDER BY name ASC');
- $sth->execute(array($categories[0]['id']));
- $products = $sth->fetchAll(PDO::FETCH_ASSOC);
- }
- return template_index($categories, $products);
- }
- public function ajax_category($id){
- $sth = $this->_db->prepare(
- 'SELECT id, name FROM '.DB_TBL_PRODUCTS.
- ' WHERE category_id=?');
- $sth->execute(array($id));
- $data = $sth->fetchAll(PDO::FETCH_ASSOC);
- header('Content-Type: application/json');
- return json_encode(array('products' => $data));
- }
- public function ajax_product($id){
- $sth = $this->_db->prepare(
- 'SELECT col5, col6, col7 FROM '.DB_TBL_PRODUCTS.
- ' WHERE id=?');
- $sth->execute(array($id));
- $data = $sth->fetch(PDO::FETCH_ASSOC);
- if(!$data){
- header("HTTP/1.1 404 Not Found");
- return '';
- }
- header('Content-Type: application/json');
- return json_encode($data);
- }
- }
- function main(){
- $db = get_db();
- $actions = new Actions($db);
- switch(array_get($_GET, 'act')){
- case 'ajax_product':
- echo $actions->ajax_product(array_get($_GET, 'id'));
- break;
- case 'ajax_category':
- echo $actions->ajax_category(array_get($_GET, 'id'));
- break;
- default:
- echo $actions->index();
- break;
- }
- $db = null;
- }
- function get_db(){
- $db = new PDO(DB_DSN, DB_USER, DB_PASS);
- $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
- return $db;
- }
- function url($action){
- return BASE_URL . '?act='.$action;
- }
- function html_escape($str){
- return htmlspecialchars($str, ENT_QUOTES, 'UTF-8');
- }
- function array_get(array $a, $key, $default=null){
- return isset($a[$key]) ? $a[$key] : $default;
- }
- function template_index($categories, $products){
- header('Content-type: text/html; charset=utf-8');
- ?>
- <!DOCTYPE html>
- <html>
- <head>
- <title>Kalkulator</title>
- <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
- <script>
- $(document).ready(function(){
- var _cache = {};
- function update_results(id, amount){
- if (id in _cache){
- _update_results(_cache[id], amount);
- }else{
- $.get(
- "<?php echo url('ajax_product'); ?>&id="+id,
- function(data){
- _cache[id] = data;
- _update_results(data, amount);
- },
- "json"
- );
- }
- }
- function _update_results(prod, amount){
- $("#results").empty().append(
- $("<li></li>").text("Verdi 1: "+(amount * prod.col5))).append(
- $("<li></li>").text("Verdi 2: "+(amount * prod.col6))).append(
- $("<li></li>").text("Verdi 3: "+(amount * prod.col7)));
- }
- function _update_category(id){
- $.get(
- "<?php echo url('ajax_category'); ?>&id="+id,
- function(data){
- $("#products").empty();
- jQuery.each(data.products, function(i,v){
- $("#products").append($("<option></option>").val(v.id).text(v.name));
- });
- $("#products").change();
- },
- "json"
- );
- }
- $("#categories").change(function(){
- var value = $(this).val();
- _update_category(value);
- });
- $("#products").change(function(){
- var amount = parseInt($("#amount").val(), 10);
- update_results($(this).val(), amount);
- });
- $("#amount").bind('keyup change paste', function(){
- var value = parseInt($(this).val(), 10);
- var prod_id = $("#products").val();
- update_results(prod_id, value);
- });
- });
- </script>
- </head>
- <body>
- <p>
- <select id="categories">
- <?php foreach($categories as $cat): ?>
- <option value="<?php echo $cat['id']; ?>"><?php echo html_escape($cat['name']); ?></option>
- <?php endforeach; ?>
- </select>
- </p>
- <p>
- <select id="products">
- <?php foreach($products as $prod): ?>
- <option value="<?php echo $prod['id']; ?>"><?php echo html_escape($prod['name']); ?></option>
- <?php endforeach; ?>
- </select>
- </p>
- <p><input type="text" id="amount"/></p>
- <ul id="results"></ul>
- </body>
- </html>
- <?php
- }
- function create_test_tables(){
- $db = get_db();
- $db->exec("CREATE TABLE ".DB_TBL_CATEGORIES."(
- id INTEGER PRIMARY KEY NOT NULL AUTO_INCREMENT,
- name VARCHAR(30) DEFAULT '' NOT NULL,
- INDEX(name)
- ) DEFAULT CHARSET=utf8 COLLATE=utf8_danish_ci;");
- $db->exec("CREATE TABLE ".DB_TBL_PRODUCTS."(
- id INTEGER PRIMARY KEY NOT NULL AUTO_INCREMENT,
- category_id INTEGER NOT NULL,
- name VARCHAR(30) DEFAULT '' NOT NULL,
- col5 INTEGER DEFAULT 0 NOT NULL,
- col6 INTEGER DEFAULT 0 NOT NULL,
- col7 INTEGER DEFAULT 0 NOT NULL,
- INDEX(name),
- INDEX(category_id)
- ) DEFAULT CHARSET=utf8 COLLATE=utf8_danish_ci;");
- $db=null;
- }
- function create_test_data(){
- $db = get_db();
- for($i=1; $i<10; ++$i){
- $db->exec('INSERT INTO '.DB_TBL_CATEGORIES." (name) VALUES('Test $i')");
- $cat_id = $db->lastInsertId();
- for($j=1; $j<10; ++$j){
- $db->exec('INSERT INTO '.DB_TBL_PRODUCTS.' (category_id, name, col5, col6, col7) '.
- "VALUES($cat_id, 'Test $j', ".rand(1, 50).', '.rand(1, 50).', '.rand(1, 50).')');
- }
- }
- $db=null;
- }
- // create_test_tables();
- // create_test_data();
- main();
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement