Advertisement
Guest User

Deviad

a guest
Feb 16th, 2016
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.32 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 = 'joomla_dev_10';
  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. <?php
  51.  
  52. /**
  53.  * @author Davide Pugliese
  54.  * @uses   PDO
  55.  *
  56.  * This is a little engine to run queries the smart way.
  57.  *
  58.  */
  59.  
  60.  
  61. class DbObj
  62. {
  63.     protected static $the_table = Null;
  64.     protected static $the_attrs = NUll;
  65.     protected static $projection = Null;
  66.     public static $the_values;
  67.    
  68.    
  69.    
  70.     public static function addObj($the_table, $the_attrs, $the_values)
  71.     {
  72.         echo "INTO THE GENERIC FUNCTION";
  73.         var_dump($the_table);
  74.         var_dump($the_attrs);
  75.         var_dump($the_values);
  76.         $sql     = "INSERT INTO $the_table ( $the_attrs )  VALUES ( $the_values );";
  77.         $pdo_obj = DbMgmt::newConn(); //get the pdo object
  78.         $stmt    = $pdo_obj->prepare($sql);
  79.         $stmt->execute();
  80.         echo "Data inserted!";
  81.        
  82.     }
  83.    
  84.     public static function rmObj($the_table, $the_attrs, $the_values)
  85.     {
  86.         $sql     = "DELETE FROM $the_table WHERE ($the_attrs) =  ( $the_values );";
  87.         $pdo_obj = DbMgmt::newConn(); //get the pdo object
  88.         $stmt    = $pdo_obj->prepare($sql);
  89.         $stmt->execute();
  90.         echo "Data deleted!";
  91.     }
  92.    
  93.     public static function getObj($projection, $the_table, $the_attrs, $the_values)
  94.     {
  95.         $sql = "SELECT $projection FROM $the_table WHERE $the_attrs LIKE $the_values;";
  96.         $pdo_obj = DbMgmt::newConn(); //get the pdo object
  97.         $stmt    = $pdo_obj->prepare($sql);
  98.         $stmt->execute();
  99.         $result = $stmt->fetchAll(PDO::FETCH_ASSOC);
  100.         return $result;
  101.         //echo "Data fetched!";
  102.  
  103.     }
  104. }
  105.  
  106.  
  107. //VALUE MUST BE INSIDE QUOTES LIKE THIS: 'VALUE1', 'VALUE2'
  108.  
  109. //$task1 = Task::addTask("'Pippo'");
  110.  
  111. class MenuItems extends DbObj {
  112.     protected static $projection = "title,path";
  113.     protected static $the_table = "tance_menu";
  114.     protected static $the_attrs = "menutype";
  115.  
  116.     public static function getArticle($the_values) {
  117.        
  118.         $fetched_array = parent::getObj(self::$projection, self::$the_table, self::$the_attrs, $the_values);
  119.         return $fetched_array;
  120.  
  121.     }
  122. }
  123.  
  124.   MenuItems::getArticle("'mainmenu'");
  125.  
  126.  
  127. ?>
  128.  
  129.     <ul>
  130.         <?php
  131.           $menu_item_list = MenuItems::getArticle("'mainmenu'");
  132.           var_dump($menu_item_list);
  133.           foreach ($menu_item_list as &$an_item) {
  134.             foreach ($an_item as $key  => &$value) {
  135.               $the_title=$an_item["title"];
  136.               $the_path=$an_item["path"];
  137.               echo $the_title . "=>" . $the_path . "<br>";
  138.             }  
  139.           }
  140.         ?>
  141.         </ul>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement