Results 1 to 10 of 20
-
17th Jun 2009, 10:11 PM #1OPMemberWebsite's:
litewarez.net litewarez.com triniwarez.comA Snippet from my latest project
Ok so this is just an insider to a new project im working on
now this project is a professional website that is built on PHP 5.1+ and will not be released under any licence and im not going to be telling you what the site is etc because i like to keep my professional work seperate to my fun work.. aka the scene so here take a look at it
mysql.class.php
PHP Code:<?php
class MySql{
var $settings = array();
var $handlers = array();
var $errors = array();
var $query_Data = array();
function __construct($host,$user,$pass,$db){
$this->settings['credentials']['host'] = $host;
$this->settings['credentials']['user'] = $user;
$this->settings['credentials']['pass'] = $pass;
$this->settings['credentials']['data'] = $db;
//Start the connection
$this->__Connect();
$this->__Assoc_Database();
//Some Charactor Setting
mysql_query("SET NAMES 'utf8'",$this->handlers['connect']);
mysql_query("SET CHARASET SET 'utf8'",$this->handlers['connect']);
}
public function query($query){
$resource = mysql_query($query);
if($resource){
if(is_resource($resource)){
//Set some vars
$i = 0;
$data = array();
//Loop the results
while($row = mysql_fetch_assoc($resource)){
$this->query_Data[$i] = $row;
$i++; //Post ++
}
mysql_free_result($resource);
$query = new stdClass();
$query->row = (isset($this->query_Data[0]) ? $this->query_Data : array());
$query->rows = $this->query_Data;
$query->numrows = $i;
unset($this->query_Data);
return $query;
}
}else{
$this->__Error(__FILE__,__CLASS__,__METHOD__,__LINE__,mysql_error());
}
}
private function __Connect(){
$this->handlers['connect'] = mysql_connect(
$this->settings['credentials']['host'],
$this->settings['credentials']['user'],
$this->settings['credentials']['pass']) or
$this->__Error(__FILE__,__CLASS__,__METHOD__,__LINE__,mysql_error());
}
private function __Assoc_Database(){
$this->handlers['database'] = mysql_select_db(
$this->settings['credentials']['data']) or
$this->__Error(__FILE__,__CLASS__,__METHOD__,__LINE__,mysql_error());
}
private function __Error($file,$class,$funtion,$line,$error){
die(
sprintf("<pre class='error_php_internal'>There was an error\rFILE: %s\rCLASS: %s\rMETHOD: %s\rLINE: %d\rERROR: %s</pre>",
$file,
$class,
$function,
$line,
$error)
);
}
public function hello(){
echo 'hello';
}
function __destruct(){
mysql_close($this->handlers['connect']);
unset(
$this->settings,
$this->handlers,
$this->errors,
$this->query_Data
);
}
}
?>
PHP Code:<?php
//used for holding objects on one array() ....
final class Registry{
static private $data = array();
static public function get($key){
return (isset(self::$data[$key]) ? self::$data[$key] : NULL);
}
static public function set($key,$value){
self::$data[$key] = $value;
}
static function has($key){
return isset(self::$data[$key]);
}
}
?>
PHP Code:<?php
//Include Main Config
include 'config.php';
//Include Startup
include $settings['dir']['includes'] . 'startup.php';
//Sessions
Registry::set('sessions',new Sessions);
//Config
$Config = new Config;
Registry::set('config',$Config);
//Mysql
$MySql = new MySql($settings['mysql']['host'],$settings['mysql']['user'],$settings['mysql']['pass'],$settings['mysql']['data']);
Registry::set('mysql',$MySql);
//Settings (keep uppercase because it will overide the config file);
$Settings = $MySql->query('SELECT * FROM settings');
foreach($Settings->rows as $row){$Config->set($row['key'], $row['value']);}
$Templater = new Templater;
Registry::set('Templater',$Templater);
?>
but enjoy looking at my source and comment on what you think of my OOPlitewarez Reviewed by litewarez on . A Snippet from my latest project Ok so this is just an insider to a new project im working on now this project is a professional website that is built on PHP 5.1+ and will not be released under any licence and im not going to be telling you what the site is etc because i like to keep my professional work seperate to my fun work.. aka the scene so here take a look at it mysql.class.php <?php class MySql{ Rating: 5Join Litewarez.net today and become apart of the community.
Unique | Clean | Advanced (All with you in mind)
Downloads | Webmasters
Notifications,Forum,Chat,Community all at Litewarez Webmasters
-
18th Jun 2009, 03:35 AM #2Respected DeveloperWebsite's:
X4B.orgThe DBAL (Database class) is quite nice, howeaver I would change
Code:mysql_query("SET NAMES 'utf8'",$this->handlers['connect']); mysql_query("SET CHARASET SET 'utf8'",$this->handlers['connect']);
OOP is good, as long as its not overused. Use it for DBAL, DOM and other things but do not use it for function storage and the likes, there is no point.
Not a fan of the way you combined mysql_query and mysql_fetch_assoc in
Code:public function
Code:query($query){ ...
-
18th Jun 2009, 09:12 AM #3OPMemberWebsite's:
litewarez.net litewarez.com triniwarez.comlol yea i aggree with keeping the the query in the internal object lol the reason i dont that at first was because $this->query() was only going to be used for returning arrays but i chancged it to check to see if the handler returned a resource but as ive update the class a lil i forgot to update that section
the reason for combining the class and the mysql_fetch _assoc is do the to template system where i only want foreach loops and while count loops, i think this way it is easier on the template system.
thanks for the comment tho doodJoin Litewarez.net today and become apart of the community.
Unique | Clean | Advanced (All with you in mind)
Downloads | Webmasters
Notifications,Forum,Chat,Community all at Litewarez Webmasters
-
18th Jun 2009, 10:32 AM #4MemberWebsite's:
WareztheDDL.com GTFO.wsDamn i thought i was special
-
18th Jun 2009, 11:30 AM #5Respected DeveloperWebsite's:
X4B.org
-
18th Jun 2009, 11:38 AM #6OPMemberWebsite's:
litewarez.net litewarez.com triniwarez.comsplitice please do id like to see your snippets :G just drop a few classes in here
Join Litewarez.net today and become apart of the community.
Unique | Clean | Advanced (All with you in mind)
Downloads | Webmasters
Notifications,Forum,Chat,Community all at Litewarez Webmasters
-
18th Jun 2009, 03:57 PM #7MemberWebsite's:
Plutost.com OffshorePort.net Desi-Mobilez.com PhotoshopDesigner.co.uk TutsBy.me BabesWallpapers.netdamm...
everything.. just gone up my heads
lol I know only the newbish PHPlol
I need to learn more
Anyways.. by seeing your excitement, good job litewarezPlutost.com Asia & Europe Hosting Provider / Offshore Shared/VPS Hosting / Even Better than Gold
WHMCS License Verify (Catch those scam hosts)
Tutorials By .Me
-
19th Jun 2009, 03:13 AM #8Respected DeveloperWebsite's:
X4B.orgOk the code im currently rewirting, I wrote this maybe 6 months ago but im not happy with it.
Code:<?php /** * Inserts values from $arr2 after (or before) $key in $arr1 * if $key is not found, $arr2 is appended to $arr1 using array_merge() * * @param $arr1 * array to insert into * @param $key * key of $arr1 to insert after * @param $arr2 * array whose values should be inserted * @param $before * insert before the given key. defaults to inserting after * @return * merged array */ function array_insert($arr1, $key, $arr2, $before = FALSE){ $done = FALSE; foreach($arr1 as $arr1_key => $arr1_val){ if(!$before){ $new_array[$arr1_key] = $arr1_val; } if($arr1_key == $key && !$done){ foreach($arr2 as $arr2_key => $arr2_val){ $new_array[$arr2_key] = $arr2_val; } $done = TRUE; } if($before){ $new_array[$arr1_key] = $arr1_val; } } if(!$done){ $new_array = array_merge($arr1, $arr2); } return $new_array; } class post_parse_node{ public $tag = ''; public $attrib = ''; public $inner = array(); public $parent = null; public $main_class; public $uid = false; function post_parse_node($tag,$attrib,&$parent,$uid=false){ $this->tag = $tag; $this->attrib = $attrib; $this->parent = $parent; $this->uid = $uid; return $this; } function plain_text($set=false){ if($set){ foreach($this->inner as $i){ $i->remove(); } unset($this->inner); $this->inner[] = new post_parse_node('__',$set,$this); return true; }else{ $<span class="searchlite">bbcode</span> = ''; if($this->tag==='__'){ return $this->attrib; }else{ foreach($this->inner as $tag){ $<span class="searchlite">bbcode</span> .= $tag->plain_text(); } return $bbcode; } } } function <span class="searchlite">bbcode</span>(){ if($this->tag==='__'){ return $this->attrib; }else{ $<span class="searchlite">bbcode</span> = '['.$this->tag.($this->attrib?'='.$this->attrib:'').($this->uid?':'.$this->uid:'').']'; foreach($this->inner as $tag){ $<span class="searchlite">bbcode</span> .= $tag-><span class="searchlite">bbcode</span>(); } return $<span class="searchlite">bbcode</span>.'[/'.$this->tag.($this->uid?':'.$this->uid:'').']'; } } function remove(){ $this->main_class->remove_tag($this); if($this->parent instanceof post_parse_node && isset($this->parent->elements)&&count($this->parent->elements)){ foreach($this->parent->elements as $k=>$e){ if($e===$this){ unset($this->parent->elements[$k]); } } } if(isset($this->elements)&&count($this->elements)){ foreach($this->elements as &$e){ $e->parent = $this->parent(); if($this->parent){ $this->parent->elements[] = $e; } } } unset($this); } function insert_parent($item, $attrib=''){ $old_parent = &$this->parent; $t = false; if($old_parent instanceof post_parse_node){ foreach($old_parent->inner as $k=>$e){ if($e===$this){ die(var_dump($this->uid)); $t = new post_parse_node($item,$attrib,$old_parent,$this->uid); $old_parent->inner[$k] = $t; } } }else{ foreach($this->main_class->elements as $k=>$e){ if($e===$this){ $t = new post_parse_node($item,$attrib,$old_parent,$this->uid); $this->main_class->elements[$k] = &$t; $this->main_class->elements[] = &$this; } } } if($t){ $t->inner[] = &$this; $this->parent = &$t; return true; } return false; } function clear(){ foreach($this->inner as $k=>&$i){ if($i instanceof post_parse_node){ $i->clear(); if($this->inner[$k] instanceof post_parse_node){ $this->inner[$k]->clear(); } unset($this->inner[$k]); unset($i); } } unset($this->main_class); if(isset($this->parent)){ unset($this->parent); } return true; } } class post_parse{ var $elements = array(); public $uid=false; function post_parse($pdata,$uid=false){ $this->uid = $uid; $this->parse($pdata); return true; } /* * Recursive function to parse <span class="searchlite">bbcode</span> */ function parse($post){ $tags = $this->_tag_array($post); $tags = $this->_sort_parent($tags); $tags = $this->_process_inner($tags); $this->elements = $tags; } private function _process_inner($tags){ foreach($tags as $t){ if($t->parent){ $t->parent->inner[] = $t; } } return $tags; } private function is_text($test){ if($test === '__'){ return true; } return false; } private function _sort_parent($tags){ $ret = array(); $parent = 0;//Root elements get a parent of 0 foreach($tags as $v){ if($this->is_open($v['tag'])){ if(!isset($v['attrib'])) $v['attrib'] = ''; $r = new post_parse_node($v['tag'],$v['attrib'],$parent,$this->uid); $r->main_class = &$this; if(!$this->is_text($v['tag'])) $parent = $r; $ret[] = $r; }else{ $parent = $parent->parent; } } return $ret; } function remove_tag($ref){ foreach($this->elements as $k=>$e){ if($e===$ref){ unset($this->elements[$k]); } } } function is_open($tag){ if($tag{0}==='/'||$tag{0}==='\\'){ return false; } return true; } private function _tag_array($post){ $pos = 0; $tags = array(); $open_tag = false; $text = ''; $pr = strlen($post); while ( isset($post {$pos})|| $pr>$pos) { if($post {$pos}==='['){//open tag $open_tag = ''; } if($open_tag===false){ $text .= $post {$pos}; }else{//Tag name $open_tag .= $post {$pos}; } if($post {$pos}===']'&&strlen($open_tag)){//close if(strlen($text)){ $tags[] = array('tag'=>'__', 'attrib'=>$text); $text = ''; } $open_tag = substr($open_tag, 1, strlen($open_tag)-2); if(strpos($open_tag,'=')){ $open_tag = explode('=',$open_tag); if($this->uid){ $tags[] = array('tag'=>$open_tag[0],'attrib'=>str_replace(':'.$this->uid,'',$open_tag[1])); }else{ $tags[] = array('tag'=>$open_tag[0],'attrib'=>$open_tag[1]); } }else{ if($this->uid){ $tags[] = array('tag'=>str_replace(':'.$this->uid,'',$open_tag)); } else $tags[] = array('tag'=>$open_tag); } $open_tag = false; } $pos ++; } return $tags; } function clear(){ foreach($this->elements as $k=>$e){ if($e instanceof post_parse_node ){ $e->clear(); unset($e); if($this->elements[$k] instanceof post_parse_node ){ $this->elements[$k]->clear(); unset($this->elements[$k]); } } } return true; } function find_tag($tag_name){ $ret = array(); foreach($this->elements as $tag){ if($tag->tag === $tag_name){ $ret[] = $tag; } } return $ret; } function <span class="searchlite">bbcode</span>(){ $ret = ''; foreach($this->elements as $tag){ if($tag->parent===0) $ret .= $tag-><span class="searchlite">bbcode</span>(); } return $ret; } }
-
19th Jun 2009, 12:45 PM #9OPMemberWebsite's:
litewarez.net litewarez.com triniwarez.comlmao i sat here looking at this line tihkning wtf lol and then i reailises you must of copied of a php highlighted post lool
$<span class="searchlite">bbcode</span> = '['.$this->tag.($this->attrib?'='.$this->attrib:'').($this->uid?':'.$this->uid:'').']';
should be
$bbcode = '['.$this->tag.($this->attrib?'='.$this->attrib:'').($this->uid?':'.$this->uid:'').']';
lol but yea your code is good but i prefer to keep my code as neat as possible and some rules i follow is trying not to use $array[0] or [1] etc also now using my registry system i can do stuff like
Registry::get('mysql')->query('SELECT * FROM settings')->rows;
the firs section Registry->get('mysql') will retrieve the object out of the registry array then you follow on to access the mysql class and run the query function and then as i use an stdClass i just call for the rows or row cound or just the single row... this is a real times saver :G
but yea good code doodJoin Litewarez.net today and become apart of the community.
Unique | Clean | Advanced (All with you in mind)
Downloads | Webmasters
Notifications,Forum,Chat,Community all at Litewarez Webmasters
-
20th Jun 2009, 03:04 AM #10Respected DeveloperWebsite's:
X4B.orgLOL, yeah I copied it from a post I made, this code isnt the neatest. It was my first dabble in making a dom class. Really it is quite an advanced peice of code hence why it makes little sence to most.
Im not really a fan of registry classes, they add quite alot of overhead to page generation. I find its better to just have a few classes that are always defined as the same variable e.g $db, $acl, $user etc.
I think Ill post a download to ddl2 rc1 in this thread for you take a look and give me an honest code review before I release normally if your up for it.
Sponsored Links
Thread Information
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)
Similar Threads
-
Snippet of the Day
By SplitIce in forum Web Development AreaReplies: 41Last Post: 26th Aug 2012, 06:09 PM -
Plz Help To Add A Php Snippet Into My DLE Index !
By JoomlaZ in forum Web Development AreaReplies: 0Last Post: 7th Jul 2011, 01:18 PM -
BWscene Project - Partners wanted - HUGE project
By ushare in forum Community CooperativeReplies: 0Last Post: 4th Mar 2011, 11:18 PM -
[C#] Tiny Web Server (snippet)
By Hyperz in forum Web Development AreaReplies: 6Last Post: 24th Jun 2010, 01:19 PM -
See real OOP (Snippet from Litewarez V2) Webmasters CP
By litewarez in forum Tutorials and GuidesReplies: 21Last Post: 19th Sep 2009, 03:59 PM
themaCreator - create posts from...
Version 3.45 released. Open older version (or...