October 9, 2009

Real-World OOP with PHP and MySQL

Due to actual copyright constraints I can’t post the actual contents of this tutorial up here, but I can post the excerpt and provide you with a link to check it out, just click the image to head over to Net-Tuts and check it out

Nettuts - Real World OOP

October 6, 2009

Preserve query strings

As you keep developing your website, you’ll notice that occasionally you’ll need to keep track of more than one query string at a time. Eventually, you’ll find that this gets to be such a task in itself, you’ll wish there was an easier way to do it. That is what the preserve_links() function does. It takes the url, grabs all the query strings, updates the query string based on what you want, and then uploads it all.

<?php 
function preserve_links($option,$value){
	$keys = array_keys($_GET); 
	$str = $_SERVER['PHP_SELF'].'?'.$option.'='.$value; 
 
	foreach($keys as $key=>$val){
		$tmp[$val] = addslashes(strip_tags($_GET[$val])); 
		if($tmp[$val] != '' && $option != $val){
			$str .= '&'.$val.'='.$tmp[$val]; 
		}
	}
 
	return $str; 
}
?>

As you can see it’s fairly straight forward. You pass the query string you want to set as the arguments. It doesn’t matter if the query string is one that is already present in the url as it will be overwritten. For example if your url is:

index.php?page=4&id=12

running the command preserve_links(‘page’,5); will generate the string of index.php?page=5&id=12

If you wish to append a query string preserve_links(‘session’,38581); will generate a url of index.php?session=38581&page=5&id=12

It’s a quick nifty little function that saves you a lot of headaches!

September 17, 2009

Simple query strings

Often times a developer will want to pass a piece of information between pages on his website. While the most desirable method to do this is via a form, sometime this just isn’t possible. During these times, it’s time to turn to query strings.

Query strings are a bit of a confusing concept, mostly because of their name. If you ignore the terminology, it is just a way for you to pass information between pages on a website using the url. This is the key to Query strings. They make your url go from this

index.php

to this:

index.php?q1=something&q2=somethingelse

And that’s all a query string is. When a user sees a link like that in the location bar in their browser what they are really seeing is index.php but with a little added information passed to it.

How do you do it?

Creating query strings is ridiculously simple. Say you want to pass an id to news.php. You would like it like this:

<a href="index.php?id=4">Link to index.php with id 4</a>

If you wanted to pass an id and another value, you would do it like this:

<a href="index.php?id=4&another=value">Link to index.php with id 4 and another value</a>

And that’s it. If a user clicks on that link they will be directed to index.php with your query string attached!

How do I get that information?

Writing the query string, of course, is only half the battle. The other half is actually reading that query string. This is where the $_GET array comes into play. $_GET is built into PHP and provides you access to the values after the ? in query strings. Since $_GET is an array, the key that you will use to access your values, are their names!

A little confusing, but let me give you a quick breakdown using one of our query strings:

key = id
value = 4

So that means, to get that value 4, we would need to use $_GET['id'].

If we follow that logic, then to get the value of “another” we would use $_GET['another'].

Summary

Query strings are a little confusing until you realize that they’re just a way of passing variables through the url.

Still confused? Post your questions in the comments or send me an email!