Creating a Multi-Page Site From One Page Using GET

Creating a Multi-Page Site From One Page Using GET from Caleb Jonasson on Vimeo.

Source Code

Download Zip File

1
2
3
4
5
6
7
8
9
10
11
12
13
<?php
 
$t = $_GET['t'];
 
if($t == 'contact'){
	echo 'contact page';
}else if($t == 'about'){
	echo 'about page';
}else{
	echo 'index page';
	echo '<br> <a href="?t=about">about</a>';
}
?>

Adding The ‘Like’ Button to WordPress

Plugins can often slow down WordPress’ load times so it best to build into the theme if possible. This is why we are going to run through a quick tutorial were we will be adding a like button to a WordPress theme.

Getting The Like Button Code

The first thing that you are going to need is the like button code itself. I will be providing it for you but if you would like to go and get it yourself there are some more customization options by going to the Facebook developer page.

1
2
3
<iframe src="http://www.facebook.com/widgets/like.php?href=http://skorg.org/"
scrolling="no" frameborder="0"
style="border:none; width:480px; height:80px"></iframe>

Now we need to find a good place for our like button to go. I would recommend placing it below the post so once the user has completed reading through the article they can then like it. If you put it above there is a chance that they are going to miss the button and just move on.

The button is going to be added to the bottom of the post on the single.php page that you can find in your theme directory. Just go to ‘/wp-content/themes/your-theme-name/single.php’.

Now we will simply add the code in and tweak a couple of things so it will work on each page.

1
2
3
<iframe src="http://www.facebook.com/widgets/like.php?href=<?php the_permalink()?>"
scrolling="no" frameborder="0"
style="border:none; width:480px; height:80px"></iframe>

Adding the Code to Your Theme

The code will be inserted and your single page should look something more like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<div class="clear"></div>   
	<div class="meta-entry clear">
		<?php
			//full post
        	the_content(__('read more...', 'skorg-cwd1'));
        ?>
        <iframe src="http://www.facebook.com/widgets/like.php?href=<?php the_permalink()?>"
        scrolling="no" frameborder="0"
        style="border:none; width:480px; height:80px"></iframe>
 
       	<?php edit_post_link(__('Edit!', 'skorg-cwd1')); ?>
        <?php wp_link_pages(); ?>
    </div><!--end entry-->
	<div class="post-footer">

Notes

When inserting the post just look for something call the_content() then simply past the code bellow this and your like button should work without a flaw.

SQL: One To Many Using Inner Join

In this tutorial will be going through it as though we were about to create a browser game that involves units, players, stats, etc.

How Does an Inner Join Work?

Before we get to far into this and start making our own tables you need to first know how the tables are going to do what they do.  We are going to be using a join to connect two tables, this join is called an inner join, with this join type we will be able to link the tables together on a similar column. In this case it will be an ID.

In this game we are going to be needing some players and some units that they will be able to control. So, from within the units table we will have an id that will match the players id. This will allow us to merge the tables on a specific column.

What Will the Inner Join Tables Look Like?

Before we can get to the inner join table we will first need the players table. This will have a name, password and id. The id will have to be auto incremental in order to keep track of the id’s and keep them all unique.

Now that we have our users table we will need to merge them onto the units table to keep track of what units the player owns. The inner join will be performed on the user_id on the table below to the id on the table above.

Once the tables are merged together you will get the following results.

As you can see the table now consists of multiple players and the units. This method of table construction can be very powerful since you can then do another inner join from the units table to the singular unit table. This will allow you to link the individual health, strength, etc of a unit to the player.

SQL Query of an Inner Join

The sql query looks a little odd when you first look at it but once you go through what is going on you will begin to understand it a lot better.

1
2
3
4
5
SELECT
p.id p_id, p.name p_name,
u.user_id u_user_id, u.unit_id u_unit_id, u.id u_id
FROM players p
INNER JOIN units u ON p_id = u_user_id

You can also select all of the users based on the user id. This will involve using the previous statement but it will just require you to add the “WHERE” statement. Your code will now look like this.

1
2
3
4
5
6
SELECT
p.id p_id, p.name p_name,
u.user_id u_user_id, u.unit_id u_unit_id, u.id u_id
FROM players p
INNER JOIN units u ON p_id = u_user_id
WHERE u_id = '54'

This is now where the true one to many statement happens. one player to many units. And if you actually work on a game like this you can expand onwards and use a one to one unit or many to many using linking tables.

Query One To Many Using PHP

In order to query a one to many using PHP will just need to use the following code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//store the sql query in the variable $sql
$sql = "SELECT
p.id p_id, p.name p_name,
u.user_id u_user_id, u.unit_id u_unit_id, u.id u_id
FROM players p
INNER JOIN units u ON p_id = u_user_id
WHERE u_id = '2'";
 
//query the database and store the results into $t1. If query fails display the error and kill the page.
$t1 = mysql_query($sql)or die(mysql_error());
 
//This will loop through the results and display them for you
while($row = mysql_fetch_array($t1)){
	echo 'unit id: '.$row['u_unit_id'].'<br>';
}

Additional Notes:

When you are creating the tables keep in mind that every id field should be stored as an integer and should match the limit. If you expect to have a large game down the road make sure that all of your id’s have a length of 11 or 15. That number is up to you but it is best to keep them all at the same length.

Using Sessions To Handle Errors With PHP

Here is a 10 minute video tutorial that is all about handling errors with sessions. It will teach you how to use three simple functions to set and display error messages for your PHP website or application.

Using sessions to handle errors in php from Caleb Jonasson on Vimeo.

Sorry about the styling error, apparently I cannot type.

Demo

Download Source Files

Recycle pages by splitting your website into pieces with PHP

Recycle pages by splitting your website into pieces with PHP from Calebj on Vimeo.

Edit: Tuesday December 28th 2010, 9:11pm

I have just uploaded the archived end result at a comments request. You can download it here: Recycle_pages_code_with_design.zip

PHP: Increasing a scripts runtime

Often when working with long php scripts you may need more time then is aloud. Recently when working with lirkr search engine I came across a problem where the server would time me out due to the script taking so long. The reason this was taking so long is because I had to read information from pages and load it into the search engines database word by word and check if the word already exists.

Here is a simple way to change the duration of time a script is aloud to be executed for.

1
2
3
<?php
ini_set(max_execution_time, "300");
?>

Be careful when using this script. If your code lasts longer then a few minutes then chances are the code is not very efficient, you have a infinite loop somewhere or you could just be working with a database that not a lot of people will ever have to use.

Going through website creation part 2: Backend

This step mainly involves breaking the site up into sections of PHP. This will allow us to use various parts and pieces to make the site dynamic.

We may want to create a database and for this article I will be creating a very simple design that is just going to be used to display the information on the page. We will do this by adding a simple connect.php page that will contain the connection information.

1
2
3
4
5
6
7
8
<?php
 
$con = mysql_connect('database.website.com','username','password');
if (!$con) {
die('Could not connect: ' . mysql_error());
} mysql_select_db('databaseName');
 
?>

Now that we have successfully created our connect.php file we can move on to create a small piece of code we will include in the body of the page we created in the first tutorial. In this file we will be selecting information from our database.

1
2
3
4
5
6
7
8
9
10
11
<?php
 
include('connect.php');
$loader = mysql_query("SELECT * FROM table ORDER BY title") or die(mysql_error());
while($row = mysql_fetch_array($loader)) {
 
echo .$row[title].'<br>'.$row['description'].'<br>'.$row['link'].'<br>'.$row['quality'];
 
}
 
?>

Now we have successfully pulled information from our database, created a connection to the database and loaded the connection dynamically. Hopefully this will have helped you in your journey to create a dynamic website.