Pseudo Random Goodyness - (27th April 2009)

Tonight I was writing a function to randomly grab a row from the database for a website i'm working on. I was reading the MySQL documentation and it seems as though they have a rand() function which you can run, although the documentation available here http://dev.mysql.com/doc/refman/5.0/en/mathematical-functions.html did say "Since using MySQL's RAND() function on a large rowset is notoriously slow: ...". So i decided to mock up my own pseudo random database grabber, i wrote it in less than 10 lines and here goes.

In my controller I have the following



    function index()
    {
        $number_of_questions = $this->Question_model->getTotalQuestions();
        $random = rand(0, $number_of_questions - 1);

        redirect($this->Question_model->getNextQuestionID($random));

    }

And here are my functions I've written for getTotalQuestions() and getNextQuestionID().



    function getTotalQuestions()
    {
        return $this->db->count_all_results('hl_questions');
    }

    function getNextQuestionID($id)
    {
        $this->db->select('question_id');
        return $result = $this->db->get('hl_questions', '1', $id)->row()->question_id;

    }


As you can see, i'm only returning question_id, although you could easily alter my code to return the entire row. This function is very efficient, I ran the profiler to check what SQL was being used and here it is:

for getTotalQuestions()


 
SELECT COUNT(*) AS `numrows`
FROM (`hl_questions`) 

and for getNextQuestionID()



SELECT `question_id`
FROM (`hl_questions`)
LIMIT 2, 1



Obviously the 2 here will change depending on what random number is pulled out. As you can see from the SQL it's going to be quick and efficent. Were doing a count of all the rows first (which is quick as i'm using the default storage engine on MySQL {MyISAM}).

Secondly were just selecting one row and using LIMIT to pull out what row we want.

Posted by Mario @ 12:24am
View Comments


Picking the best fonts for your website. - (9th April 2009)

The other day I was trying to pick some good fonts to use on my new project. The project is a piece of web software that myself and others will use to manage projects/tasks and monitor how long they spend on each activity. Being a serious piece of software i'm trying to make it look as modern and as visually attractive as possible (so it might actually be fun to use). The software has big fonts and large text fields to achieve this, although I realized I would need to pick a font which continues this as well.

After some browsing around the web I found this link http://www.ampsoft.net/webdesign-l/WindowsMacFonts.html, which is really useful. It gives you the Windows font name the Mac font name and the family, so if you specify all 3 fonts in your CSS it's going to use the 2nd or 3rd if the 1st isn't' found.

For my project I chose to use the "Trebuchet, Helvetica, sans-serif" family of fonts, i think it works quite nicely :)

Posted by Mario @ 12:31am
View Comments


Apache Tweaking & CodeIgniter Performance Testing - (24th March 2009)

The last few months I've been focused on improving the performance of my web server (after a friend showed me that his server was serving requests much quicker). I've done a few things that don't take too long which have dramatically improved the performance of my websites. The first thing I did was to tweak the apache config file, apache2.conf to suit my web server. My server only has 256MB ram so having a high amount of max requests isn't reasonable, so I had to fiddle around with this for a while.

After reading some forums and testing/benchmarking a fair bit i found some settings which don't use too much memory but allow for good performance. Here's a thread that helped me out a fair bit http://forum.slicehost.com/comments.php?DiscussionID=1082 and here's my apache2.conf:



<IfModule mpm_prefork_module>
        StartServers 4
        MinSpareServers 4
        MaxSpareServers 4
        ServerLimit 50
        MaxClients 50
        MaxRequestsPerChild 1000
</IfModule>

As you can see 4 servers and a max of 50 clients seems the go for me. Every server is different so just because these settings work well for me, they might not work the best for you. I also managed to enable Gzip compression on apache using the built in module mod_deflate. This adds load the CPU (as it has to constantly compress files to be served) but speeds things as the client receives files quicker. You can find a good guide here --> http://betterexplained.com/articles/how-to-optimize-your-site-with-gzip-compression/.

Once the tweaking was done I decided to run some quick tests. I served 3 seperate pages, the first simply with "Hello World!" displayed on the page without any formatting. The second page the same but having PHP echo the output. The third was using CodeIngiter to display the message, here are the results below.

HTML: Requests per second: 2550.88
PHP: Requests per second: 1689.09
CodeIgniter: Requests per second: 356.11

As you can see HTML is about 50% quicker than using PHP and CodeIgniter is about 4-5 times slower than using base PHP. The big performance difference between using raw PHP and CodeIgniter starts to become less noticable when applications grow in size. For example I have two pages that work on a news article system I've written myself. One is raw PHP and the other based on the CodeIgniter framework. The raw PHP version is only 1.5times quicker than the CI Framework version. This is a small loss when you think of the convenience, features and security the framework brings.

Posted by Mario @ 7:56pm
View Comments


Whiteboards are awesome! - (26th February 2009)

So I've got some projects I'm working on at the moment, some for myself and some for my clients. One of the projects I was working on was an open source chat application, using PHP and JavaScript with AJAX techniques as well. I managed to finish basic parts of the chat, user registration/authentication and chatting/changing nicknames etc... so that all works fine. Although through my development I realized i'm Very unorganized.

In an attempt to become more organize I decided I would write my own project management software (which i'm working on at the moment and it's going quite well). The goal of the software is to help me be more organized which should as a result speed up my application development and make sure that I don't miss anything along the way.

To make sure I was organized while writing the project management software, I did the only sane thing and bought a white board and let me tell you White boards are awesome!. I picked mine up for $25 AUS from here http://www.officeworks.com.au/ and it's awesome, about 90cm wide by 60cm high.

The great thing about a whiteboard is that it's a great reference point when doing your coding, I jotted down the database structure i would need as well as pages, views etc... then whenever I got lost i would just have a quick look to see what needed to be done.

Posted by Mario @ 3:33pm
View Comments


Search engine optimization - (9th February 2009)

Ok so it's 2:40am and i'm writing this awesome article about search engine optimization. Two weeks ago I created an online portfolio which you can view here http://www.mariovisic.com/. After nearly a week my website wasn't indexed by any major search engines, even though I had submitted my URL to Google.

What I did next was whip out my old Google Webmaster tools account and add my new domain. After a few minutes I submitted a sitemap, a few days later I was indexed by Google. Also i'm the first hit if you google my name http://www.google.com.au/search?q=mario+visic oooh yeah.

This got me thinking, what can I do to improve the rankings for rampage-online. If you Google rampage-online there's about 6 million hits so i'm not listed in the first few pages which isn't great. Then I had a look over a few things i'd done very early on and spotted some errors. I've listed a few improvements below which will help any site in its rankings.

1) Don't use the same keywords/description/title on all your pages
The description, title and keywords of each page should reflect the content of the page as specifically as possible.

2) Use words in the URL instead of numbers
Previously you could access articles on rampage-online by going to RO.net/articles/30 or some other article ID. I've recently change this to RO.net/articles/name-of-article. This helps search results A LOT, as search terms that appear in your URL will give you page a higher ranking.

3) mod_rewrite as much as you can!
Neat URL's are awesome. How much nicer is RO.net/articles/-article-name rather than RO.net/index.php?page=articles&id=5. Search engines seem to like this more as well.

I've just now submitted a new sitemap to google and we'll see how we go :)

Posted by Mario @ 2:55am
View Comments


New AJAX Chat - (1st February 2009)

After some fiddling around tonight I managed to finish the installer I was working on which I'll be using for the chat i'm releasing soon. I wasn't able to do too much bug testing but it seems to work fairly well at the moment.

Keep your eyes on the software section as I should be making my first release soon. The chat will be based on the version that's currently setup on this website, although it will be cut and chopped to make it portable and easy to use (comes with a GUI installer).

Posted by Mario @ 10:25pm
View Comments


My Dream Job - (31st January 2009)

I was watching some video's the other day while my development web server was having hardware issues. Yes it was a very unproductive day. I noticed that College Humor which is one of my favourite humor websites are looking for a PHP Developer. This would be my absolute dream job, working on a website like that. The link is here http://www.collegehumor.com/jobs#job_15.

I don't think I'll apply as it's based in New York and I can't work full time at the moment due to my studies. I'm still looking for part time or casual employment which is why i've created an online portfolio to showcase my work http://www.mariovisic.com/.

I'm still looking for projects to take on to expand my portfolio so please get in contact with me if you would like a website or web application created.

Posted by Mario @ 1:47pm
View Comments


Make me a chat and make it good! - (9th January 2009)

After a bit of fiddling around and lots of testing I've managed to finish the chat application I have been working on and it's currently up and running on the website.

The chat is built on PHP and MySQL for the backend. It also uses AJAX and JSON to dynamically update the content.

The chat itself is fairly responsive and includes a userlist so you can see who's online. You can view the chat here http://www.rampage-online.net/chat although you will to reigster and login before it will be available.

In the future I may be releasing the chat as a full package with installer, although there's a few things I must do before this can be done.

If you want to let me know how much you like the chat, or even how much you don't like the chat, please let me know using the Contact section here http://www.rampage-online.net/contact.

Posted by Mario @ 5:41pm
View Comments


CodeIgniter v1.70... I Like it! - (21st November 2008)

So i had just finished an exam today and decided to take a break from study for a few hours. I was working on a custom made forum that I'm currently building (It's still very early in development). I was dealing with some forms that needed to be validated and I noticed that on the CI User guide the form validation class had been upgraded in the new update of CodeIgniter.

You can see it in action here --> http://codeigniter.com/user_guide/libraries/form_validation.html. It's very nice and requires less code than the old validation class.

Posted by Mario @ 10:17am
View Comments


CodeIgniter on IIS - (22nd October 2008)

I ran into some issues today trying to get my previously working (on apache) CodeIgniter setup to work on my IIS web hosting. The problem I was having is that there wasn't any errors shown, just a blank page with no errors at all.

I quickly found that my website worked fine if I placed it in a folder, although not wanting to have ugly URL's i pointed my hosting to go to that folder through CPanel.

Now that problem was solved I wanted to hide the index.php part of the URL to make the URL's look nice and neat. There is a guide to do this on apache using mod_rewrite here http://codeigniter.com/wiki/mod_rewrite/ , although I only have access to ISAPI_rewrite on IIS.

After googling a bit i found some instructions to use ISAPI_rewrite to make clean URL's. The first step is to edit the httpd.ini file and put in the following info



[ISAPI_Rewrite]
RewriteRule /(?:css|img)/(.*) $0 [I,L]
RewriteRule /robots\.txt $0 [I,L]
RewriteRule /(.*) /index.php\?/$1 [I,L] 

If you look at the code above you'll see that the second line has css and img in the brackets, this is because I use css and img folders for my website, you will need to change this to show whatever folders you use, you can have as many as you like, just seperate them with a | for example, if you had folders, "fol1" "fol2" and "fol3" your line would show this:


RewriteRule /(?:fol1|fol2|fol3)/(.*) $0 [I,L]

Once this is done you will need to change your config.php file located in "system/application/config". The line with the index page should read


$config['index_page'] = "";



After that, you should be able to access pages on your website using clean URL's like http://www.rampage-online.net/articles

Good luck :P

Posted by Mario @ 2:01am
View Comments


Welcome - (12th September 2008)

Hello everyone and welcome to rampage-online. The first thing you should know is this website isn't complete as just yet, the software and images sections aren't done yet, although everything else should be working fine. I'm still looking to make a new banner as I put together the current one in less than 2 minutes, very rushed job.

Over the next few weeks i'll be adding some example PHP and Mysql articles and tutorials. I'll also be adding open source PHP software on the software section above. I'm planning on making an interactive chat program and maybe a photo gallery + simple poll.

I've made some cool code boxes like this -->



<?php

 
echo 'php code goes here!';

?>




To house the code :P

So I can make nice lil code snippets :)

Enjoy

Posted by Mario @ 7:30pm
View Comments