Archive for 'PHP'

5 Questions Web Developers Need To Ask Themselves

It’s all very well for developers to create visionary frameworks and applications that only they understand - that is the freedom that makes web development great - until someone on the outside tries to work with it!

As far as i’m concerned, at the very least, the view/template of a web application needs to be changeable even to a web designer who has zero to minimal experience working with dynamic websites. Why - when we can create “beautiful” applications with minimal code repetition - because you are breaking the brilliance of code seperation. Its no different to seperating style and content. Lets face - usually, designers are good at design, and programmers are good at programming. Therefore the two skills need to co-exist, happily.

5 Questions to ask yourself:

1. How long would it take a pure web designer (HTML + CSS only) to change any element on any page?

2. How long would it take an average programmer to make some basic functionality changes, such as adding a new database table and running some pre-existing sanitation functions?

3. I’m looking at the website index. If I download index.php (or whatever) how many breadcrumbs do I have to follow to change, for example, the sidebar?

4. Is the folder structure sensible, and have the old files and folders been deleted?

5. Am I coding in a style that only I fully understand, on a commercial project that may rightfully be worked on by other developers that I may never speak to?

Posted on 6 November '08 by Steve, under PHP, Web Development. No Comments.

How to duplicate a phpBB3 theme

If you ever want to duplicate an existing phpBB3 theme, you might be sorely frustrated when you can’t seem to get any of your changes to take hold. It is sometimes necessary to go to the ACP, and hit purge the cache, however there is a further trick.

First, duplicate the theme folder you want to start with, and rename to your theme name.

Second, you need to go to the theme folder, and open style.cfg, /imageset/imageset.cfg, /template/template.cfg, /theme/theme.cfg, in a text editor. All of these files contain name = value. Rename all of these to your theme name.

Upload all of these changes, then head to the ACP and go to the “styles” tab. Click install on your theme.  On the left hand menu, go to each of the style components, and select install for each one. Then head back to the main styles page and click “details” for your theme. Select the imageset, template and theme from the dropdown menu.

Now you should be done!

Posted on 2 July '08 by Steve, under PHP. No Comments.

How to make a tag cloud in PHP, MySQL and CSS

Tag CloudToday I wanted to make a tag cloud, but all my searching proved fruitless - so I decided to stop being lazy and just work it out myself!

For my example, first you'll need a database that stores the keywords / search terms.

The way mine works is every time a search is run, I check to see if the term has been searched before. If it has, I update the counter field + 1. If the term has not been searched for before, I insert it with a counter of 1.

Alternatively you may store every search individually and use the GROUP BY clause to determine how many times the search has been run.

My query looks like this:

PHP:
// don't forget to connect to DB first!

$terms = array(); // create empty array
$maximum = 0; // $maximum is the highest counter for a search term

$query = mysql_query("SELECT term, counter FROM search ORDER BY counter DESC LIMIT 30");

while ($row = mysql_fetch_array($query))
{
    $term = $row['term'];
    $counter = $row['counter'];
   
    // update $maximum if this term is more popular than the previous terms
    if ($counter> $maximum) $maximum = $counter;
   
    $terms[] = array('term' => $term, 'counter' => $counter);

}

// shuffle terms unless you want to retain the order of highest to lowest
shuffle($terms);

Next we'll setup a bit of css. Feel free to adjust these as you see fit.

CSS:
#tagcloud {
    width: 300px;
    background:#FFFFCC;
    color:#0066FF;
    padding: 10px;
    border: 1px solid #FFE7B6;
    text-align:center;
}

#tagcloud a:link, #tagcloud a:visited {
    text-decoration:none;
}

#tagcloud a:hover, #tagcloud a:active {
    text-decoration: underline;
    color: #000;
}

#tagcloud span {
    padding: 4px;
}

.smallest {
    font-size: x-small;
}

.small {
    font-size: small;
}

.medium {
    font-size:medium;
}

.large {
    font-size:large;
}

.largest {
    font-size:larger;
}

Finally we just want to loop through the array and display it with the appropriate css class.

PHP:
// start the output to the page
echo "<h3>Popular Searches</h3>
<div id=\"tagcloud\">
<div>\n"
;

foreach ($terms as $k) // start looping through the tags
{
    // determine the popularity of this term as a percentage
    $percent = floor(($k['counter'] / $maximum) * 100);
   
    // determine the class for this term based on the percentage
    if ($percent <20)
    {
        $class = 'smallest';
    } elseif ($percent>= 20 and $percent <40) {
        $class = 'small';
    } elseif ($percent>= 40 and $percent <60) {
        $class = 'medium';
    } elseif ($percent>= 60 and $percent <80) {
        $class = 'large';
    } else {
        $class = 'largest';
    }
   
    // output this term
    echo "<span class=\"$class\"><a href=\"search.php?search=" . urlencode($k['term']) . "\">" . $k['term'] . "</a></span>\n ";
}

// close the output
echo "</div>
</div>\n"
;

And thats it! Hope this saves someone some time. Add your comments below!

Posted on 22 August '07 by Steve, under PHP. 38 Comments.

What is a Cron Job?

Q: What is a cron job?

A: A cron job is an automated task that runs on a linux server, similar to Scheduled Tasks on windows.

The main idea is that you can run a process without you or someone else manually setting it off - my first experience with cron jobs was when working with an online auction website. As you can imagine, an auction runs for a fixed time period. So lets say an auction runs for 7 days. What happens at the end of 7 days? The database says this auction is closed. If the auction did not sell, whether the reserve was not met, or their were no bids at all, then we want to automatically relist that auction.

There are two options:

a) run a process when a page load occurs... sucks because the page load is gunna take longer due to the extra processes occurring

b) run a cron job every x minutes, looking for auctions that have passed their end time, and either relist them or close them down permanently

I tend to code in PHP and MySQL, which means that I create a PHP file that does all the maintenance tasks required and I run it at set intervals. The cool thing is, if you want to be running different tasks at multiple times of the day/week/year, just setup multiple cron jobs.

Thats great, but how the hell do I setup a cron job?

I'm glad you asked!

If you are comfortable working with command line, then you might want to visit a site like this. If, like me, you have access to cPanel, life is much, much easier.

How to setup a cronjob in cPanel

1. login to cPanel

2. click on the cron jobs tab (near the bottom)

3. Select your time you want the script to run. The options are pretty straightforward. If you select Minutes > 30 that means the script will run at xx:30am/pm. You can also hold the Ctrl key to select multiple minutes/hours etc.

4. Enter the "command to run". For my purposes, I generally want to load a php file. It doesn't hurt to put these files outside of the public web folder "just in case". Something like this should do the trick on a standard cPanel/WHM server:
/usr/local/bin/php -f /home/myusername/scripts/ascript.php

The first part is the "path to php". You might want to ask your hosting providers what this is, if the above doesn't work. The second part is the absolute path to your file. By default on a cPanel server the structure is /home/your_account_username/ where you can modify everything inside your account folder. Generally contained in the account username folder is a www/public_html folder with your live web files.

Hope this is of some help - cron jobs are a breeze - once they are running as desired its fun dreaming up new ways to make them more and more sophisticated... after all, now the server is actively taking away the daily grind!

If you have any questions do not hesitate to ask below.

Posted on 15 August '07 by Steve, under PHP. No Comments.

MySQL Reserved Words

The database language MySQL has a number of reserved words that should not be used in queries or for field names. Some of the big ones are:

  • Add
  • All
  • Between
  • Both
  • Check
  • Current_Date
  • Drop
  • For
  • From
  • Group
  • INT
  • Key
  • Load
  • Lock
  • Null
  • On
  • Out
  • Real
  • Repeat
  • Table
  • Use
  • Write

And so it goes. I came across the official list when I couldn't work out why this query wouldn't work:

SQL:
SELECT COUNT(id) FROM messages WHERE READ = ‘0AND TO = $logged_in_id

The reason? Both "to" and "read" are reserved words and the query failed. See the full list here:

http://dev.mysql.com/doc/refman/5.0/en/reserved-words.html

Posted on 25 June '07 by Steve, under PHP. 1 Comment.

PHP and why you should use it

PHP is a programming language that is arguably one of the easiest to learn, cost efficient and common languages out there. It is used primarily to create dynamic website content. By dynamic I mean that it flows and it can change in a way that static website content can not.

With so much time invested by individuals and businesses in establishing themselves with a programming language, bias is inevitable, and you don't have to dig deep to find someone criticising PHP. But then, the same goes for any language. Each has their own advantages and disadvantages.

What remains a fact is that PHP has been popular in the past, is popular now, and will continue to be popular well into the future.

Some of PHP's advantages include:

  • Considered easy to learn as far as programming languages go, meaning it is very well known in the web development community
  • Hosting is more common and cheaper than for any other language
  • Large range of turnkey php applications available, some free, some not. Examples include WordPress, (the software this forum is running on), phpBB and wikipedia.
  • Abundance of free online resources

Here is a classic usage for PHP;
Situation - A shop owner wishes to supplement their traditional shop with an online shop, in the form of a 500 product website.
Solution A: Build a static website, and make 500 separate pages to accommodate each and every product.
Solution B: Build a dynamic website, create 1 product page and create 500 database entries

So what is the difference? In any case, the shop owner / web developer is going to have to process 500 products. Sure this sucks, at least initially.

PHP starts flexing its muscle when we get to the ongoing management side of things. The shopowner has decided that the product pages would convert better if the pictures were at the top of the page, instead of at the bottom;

Static Site: guess what - 500 product pages = 500 pages to download, change and upload - say goodbye to your trip to the beach this weekend!
Dynamic Site: 1 product template = 1 file to change, 500 'pages' updated in one foul swoop.

The reason for this miracle of time saving is because PHP, in combination with a database language such as MySQL can join forces to create what you seen in your web browser on the fly. As a page is loaded, PHP instructs the database to return the relevant information for the product in question.

This is but one hypothetical situation - but instantly you can see the scalability of PHP. This is really only the tip of the iceberg.

One of the cool things about PHP as one delves into the "web development" field, is that as you look for more and more advanced concepts every day, the inbuilt functionality is sure not to disappoint. I'm yet to hit a point where I think, "damn I wish I could do that in PHP!".

Perhaps years down the road one might hit this roadblock, but as long as the team behind PHP keep there eyes on the ball, and the community remains strong and innovative, that day will*never* come. The point here is that who knows where the internet will be in 10, 20 or 50 years time.

Further Reading:
Comparison of Programming Languages
PHP
PHP Usage Graph
Programming Movers and shakers

Posted on 2 February '07 by Steve, under PHP. No Comments.

PHP Server Side Includes

One of php's most practical solutions is a server side include.

Consider the navigation at the top of every page on this website. At time of writing there is 7 unique pages on thomasmultimedia.com.au, excluding this blog. If I were to add a new page to my site, would I not have to update each and every page to show the new link?

What a horrible sounding job -- what if I had to make 2 or 3 changes to each and every one of those pages? In the context of 7 pages this might take say 10-15 minutes to open, change, save and upload 7 files. But what about if I have 20, 50, or 1000 different pages? This could take a day, if not a week!

What a horrible way to spend a week... and yet i'm sure someone, somewhere, in a deep, dark dungeon has slaved away at this menial task for hours on end.

All bow to King server side include!

A Server Side Include is a fairly ordinary file that is created for a website. It can contain any kind of web friendly content, but the most common usage is to display the same things over and over on different pages. So utilising this fantastic technology I have 1 file that contains my main navigation. If I want to rename, add, delete... or make any change under the sun, a server side include is my friend. I change the file once, and like magic every instance of the file is now reflecting the changes. Cool huh?

For any web people who have just read this post, don't worry, I won't leave you in the dark about how to do it - my favourite method is php.

PHP:
include('path/to/file.php');

For Dreamweaver users, use brackets - the above will leave a lovely/nasty little php symbol instead of the visual contents of the file. Try this to have the file contents display perfectly in your design view:

PHP:
include('path/to/file.php');

Remember that the path can be relative or absolute. If you work with folders for example, you may need something like this if the current file is located in its own folder.

PHP:
include('../includes/navigation.php');

You can also use the absolute path, which is commonly required in configuration files of popular applications. You will need to know your absolute server path, which frequently looks something like this:

PHP:
include ('home/username/public_html/includes/navigation.php');

Need help setting up server side includes or developing your site in some way? Read about Thomas Multimedia's web design services 

Posted on 10 December '06 by Steve, under PHP. 3 Comments.