Go Back   nV News Forums > Software Forums > Software Development

Newegg Daily Deals

Reply
 
Thread Tools
Old 08-27-08, 12:14 PM   #25
ViN86
 
Join Date: Mar 2004
Posts: 15,486
Default Re: Learn PHP/MySQL Feedback Thread

Quote:
Originally Posted by pross View Post
nice thread so far mate, i have only one small concern so far; you are using double quotes in your variables, tho this is ok to do and works fine it can lead to hassles later especially if you want to include a double quote in the html output, for example..

$test = "hello";
$output = "<a href="http://someurl.com">" . $test;

the above snippet will break unless the double quotes inside the html link are escaped with a \

single quotes however do not have this problem..

$test = 'hello';
$output = '<a href="http://someurl.com">' . $test;
yes, good point.

i typically use double quotes and escape them when i want them in the HTML. but single quotes would probably be a better idea.
ViN86 is offline   Reply With Quote
Old 08-27-08, 02:17 PM   #26
fivefeet8
Ngemu Mod
 
Join Date: Jul 2003
Location: Fresno, CA
Posts: 1,886
Default Re: Learn PHP/MySQL Feedback Thread

Quote:
Originally Posted by ViN86 View Post
i typically use double quotes and escape them when i want them in the HTML. but single quotes would probably be a better idea.
Double quotes allows you to insert php variables directly in the string without concatenation. Ie.

Code:
$var = 'text';
echo "<input type='$var' />";
The php documentation seems to indicate that single quote strings are faster to parse though since the interpreter doesn't need to scan the string for php variables. You can also use double quotes for the php string and use single quotes for the html like above.

The primary issue with using either single or double quotes with HTML within a string is when it comes to javascript function calls and their arguments when the arguments are javascript strings. In which case, either concatenation or quote escapes will work.
__________________
[i7 2600k @4.4ghertz][2x4 GB DDR3 1600][EVGA GTX570 1.280GB SC][EVGA GTX460 physx][Asrock Extreme7 Gen3 Z68][2xSeagate 160 Gb SATA HD raid0][Seagate 250 GB SATA2 HD][Sony Bravia 40' 1080p LCD HDTV][NEC 3520a DVD+-DLw][Windows 7 Ultimate x64][Rosewill 1000w]
fivefeet8 is offline   Reply With Quote
Old 08-27-08, 02:58 PM   #27
Monolyth
Meow Mix Kills
 
Monolyth's Avatar
 
Join Date: Aug 2002
Location: END OF DAYS
Posts: 1,228
Default Re: Learn PHP/MySQL Feedback Thread

I say a full OOP thread is the best, the sooner people get comfortable with it the sooner they will be better PHP programmers. You can hack up PHP code, but once you step into OOP as well as object model development you can really do some cool things that will make your life down the road 20x easier.
__________________
Gaming 5.0
Asus R4 Extreme | Core i7 3930K @ 4.8Ghz Corsair H100 | 16GB G.Skill@1866Mhz | EVGA GTX TITAN Superclocked | Sammy 40" LCD
Asus Essence One | OCZ Revo 3 120GB, Revo 3 X2 240GB | HSPC Tech Station | Corsair HX1200 | Windows 8.1 Pro


Storage 2.0
Gigabyte Z68XP-UD3-iSSD | Core i7 2600K @ 4.5Ghz NH-D14 | 16GB G.Skill@1600Mhz | Areca 1880ix-16 RAID HBA
Temp Storage - 1.8TB (4x450GB Hitachi 15k RPM SAS) | 18TB (12x3TB Hitachi RAID10) | HSPC Tech Station | Corsair HX620 | Windows 7 x64
Monolyth is offline   Reply With Quote
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
Old 08-28-08, 02:01 PM   #29
fivefeet8
Ngemu Mod
 
Join Date: Jul 2003
Location: Fresno, CA
Posts: 1,886
Default Re: Learn PHP/MySQL Feedback Thread

Your object definitions don't have a class constructor. Most OOP languages use constructors to initialize object variables. PHP is far more lenient in this regard and allows variables to be created on the whim without initialization. It does throw a bunch of warnings though.

A discussion about classes and objects also needs to touch on passing variables by reference and by value. PHP by default, makes all variables passed into functions by reference. There is a value in the php.ini to change it to pass by value. You can then pass by reference only when you explicitly do so. Ie.

Code:
function passbyreference(&$str, &$obj, Array &$arr){

  // do something
}
All the arguments in that function will be passed by reference. According to the PHP.ini file, the default behavior of passing by reference will be deprecated in future versions of PHP(PHP6). It's wise to make your functions explicitly pass by reference if that's how you designed them to function. This default behavior caused me some headaches when my object variables were changing values when I didn't want them to after being passed into a function.
__________________
[i7 2600k @4.4ghertz][2x4 GB DDR3 1600][EVGA GTX570 1.280GB SC][EVGA GTX460 physx][Asrock Extreme7 Gen3 Z68][2xSeagate 160 Gb SATA HD raid0][Seagate 250 GB SATA2 HD][Sony Bravia 40' 1080p LCD HDTV][NEC 3520a DVD+-DLw][Windows 7 Ultimate x64][Rosewill 1000w]
fivefeet8 is offline   Reply With Quote
Old 09-16-08, 09:34 PM   #30
jcrox
Mahna Mahna
 
jcrox's Avatar
 
Join Date: Jul 2006
Location: Madison, Wi
Posts: 6,134
Default Re: Learn PHP/MySQL Feedback Thread

I'm currently taking a PHP and MySQL class so, if anyone that's wanting to learn PHP and MySQL is interested I'd be happy to post our labs and projects from class here.

Just let me know and I'll get to it.
__________________

Gaming

MSI MPower Z87 | 4770k | CNPS9900MAX-B | 8GB G.Skill 1866 | EVGA GTX760 |ThermalTake 850w | X-Fi Titanium | HAF | Asus PA248Q | Lycosa | Death Adder | Win8 Pro 64

Dev/Backup

MSI GD65 | 2500k | H50 | 8GB G.skill | GTX570 | Ubuntu 13.10

Laptop

17" Inspiron | i5 | 4GB DDR3 | 500GB HDD | Win7 Pro 64

jcrox is offline   Reply With Quote
Old 09-17-08, 03:26 PM   #31
LovingSticky
plenty of work, no games
 
LovingSticky's Avatar
 
Join Date: Sep 2007
Posts: 559
Default Re: Learn PHP/MySQL Feedback Thread

Quote:
Originally Posted by pross View Post
nice thread so far mate, i have only one small concern so far; you are using double quotes in your variables, tho this is ok to do and works fine it can lead to hassles later especially if you want to include a double quote in the html output, for example..

$test = "hello";
$output = "<a href="http://someurl.com">" . $test;

the above snippet will break unless the double quotes inside the html link are escaped with a \

single quotes however do not have this problem..

$test = 'hello';
$output = '<a href="http://someurl.com">' . $test;

Well, difference between single and double quotes is that double quotes get parsed, while single quotes do not. Ie. that if you do not need PHP to parse your strings, you should NOT use double quotes.

You should also not use [dot] to concat strings, much faster is to use comma, as it does not concat them, but just echoes them.

So, this is fastest:

echo '1', $itemid;

This is slower:

echo "1$itemid";

And this would be slowest:

echo "1".$itemid;

All those examples produce same result, of course.
__________________
Q9550@3.4 + Asus P5Q Premium + 16GB RAM + 2xVelociraptor 300GB + 4xWD Black 1TB RAID-5 on Adaptec 5805 + PoV GTX580 + Dell 3008WFP + Windows 7 x64
Xbox 360 Elite + Kinect + Onkyo receiver + Acoustic Energy Aelite 1/3/center speakers + 46" FullHD Samsung TV
LovingSticky is offline   Reply With Quote
Old 09-17-08, 04:42 PM   #32
nemecb
Registered User
 
Join Date: Jan 2005
Location: Rochester, MN
Posts: 4,018
Default Re: Learn PHP/MySQL Feedback Thread

Quote:
Originally Posted by LovingSticky View Post
You should also not use [dot] to concat strings, much faster is to use comma, as it does not concat them, but just echoes them.

So, this is fastest:

echo '1', $itemid;

This is slower:

echo "1$itemid";

And this would be slowest:

echo "1".$itemid;

All those examples produce same result, of course.
I haven't tried it, but it seems that the , concat is not actually concatenation but a separator for multiple arguments to echo, and thus is not a general purpose replacement for . concatenation. It's a useful suggestion for using echo, but keep in mind that premature optimization is the root of all evil so obfuscating your code to take advantage of this is probably not a good idea (unless of course you discover that you're taking a significant performance hit from that code, but then that wouldn't be premature optimization either). Also, it doesn't apply to the example because the result of the concatenation is being assigned to a variable, not echo'd.

So I guess my point is that it's a useful tip, but your statement that "You should also not use [dot] to concat strings" is not correct without qualification.
nemecb is offline   Reply With Quote

Old 09-17-08, 04:52 PM   #33
LovingSticky
plenty of work, no games
 
LovingSticky's Avatar
 
Join Date: Sep 2007
Posts: 559
Default Re: Learn PHP/MySQL Feedback Thread

Quote:
Originally Posted by nemecb View Post
"You should also not use [dot] to concat strings" is not correct without qualification.
Well, I think some things are pretty clear and do not have to be described in detail Like "You should also not use [dot] to concat strings unless you really need it"

What is important:
- content of "" gets parsed
- content of '' does not
__________________
Q9550@3.4 + Asus P5Q Premium + 16GB RAM + 2xVelociraptor 300GB + 4xWD Black 1TB RAID-5 on Adaptec 5805 + PoV GTX580 + Dell 3008WFP + Windows 7 x64
Xbox 360 Elite + Kinect + Onkyo receiver + Acoustic Energy Aelite 1/3/center speakers + 46" FullHD Samsung TV
LovingSticky is offline   Reply With Quote
Old 09-17-08, 05:01 PM   #34
nemecb
Registered User
 
Join Date: Jan 2005
Location: Rochester, MN
Posts: 4,018
Default Re: Learn PHP/MySQL Feedback Thread

Quote:
Originally Posted by LovingSticky View Post
Well, I think some things are pretty clear and do not have to be described in detail Like "You should also not use [dot] to concat strings unless you really need it"
Yeah, no problem. I was just afraid a newbie might misinterpret that as a general replacement for string concatenation and be really confused when they got syntax errors.
nemecb is offline   Reply With Quote
Old 09-25-08, 01:27 PM   #35
ViN86
 
Join Date: Mar 2004
Posts: 15,486
Default Re: Learn PHP/MySQL Feedback Thread

im sorry i have been tremendously busy with school. i will get to the tutorial asap.
ViN86 is offline   Reply With Quote
Old 09-25-08, 04:04 PM   #36
DiscipleDOC
 
DiscipleDOC's Avatar
 
Join Date: Dec 2002
Location: Alabama, Planet Earth
Posts: 5,993
Default Re: Learn PHP/MySQL Feedback Thread

Quote:
Originally Posted by ViN86 View Post
im sorry i have been tremendously busy with school. i will get to the tutorial asap.
Slacker!

DiscipleDOC is offline   Reply With Quote
Reply


Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


Similar Threads
Thread Thread Starter Forum Replies Last Post
Official Max Payne 3 Feedback Thread Ghosthunter Gaming Central 94 10-05-12 06:00 AM
Detonator 40.72 feedback thread mongoose NVIDIA Windows Graphics Drivers 94 12-03-02 10:45 AM
Official detonator 40.71 feedback thread Crake NVIDIA Windows Graphics Drivers 88 10-01-02 11:38 AM
Detonator 30.30 feedback thread Matthyahuw NVIDIA Windows Graphics Drivers 26 07-31-02 09:14 PM

All times are GMT -5. The time now is 04:37 AM.


Powered by vBulletin® Version 3.7.1
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Copyright 1998 - 2014, nV News.