Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class DbMgmt
- {
- const SERVERNAME = 'localhost';
- const USERNAME = 'root';
- const PASSWORD = '';
- const DBNAME = 'joomla_dev_10';
- public static $conn;
- public static function newConn()
- {
- try {
- self::$conn = new PDO('mysql:host='.self::SERVERNAME, self::USERNAME, self::PASSWORD);
- self::$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
- $sql = 'USE ' . self::DBNAME;
- self::$conn->exec($sql);
- echo "Connection established <br>";
- return self::$conn;
- } catch (PDOException $e) {
- echo $sql.'<br>'.$e->getMessage();
- }
- }
- /*public static function createDB()
- {
- try {
- // set the PDO error mode to exception
- self::$conn = new PDO('mysql:host='.self::SERVERNAME, self::USERNAME, self::PASSWORD);
- self::$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
- $sql = 'CREATE DATABASE '.self::DBNAME.';';
- $sql .= 'USE '.self::DBNAME;
- // use exec() because no results are returned
- self::$conn->exec($sql);
- echo 'Database '."''".self::DBNAME."''".' created successfully <br>';
- $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))';
- self::$conn->exec($sql);
- echo 'Table(s) added correctly<br>';
- } catch (PDOException $e) {
- echo $sql.'<br>'.$e->getMessage();
- }
- self::$conn = null;
- }*/
- }
- ?>
- <?php
- /**
- * @author Davide Pugliese
- * @uses PDO
- *
- * This is a little engine to run queries the smart way.
- *
- */
- class DbObj
- {
- protected static $the_table = Null;
- protected static $the_attrs = NUll;
- protected static $projection = Null;
- public static $the_values;
- public static function addObj($the_table, $the_attrs, $the_values)
- {
- echo "INTO THE GENERIC FUNCTION";
- var_dump($the_table);
- var_dump($the_attrs);
- var_dump($the_values);
- $sql = "INSERT INTO $the_table ( $the_attrs ) VALUES ( $the_values );";
- $pdo_obj = DbMgmt::newConn(); //get the pdo object
- $stmt = $pdo_obj->prepare($sql);
- $stmt->execute();
- echo "Data inserted!";
- }
- public static function rmObj($the_table, $the_attrs, $the_values)
- {
- $sql = "DELETE FROM $the_table WHERE ($the_attrs) = ( $the_values );";
- $pdo_obj = DbMgmt::newConn(); //get the pdo object
- $stmt = $pdo_obj->prepare($sql);
- $stmt->execute();
- echo "Data deleted!";
- }
- public static function getObj($projection, $the_table, $the_attrs, $the_values)
- {
- $sql = "SELECT $projection FROM $the_table WHERE $the_attrs LIKE $the_values;";
- $pdo_obj = DbMgmt::newConn(); //get the pdo object
- $stmt = $pdo_obj->prepare($sql);
- $stmt->execute();
- $result = $stmt->fetchAll(PDO::FETCH_ASSOC);
- return $result;
- //echo "Data fetched!";
- }
- }
- //VALUE MUST BE INSIDE QUOTES LIKE THIS: 'VALUE1', 'VALUE2'
- //$task1 = Task::addTask("'Pippo'");
- class MenuItems extends DbObj {
- protected static $projection = "title,path";
- protected static $the_table = "tance_menu";
- protected static $the_attrs = "menutype";
- public static function getArticle($the_values) {
- $fetched_array = parent::getObj(self::$projection, self::$the_table, self::$the_attrs, $the_values);
- return $fetched_array;
- }
- }
- MenuItems::getArticle("'mainmenu'");
- ?>
- <ul>
- <?php
- $menu_item_list = MenuItems::getArticle("'mainmenu'");
- var_dump($menu_item_list);
- foreach ($menu_item_list as &$an_item) {
- foreach ($an_item as $key => &$value) {
- $the_title=$an_item["title"];
- $the_path=$an_item["path"];
- echo $the_title . "=>" . $the_path . "<br>";
- }
- }
- ?>
- </ul>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement