Hej udviklere,
Jeg har i lang tid været væk fra programmering og udvikling, men fik i dag lyst til at starte igen
Jeg er begyndt på en såkaldt "toolbox", en class hvor jeg vil have en masse nyttige funktioner til brug i fremtidige projekter.
Jeg vil gerne vide om der er noget jeg kan optimere og gøre bedre/hurtigere.
- <?php
- /*
- **Lets bring out the amazing toolbox!
- */
-
- include_once('class.toolbox.php');
-
-
- class database extends toolbox {
-
- /*
- **Version
- */
- public $version = '0.1.1';
-
- /*
- **Random $variables
- */
- private $con = false; // Checks to see if the connection is active
- private $result = array(); // Results that are returned from the query
- private $order;
- private $limit;
- private $select;
-
- /*
- **Connection handler
- */
- private $mysqli;
-
- /*
- **Connect function
- */
- public function connect() {
-
- $this->mysqli = new mysqli($this->mysql_host, $this->mysql_user, $this->mysql_pw, $this->mysql_db);
-
- //Check for errors during connection.
- if($this->mysqli->connect_errno) {
-
- $this->log($this->mysqli->connect_error);
- exit('Error connecting to host');
-
- }
-
- return true;
-
- }
-
- /*
- **Close connection function
- */
- public function closeConnection() {
-
- $this->mysqli->close();
-
- }
-
- /*
- **Execute function
- */
- public function execute($query) {
-
- if(!$result = $this->mysqli->query($query)) {
- $this->log($this->mysqli->error.' Query: '.$query);
- exit('Error executing query.');
- }
-
- $this->reset();
- return $result;
-
- }
-
- /*
- **Delete function
- */
- public function delete($table) {
- $delete = "DELETE FROM {$table}";
-
- if(isset($this->where))
- $delete .= " WHERE $this->where";
- if(isset($this->limit))
- $delete .= " LIMIT $this->limit";
-
- return $this->execute($delete);
-
- }
-
- /*
- **Update function
- */
- public function update($table, $changes) {
-
- $update = 'UPDATE '.$table.' SET ';
- foreach($changes as $field=>$value) {
- $update .= "`".$this->sanitize($field)."`='".$this->sanitize($value)."',";
- }
-
- //Remove our last comma
- $update = substr($update, 0, -1);
-
- if(isset($this->where))
- $update.= 'WHERE '. $this->where;
-
- return($this->execute($update));
-
-
- }
-
- /*
- **Insert Function
- */
- public function insert($table, $data) {
-
- $fields = '';
- $values = '';
-
- foreach($data as $f=>$v) {
-
- $fields .= $this->sanitize("`{$f}`,");
- $values .= (is_numeric($v) && (intval($v)==$v) ? $this->sanitize($v)."," : "'".$this->sanitize($v)."',");
-
- }
-
- //Remove commas
- $fields = substr($fields, 0, -1);
- $values = substr($values, 0, -1);
-
- $insert = "INSERT INTO {$table} ({$fields}) VALUES({$values})";
-
- return($this->execute($insert));
-
- }
-
- /*
- **Get function
- */
- public function get($table) {
-
- if(isset($this->select))
- $get = "SELECT {$this->select}";
- else
- $get = "SELECT *";
-
- $get .= " FROM {$table}";
-
- if(isset($this->where))
- $get .= " WHERE {$this->where}";
- if(isset($this->order))
- $get .= " ORDER BY $this->order";
- if(isset($this->limit))
- $get .= " LIMIT $this->limit";
-
- $result = $this->execute($get);
-
- if($result->num_rows > 0) {
-
- while($row = $result->fetch_object())
- $x[] = $row;
-
- }
- else
- return false;
- return $x;
-
-
- }
-
- /*
- **Order function
- */
- public function order($row, $how) {
- $this->order = $this->sanitize("`$row` $how");
- }
-
- /*
- **Limit function
- */
- public function limit($first, $second = 0) {
- $limit = $this->sanitize($first);
- if($second!=0)
- $limit .= ','.$this->sanitize($second);
-
- $this->limit = $limit;
-
- }
-
- /*
- **Select function
- */
- public function select($data) {
- $this->select = $this->sanitize($data);
- }
-
- /*
- **Where function
- */
- public function where($row, $value) {
- if(isset($this->where))
- $where .= " AND {$row} = '{$value}'";
- else
- $where = "{$row} = '{$value}'";
- }
-
- /*
- **Where or function
- */
- public function whereOr($row, $value, $row2, $value2) {
- if(isset($this->where))
- $this->where .= " AND $row = '$value' OR $row2 = '$value2'";
- else
- $this->where = "$row = '$value' OR $row2 = '$value2'";
- }
-
- /*
- **Sanitize function
- */
- public function sanitize($str) {
- return $this->mysqli->real_escape_string($str);
- }
-
- /*
- **Reset function
- */
- public function reset() {
- unset($this->order);
- unset($this->limit);
- unset($this->select);
- unset($this->where);
-
- return true;
- }
-
-
- /*
- **__destruct function
- */
- public function __destruct() {
- $this->closeConnection();
- }
-
-
- }
-
- ?>
Min toolbox class:
- <?php
-
- class toolbox {
-
- /*
- **Version
- */
- public $version = '0.1.3';
-
- /*
- **Mysql login credentials
- */
- public $mysql_host = 'localhost';
- public $mysql_user = 'root';
- public $mysql_pw = 'root';
- public $mysql_db = 'toolbox';
-
- public $domain = 'http://localhost:8888/helpers/'; /* Domain name here */
- public $document_root = '/helpers';
-
- /*
- **Domain function, if anybody likes it this way?!
- */
- public function get_domain() {
- return $this->domain;
- }
-
- /*
- **Get root function, if anybody likes it this way?!
- */
- public function get_root() {
- return $this->document_root;
- }
-
- /*
- ** Remove spaces from a string
- */
- public function remove_space($var) {
- return str_replace(' ', '', $var);
- }
-
- /*
- **Get extension from a file, careful though! only works if the file doesn't have "." in it's name
- */
- public function extension($var) {
- return strrchr($var, '.');
- }
-
- /*
- **Create a unique string based on time
- */
- public function unique_string() {
- return substr(md5(time()), 0, 5);
- }
-
- /*
- **Create a html redirect
- */
- public function redirect($url, $seconds, $message) {
- $url = $this->domain.$url;
- $data = '<meta HTTP-EQUIV="REFRESH" content="'.$seconds.'; url='.$url.'">';
- $data .= $message;
-
- return $data;
- }
-
-
- /*
- **Anchor function
- */
- public function anchor($link, $text, $other = '') {
-
- if($other=='outside')
- $link = $link;
- else
- $link = $this->domain . $link;
-
- $data = '<a href="'. $link .'"';
- $data .= ' '. $other;
- $data .= '>';
- $data .= $text;
- $data .= '</a>';
-
- return $data;
-
- }
- /*
- **Break function
- */
- public function br($breaks = 1) {
- $data = '';
- for($i=1;$i<=$breaks;$i++)
- $data .= '<br />';
-
- return $data;
- }
-
- /*
- **New line function
- */
- public function nl($nls = 1) {
-
- $data = '';
- for($i=1;$i<=$nls;$i++)
- $data .= "\n";
-
- return $data;
-
- }
-
- /*
- **New line and break function combined
- */
- public function nlbr($lines = 1) {
-
- $data = '';
- for($i=1;$i<=$lines;$i++)
- $data .= '<br />'."\n";
-
- return $data;
-
- }
-
- /*
- **Heading function
- */
- public function heading($heading, $version, $extra = '') {
-
- if(!is_int($version)) $version = 2;
-
- $data = '<h'. $version;
- $data .= ' '. $extra;
- $data .= '>';
- $data .= $heading;
- $data .= '</h'.$version.'>';
-
- return $data;
- }
-
- /*
- **Segment function
- */
- public function segment($number, $dummies='') {
-
- $params = explode('/', substr($_SERVER['PHP_SELF'], 1));
-
- if($number=='all')
- return $params;
- else
- if($dummies==true) {
- return $params[($number-1)];
- }
- else
- return $params[$number];
-
- }
-
- /*
- **Detect language of browser function
- */
- public function detect_language() {
-
- $language = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
-
- if($language=='da')
- $language = 'danish';
- elseif($language=='en')
- $language = 'english';
- else
- $language = $language;
-
- return $language;
-
- }
-
- /*
- **Test language function, to add your own language to detect_language() function above ^^
- */
- public function test_language() {
- return $language = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
- }
-
- /*
- **Log function
- */
- public function log($msg) {
-
- $date = date('H:i:s d-m-Y');
- $date2 = date('d-m-Y');
- /*
- **You may need to edit filename to the right directory
- */
- $filename = '../logs/'.$date2.'.log';
-
- $log_content = $date." ";
- $log_content .= $msg." ";
- $log_content .= $_SERVER['REMOTE_ADDR']." ";
- $log_content .= $_SERVER['PHP_SELF'];
- $log_content .= "\n";
-
- if(file_exists($filename)) {
- $log = file_get_contents($filename);
- $log = $log.$log_content;
- file_put_contents($filename, $log);
- }
- else
- file_put_contents($filename, $log_content);
-
- }
-
- }
-
-
-
- ?>
Jeg ville også gerne vide om der var noget i synes jeg skulle tilføje til min "toolbox"