View Single Post
Old 08-28-08, 10:13 AM   #28
t3hl33td4rg0n
Dethklok Returns!
 
t3hl33td4rg0n's Avatar
 
Join Date: Dec 2004
Location: Kent, OH
Posts: 2,142
Send a message via AIM to t3hl33td4rg0n Send a message via MSN to t3hl33td4rg0n Send a message via Yahoo to t3hl33td4rg0n
Default Re: Learn PHP/MySQL Feedback Thread

OOP was a struggle for me at first because my first programming expereinces came with GWBASIC when i was in my early teens, so procedural programming didn't come to me as second nature. The only reason i knew about it was browsing php.net and stumbled upon "class". I read about it and thought to myself "this is cool! how the hell do i use it?!"

I looked at a couple tutorials and was having a hard time grasping it until I found one specific article about it and I eventually figured it out and spending an entire day making my own from scratch... So essentially, beyond that little tutorial I found, ive been learning on the fly. Even now, I'm just hoping I'm doing it right.

Heres a template class I've been working on and off for a short while.... Any logical flaws in this way of implementing classes?

Code:
class coreTpl extends coreDb {
	
	public $tpl;
	public $tpl_name;
	public $tpl_fdir;
	public $tpl_rdir;
	public $tpl_ftpl;
	public $tpl_fcss;
	public $tpl_prts;
	
	public function getTemplate ($name, $path) {
		
		$s = 'SELECT * FROM `tpl` WHERE `name` = "'.$name.'" LIMIT 1';							// Get Template Info
		$q = mysqli_query ($this->db, $s);
		$r = mysqli_fetch_assoc ($q);
		
		$this->tpl_name = $r['name'];
		$this->tpl_fdir = $path.$r['dir'];
		$this->tpl_rdir = $path;
		$this->tpl_ftpl = $r['tpl'];
		$this->tpl_fcss = $r['css'];
		
		
		$this->tpl = file_get_contents ($this->tpl_fdir.$this->tpl_ftpl);
		$this->tpl = coreTpl::rplPathStr ($this->tpl, $this->tpl_fdir);
	}
	
	
	public function inConStd ($loc, $name, $style, $sortBy, $SortType, $offset, $limit, $where) {		// Grab Dynamic Content, And Insert Into Template
																										// Based on Loc
		$conD = coreTpl::inConStyle($style);															// Get Content Style HTML
		$out = '';
		
		$s = 'SELECT * FROM `con` WHERE `name` = "'.$name.'" ORDER BY `'.$sortBy.'` '.$SortType.' LIMIT '.$offset.', '.$limit;
		$q = mysqli_query ($this->db, $s) or die (mysqli_error ($this->db));
		
		while ($r = mysqli_fetch_assoc($q)) {										// <-- Outer Loop: Take Row And Append HTML Data Then Content
			$out .= $conD['top'];													//                Ex: TOP { Style0 | Part0 | Style1 | Part1 } BOTTOM
			for ($x = 0; $x < $conD['parts']; $x++) {								// <-- Inner Loop: Loop through array until Style Parts Meets Up With
				$part = 'part'.$x;													//                 Content Parts
				$out .= $conD[$part];												// <-- Append HTML Before Content Text
				$out .= $r[$part];													// <-- Append Content Text
			}
			$out .= $conD['bottom'];												// <-- Append Bottom HTML After Last Part Is Appended
		}
		/* Out is the dynamic content and its associated style HTML, which will be inserted into the template where $loc specifies. The output will look
		something like this: 
		
		Style TOP { Style Part0 | Content Part0 | Style Part1 | Content Part1 | Style Part2 | Content Part2 } Style BOTTOM
		
		Style Can be any HTML, like a table, div, or just about anything else and is considered a text wrapper with all the styling HTML whereas the
		content text is the actual dynamic text, which can be HTML as well. The number of parts is actually determined in the styling table. The inner loop
		will stop at the last part based on that number. So if parts >> parts is 7 then it will loop 7 times through the content data like part0 to part6 
		columns in the associative array. An example could be something like this.
		
		Parts = 3
		Style = 'My Style'
		  Top     = <table><tr>
		  part0   = <td>
		  part1   = </td><td>
		  part2   = </td></tr><tr><td colspan="2">
		  Bottom  = </td></tr></table>
		  
		Content = 'My Example'
		  Part0  = FOO
		  Part1  = BAR
		  Part2  = BAZ
		  
		Call The Method As Specified  $tpl->inConStd ('<!-- PLACEHOLDER -->', 'My Example', 'My Style', 'date', 'DESC', 0, 1);
		
		And the Result Will Be:
		
		<table><tr><td>FOO</td><td>BAR</td></tr><tr><td colspan="2">BAZ</td></tr></table>
		
		This is just a simple example, calling this method based on your criteria will retrieve 1 or more records and will loop through all rows
		returned form the query. You can also use whatever content you want with whatever style you want as well. You can also mix and match content
		in whatever way you choose. For example, if you choose to implement the user the ability to use his/her own template style and color scheme or whatever,
		it can be done very easily. Implement a little AJAX, and the user can do play with the content and page theme on the fly. Another reason is that you can 
		easily throw in as much content as you want, anywhere you want, and filter it in any way shape or form. In any case, with the content iteration methods
		the site layout, content, and functionality is completely freeform. One last example, if you want to throw in a quick snippet of info form another page
		that would normally not be on the site homepage, like a new users profile and all the features associated with profile accessability; and maybe put that
		just above the main content because this new user is important.
		*/
		
		$out = coreTpl::rplPathStr ($out, $this->tpl_fdir);							// Replace {{PATH}} and {{LIBPATH}} strings to relative locale
		switch ($where) {															// Insert Content Into Template where specified in $loc
			case 'before':
				$this->tpl = str_replace($loc, $out.$loc, $this->tpl);
			break;
			case 'after':
				$this->tpl = str_replace($loc, $loc.$out, $this->tpl);
			break;
			default:
				$this->tpl = str_replace($loc, $out.$loc, $this->tpl);
			break;
		}
									
		
	}
	
	
	private function inConStyle ($name) {											// Get The Content Style Code
		
		$s = 'SELECT * FROM `parts` WHERE `name` = "'.$name.'" LIMIT 1';
		$q = mysqli_query($this->db, $s) or die(mysql_error());
		$r = mysqli_fetch_assoc($q);
		
		return $r;
	}
	
	
	private function rplPathStr ($str, $path) {										// Replace Strings To Avoid Directory Confustion (WORKS)
		$str = str_replace('{{LIBPATH}}', $this->tpl_rdir, $str);					// Will replace {{LIBPATH}} in Template with Current Relative File Path
		$str = str_replace('{{PATH}}', $path, $str);								// Will replace {{PATH}} in Template with Current Relative File Path
		
		return $str;
	}
	
	public function putHtml ($id, $src) {
		
	}

}
And a user authentication class (pre-alpha/very unfinished):

Code:
class coreUsr extends coreDb {
	
	
	public $usr_name;
	public $usr_passwd;
	public $usr_sid;
	public $usr_auth;
	public $usr_lvl;
	public $usr_perm;
	
	
	public function usrChkAuth ($name, $passwd)	{
		
		$s = 'SELECT * FROM `usr` WHERE `name` = "'.$name.'" LIMIT 1';
		$q = mysqli_query($this->db, $s) or die(mysqli_error($this->db));
		$r = mysqli_fetch_assoc($q);
		
		if ($name == $r['name']) {
			$userName = true;
		} else {
			$userName = false;
		}
		
		if ($passwd == $r['passwd']) {
			$userPass = true;
		} else {
			$userPass = false;
		}
		
		if ($userName && $userPass) {
			$this->usr_auth = true;
			$this->usr_name = $r['name'];
			$this->usr_passwd = $passwd;
			$this->usr_lvl = $r['lvl'];
			$this->usr_sid = session_id();
			$this->usr_perm['addcon'] = $r['addcon'];
			$this->usr_perm['updcon'] = $r['updcon'];
			$this->usr_perm['delcon'] = $r['delcon'];
			return true;
		} else {
			$this->usr_auth = false;
			return false;
		}
		
	}
	
	
	public function usrLogin () {
		
		
	}
	
}




.... And as far as using what type of quotes, I always use single quotes and concatenation. I find it easier to proofread that way, and there is much less concern with escaping any strings, since theres a lot more double quotes going inside a string than single quotes.
__________________


Intel QX9650 @ 3.83GHzXFX 780ieVGA GTX 560Ti8GB GSkill DDR800Creative Sound Blaster X-Fi Titanium PCI-ESeagate 1.5TB . 4x Samsung 2TB

Quote:
Originally Posted by MowTin View Post
....it's so clear how government makes things worse not better...
t3hl33td4rg0n is offline   Reply With Quote