Advertisement
Guest User

Deviad

a guest
Feb 16th, 2016
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.05 KB | None | 0 0
  1. <?php
  2.  
  3. class DbMgmt
  4. {
  5.     const SERVERNAME = 'localhost';
  6.     const USERNAME = 'root';
  7.     const PASSWORD = '';
  8.     const DBNAME = 'my_database';
  9.     public static $conn;
  10.  
  11.     public static function newConn()
  12.     {
  13.         try {
  14.             self::$conn = new PDO('mysql:host='.self::SERVERNAME, self::USERNAME, self::PASSWORD);
  15.             self::$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  16.             $sql = 'USE ' . self::DBNAME;
  17.             self::$conn->exec($sql);
  18.             echo "Connection established <br>";
  19.  
  20.             return self::$conn;
  21.         } catch (PDOException $e) {
  22.             echo $sql.'<br>'.$e->getMessage();
  23.         }
  24.     }
  25.  
  26.     /*public static function createDB()
  27.     {
  28.         try {
  29.             // set the PDO error mode to exception
  30.  
  31.         self::$conn = new PDO('mysql:host='.self::SERVERNAME, self::USERNAME, self::PASSWORD);
  32.             self::$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  33.             $sql = 'CREATE DATABASE '.self::DBNAME.';';
  34.             $sql .= 'USE '.self::DBNAME;
  35.             // use exec() because no results are returned
  36.             self::$conn->exec($sql);
  37.             echo 'Database '."''".self::DBNAME."''".' created successfully <br>';
  38.             $sql = 'CREATE TABLE tasks ( task_id INT UNSIGNED NOT NULL AUTO_INCREMENT, parent_id INT UNSIGNED NOT NULL DEFAULT 0, task VARCHAR(100) NOT NULL, date_added TIMESTAMP NOT NULL, date_completed TIMESTAMP, PRIMARY KEY (task_id), INDEX parent (parent_id), INDEX added (date_added), INDEX completed (date_completed))';
  39.             self::$conn->exec($sql);
  40.             echo 'Table(s) added correctly<br>';
  41.         } catch (PDOException $e) {
  42.             echo $sql.'<br>'.$e->getMessage();
  43.         }
  44.         self::$conn = null;
  45.     }*/
  46. }
  47.  
  48.  
  49.  
  50.  
  51.  
  52. class DbObj
  53. {
  54.     protected static $the_table = Null;
  55.     protected static $the_attrs = NUll;
  56.     protected static $projection = Null;
  57.     public static $the_values;
  58.    
  59.    
  60.    
  61.     public static function addObj($the_table, $the_attrs, $the_values)
  62.     {
  63.         echo "INTO THE GENERIC FUNCTION";
  64.         var_dump($the_table);
  65.         var_dump($the_attrs);
  66.         var_dump($the_values);
  67.         $sql     = "INSERT INTO $the_table ( $the_attrs )  VALUES ( $the_values );";
  68.         $pdo_obj = DbMgmt::newConn(); //get the pdo object
  69.         $stmt    = $pdo_obj->prepare($sql);
  70.         $stmt->execute();
  71.         echo "Data inserted!";
  72.        
  73.     }
  74.    
  75.     public static function rmObj($the_table, $the_attrs, $the_values)
  76.     {
  77.         $sql     = "DELETE FROM $the_table WHERE ($the_attrs) =  ( $the_values );";
  78.         $pdo_obj = DbMgmt::newConn(); //get the pdo object
  79.         $stmt    = $pdo_obj->prepare($sql);
  80.         $stmt->execute();
  81.         echo "Data deleted!";
  82.     }
  83.    
  84.     public static function getObj($projection, $the_table, $the_attrs, $the_values)
  85.     {
  86.         $sql = "SELECT $projection FROM $the_table WHERE $the_attrs LIKE $the_values;";
  87.         $pdo_obj = DbMgmt::newConn(); //get the pdo object
  88.         $stmt    = $pdo_obj->prepare($sql);
  89.         $stmt->execute();
  90.         $result = $stmt->fetchAll(PDO::FETCH_ASSOC);
  91.         return $result;
  92.         //echo "Data fetched!";
  93.  
  94.     }
  95. }
  96.  
  97.  
  98. //VALUE MUST BE INSIDE QUOTES LIKE THIS: 'VALUE1', 'VALUE2'
  99.  
  100. //$task1 = Task::addTask("'Pippo'");
  101.  
  102. class MenuItems extends DbObj {
  103.     protected static $projection = "title,path";
  104.     protected static $the_table = "tance_menu";
  105.     protected static $the_attrs = "menutype";
  106.  
  107.     public static function getArticle($the_values) {
  108.        
  109.         parent::getObj(self::$projection, self::$the_table, self::$the_attrs, $the_values);
  110.  
  111.     }
  112. }
  113.  
  114.   MenuItems::getArticle("'mainmenu'");
  115.  
  116.  
  117. ?>
  118.  
  119.     <ul>
  120.         <?php
  121.           $menu_item_list = MenuItems::getArticle("'mainmenu'");
  122.           var_dump($menu_item_list);
  123.           foreach ($menu_item_list as &$an_item) {
  124.             foreach ($an_item as $key => &$value) {
  125.               echo "<li><a href='$value'>$key</a></li>";    
  126.             }  
  127.           }
  128.         ?>
  129.    </ul>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement