Monday, February 28, 2011


A look at the groceries I bought today.

Sunday, February 20, 2011

Kopa teaches PHP: URL routing done simple

URL routing is to have an url called something like "" instead of "", using somewhat same principle as the $_GET variables to include pages. It can be done pretty complicated, but in the end it's just a matter of dividing the url into a number of variables that you use to include pages ect.

To do URL routing modrewrite needs to be turned on in your PHP installation.

First off you need to add some stuff to your .htaccess file.
RewriteEngine on
RewriteRule ^ajax - [L]
RewriteRule !\.(js|ico|txt|gif|jpg|png|css)$ index.php
 First line turns the rewriting on. Second line makes an exception that the folder ajax will not be used for rewriting. I'm doing that as I have an actual folder called ajax with php files in it, that I otherwise will not be able to call with ajax functions as it would redirect them to index.php. Third lines redirects any files except the file types mentioned to your index.php file. So that means whatever link you make on your site it will redirect to index.php.

When it does that you can in your index.php file make an include that will include another .php according to the url.

So in your index.php or some route.php that is included in index.php you make the following code:
$already_there = str_replace('index.php', '', $_SERVER['SCRIPT_NAME']);
$slightly = str_replace($_SERVER['SCRIPT_NAME'], "", $_SERVER['REQUEST_URI']);
$uri = str_replace($already_there, '', $slightly);
This part does some manipulation with the url and gives a string with anything after your domain name, the part that will decide which page to include.

if($uri != ""){
    $pieces = array_filter(explode("/", $uri));
    if(count($pieces) == 3){
        $s = $pieces[0];
        $p = $pieces[1];
        $t = $pieces[2];
    }elseif(count($pieces) == 2){
        $s = $pieces[0];
        $p = $pieces[1];
    }elseif(count($pieces) == 1){
        $s = $pieces[0];
This parts sets up to three variables that you can use to include pages or do other stuff with, like you'd use $_GET values. First line checks if the string is empty, if it is we don't set any variables ofcourse. Second line divides the url into an array, dividing by /'s, as well as removes any empty values from the array, which can happen if the url ends with an /. You can also use any other symbol like a dot or whatever if you like. You could stop now and use the variables $pieces[0] ect. in your scripts. I don't want to have them in an array so I do some more. The next if else part makes sure we don't set any values if we have nothing to put into them. It simples makes the variables $s for section, $p for page and $t for tab in that order if they exists, which I in turn use to include other pages.

For example on index.php I have this code:
$sections = array("records", "news", "info", "contest", "download", "community", "tools");
      if(in_array($s, $sections)){
              include "$s.php";
First line is the different sections I have on my site, which I use to make sure it doesn't include a page that doesn't exist. Next it checks if $s exists in this array I made and then include a php file with that name.

And that's it.

There is just one more thing to remember. When you are on the url "" for example it will still think you're inside a folder. So if you have any pictures or css files ect. you need to set an absolute link to these. Like:
<img src="/images/logo.png">
Instead of:
<img src="images/logo.png"> 

Friday, February 18, 2011

The wonder that is jQuery

After someone asking wether I had any plans for use some jQuery stuff on EOL site, I got curious and checked it out. This resulting in me using it for some features on NAVOK I've had planed for a long time. Some rather simple showing and hiding of something, but with jQuery it was quite a lot simpler than it usually would be. I can only recommend it for your future javascript and ajax needs. I have not yet tried it out for ajax but it seems as straight forward.