Advertisement
Guest User

Frax

a guest
Jan 15th, 2011
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.82 KB | None | 0 0
  1. <?php
  2.  
  3. /********************************/
  4.  
  5. define('DB_DSN', 'mysql:dbname=testdb;host=127.0.0.1');
  6. define('DB_USER', '');
  7. define('DB_PASS', '');
  8.  
  9. define('BASE_URL', '/index.php');
  10.  
  11. define('DB_TBL_CATEGORIES', 'tbl_categories');
  12. define('DB_TBL_PRODUCTS', 'tbl_products');
  13.  
  14. error_reporting(E_ALL);
  15.  
  16. /********************************/
  17.  
  18.  
  19. class Actions
  20. {
  21.     private $_db;
  22.  
  23.     public function __construct(PDO $db){
  24.  
  25.         $this->_db = $db;
  26.     }
  27.  
  28.  
  29.  
  30.     public function index(){
  31.  
  32.         $sth = $this->_db->prepare(
  33.             'SELECT id, name FROM '.DB_TBL_CATEGORIES.
  34.             ' ORDER BY name ASC');
  35.         $sth->execute();
  36.  
  37.         $categories = $sth->fetchAll(PDO::FETCH_ASSOC);
  38.  
  39.         $products = array();
  40.  
  41.         if($categories){
  42.             $sth = $this->_db->prepare(
  43.                 'SELECT id, name FROM '.DB_TBL_PRODUCTS.
  44.                 ' WHERE category_id=? ORDER BY name ASC');
  45.             $sth->execute(array($categories[0]['id']));
  46.  
  47.             $products = $sth->fetchAll(PDO::FETCH_ASSOC);
  48.         }
  49.  
  50.  
  51.         return template_index($categories, $products);
  52.     }
  53.  
  54.  
  55.     public function ajax_category($id){
  56.  
  57.         $sth = $this->_db->prepare(
  58.             'SELECT id, name FROM '.DB_TBL_PRODUCTS.
  59.             ' WHERE category_id=?');
  60.         $sth->execute(array($id));
  61.  
  62.         $data = $sth->fetchAll(PDO::FETCH_ASSOC);
  63.  
  64.         header('Content-Type: application/json');
  65.  
  66.         return json_encode(array('products' => $data));
  67.     }
  68.  
  69.     public function ajax_product($id){
  70.  
  71.         $sth = $this->_db->prepare(
  72.             'SELECT col5, col6, col7 FROM '.DB_TBL_PRODUCTS.
  73.             ' WHERE id=?');
  74.         $sth->execute(array($id));
  75.  
  76.         $data = $sth->fetch(PDO::FETCH_ASSOC);
  77.  
  78.  
  79.         if(!$data){
  80.             header("HTTP/1.1 404 Not Found");
  81.             return '';
  82.         }
  83.  
  84.         header('Content-Type: application/json');
  85.  
  86.         return json_encode($data);
  87.     }
  88.  
  89. }
  90.  
  91.  
  92. function main(){
  93.  
  94.     $db = get_db();
  95.     $actions = new Actions($db);
  96.  
  97.     switch(array_get($_GET, 'act')){
  98.  
  99.         case 'ajax_product':
  100.             echo $actions->ajax_product(array_get($_GET, 'id'));
  101.             break;
  102.         case 'ajax_category':
  103.             echo $actions->ajax_category(array_get($_GET, 'id'));
  104.             break;
  105.         default:
  106.             echo $actions->index();
  107.             break;
  108.     }
  109.  
  110.     $db = null;
  111. }
  112.  
  113.  
  114. function get_db(){
  115.     $db = new PDO(DB_DSN, DB_USER, DB_PASS);
  116.     $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  117.     return $db;
  118. }
  119.  
  120. function url($action){
  121.     return BASE_URL . '?act='.$action;
  122. }
  123.  
  124. function html_escape($str){
  125.     return htmlspecialchars($str, ENT_QUOTES, 'UTF-8');
  126. }
  127.  
  128. function array_get(array $a, $key, $default=null){
  129.     return isset($a[$key]) ? $a[$key] : $default;
  130. }
  131.  
  132. function template_index($categories, $products){
  133. header('Content-type: text/html; charset=utf-8');
  134. ?>
  135. <!DOCTYPE html>
  136. <html>
  137. <head>
  138.     <title>Kalkulator</title>
  139.     <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
  140.     <script>
  141.     $(document).ready(function(){
  142.  
  143.         var _cache = {};
  144.  
  145.         function update_results(id, amount){
  146.             if (id in _cache){
  147.                 _update_results(_cache[id], amount);
  148.  
  149.             }else{
  150.                 $.get(
  151.                     "<?php echo url('ajax_product'); ?>&id="+id,
  152.  
  153.                     function(data){
  154.  
  155.                         _cache[id] = data;
  156.                         _update_results(data, amount);
  157.                     },
  158.                     "json"
  159.                 );
  160.             }
  161.         }
  162.  
  163.         function _update_results(prod, amount){
  164.  
  165.             $("#results").empty().append(
  166.             $("<li></li>").text("Verdi 1: "+(amount * prod.col5))).append(
  167.             $("<li></li>").text("Verdi 2: "+(amount * prod.col6))).append(
  168.             $("<li></li>").text("Verdi 3: "+(amount * prod.col7)));
  169.         }
  170.  
  171.         function _update_category(id){
  172.             $.get(
  173.                 "<?php echo url('ajax_category'); ?>&id="+id,
  174.  
  175.                 function(data){
  176.  
  177.                     $("#products").empty();
  178.                     jQuery.each(data.products, function(i,v){
  179.  
  180.                         $("#products").append($("<option></option>").val(v.id).text(v.name));
  181.  
  182.                     });
  183.  
  184.                     $("#products").change();
  185.                 },
  186.                 "json"
  187.             );
  188.         }
  189.  
  190.         $("#categories").change(function(){
  191.             var value = $(this).val();
  192.             _update_category(value);
  193.  
  194.         });
  195.  
  196.  
  197.         $("#products").change(function(){
  198.             var amount = parseInt($("#amount").val(), 10);
  199.  
  200.             update_results($(this).val(), amount);
  201.         });
  202.  
  203.         $("#amount").bind('keyup change paste', function(){
  204.  
  205.             var value = parseInt($(this).val(), 10);
  206.  
  207.             var prod_id = $("#products").val();
  208.  
  209.             update_results(prod_id, value);
  210.  
  211.         });
  212.     });
  213.     </script>
  214. </head>
  215. <body>
  216.  
  217.     <p>
  218.         <select id="categories">
  219.             <?php foreach($categories as $cat): ?>
  220.                 <option value="<?php echo $cat['id']; ?>"><?php echo html_escape($cat['name']); ?></option>
  221.             <?php endforeach; ?>
  222.         </select>
  223.     </p>
  224.  
  225.     <p>
  226.         <select id="products">
  227.             <?php foreach($products as $prod): ?>
  228.                 <option value="<?php echo $prod['id']; ?>"><?php echo html_escape($prod['name']); ?></option>
  229.             <?php endforeach; ?>
  230.         </select>
  231.     </p>
  232.  
  233.     <p><input type="text" id="amount"/></p>
  234.  
  235.     <ul id="results"></ul>
  236.  
  237. </body>
  238. </html>
  239. <?php
  240.  
  241. }
  242.  
  243.  
  244.  
  245. function create_test_tables(){
  246.  
  247.     $db = get_db();
  248.  
  249.     $db->exec("CREATE TABLE ".DB_TBL_CATEGORIES."(
  250.     id      INTEGER PRIMARY KEY NOT NULL AUTO_INCREMENT,
  251.     name    VARCHAR(30) DEFAULT '' NOT NULL,
  252.  
  253.     INDEX(name)
  254.  
  255. ) DEFAULT CHARSET=utf8 COLLATE=utf8_danish_ci;");
  256.  
  257.     $db->exec("CREATE TABLE ".DB_TBL_PRODUCTS."(
  258.     id              INTEGER PRIMARY KEY NOT NULL AUTO_INCREMENT,
  259.     category_id     INTEGER NOT NULL,
  260.     name            VARCHAR(30) DEFAULT '' NOT NULL,
  261.  
  262.     col5            INTEGER DEFAULT 0 NOT NULL,
  263.     col6            INTEGER DEFAULT 0 NOT NULL,
  264.     col7            INTEGER DEFAULT 0 NOT NULL,
  265.  
  266.  
  267.     INDEX(name),
  268.     INDEX(category_id)
  269.  
  270. ) DEFAULT CHARSET=utf8 COLLATE=utf8_danish_ci;");
  271.  
  272.  
  273.     $db=null;
  274. }
  275.  
  276. function create_test_data(){
  277.  
  278.     $db = get_db();
  279.  
  280.     for($i=1; $i<10; ++$i){
  281.  
  282.         $db->exec('INSERT INTO '.DB_TBL_CATEGORIES." (name) VALUES('Test $i')");
  283.  
  284.         $cat_id = $db->lastInsertId();
  285.  
  286.         for($j=1; $j<10; ++$j){
  287.             $db->exec('INSERT INTO '.DB_TBL_PRODUCTS.' (category_id, name, col5, col6, col7) '.
  288.                         "VALUES($cat_id, 'Test $j', ".rand(1, 50).', '.rand(1, 50).', '.rand(1, 50).')');
  289.         }
  290.  
  291.     }
  292.     $db=null;
  293.  
  294. }
  295.  
  296. // create_test_tables();
  297. // create_test_data();
  298. main();
  299. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement