5 December 2008 - 15:19[How to] disable the Windows Boot Manager after installing Vista

I recently upgraded my mom’s PC from XP to Vista. As far as I can tell, the Vista installer copies your old XP files into a folder called Windows.old, and you can’t boot into XP once Vista is installed in that partition… but for some reason, every time I’ve installed Windows Vista the computer boots into a “Windows Boot Manager” where you can pick whether to boot into your Vista installation or an “Earlier version of Windows”. This screen comes up even if you got rid of your “earlier version”.

By default, there’s a 30-second countdown before it boots into your default (Vista) installation. Naturally, we want to skip this menu entirely and boot straight into Vista. Here’s how to disable the Windows Boot Manager and boot straight into Vista:

  • Don’t try to change anything in msconfig, it only lets you reduce the countdown to a minimum of 3 seconds.
  • This setting can be changed easily from within Vista, so don’t download any programs, especially those that cost six dollars.
  1. Bring up the “System” information window by pressing Windows Key + Pause/Break (or: Control Panel -> System, or right click My Computer -> Properties).
  2. In the left sidebar, click Advanced system settings to bring up the System Properties window.
  3. In the Advanced tab, under Startup and Recovery, click the Settings… button.
  4. Uncheck the box next to “Time to display list of operating systems:”, and make sure Vista is selected as your Default Operating System.

The next time your system boots, it will boot Vista instantly instead of showing the boot manager.

4 Comments | Tags: how to, software

15 April 2008 - 22:42[How to] round corners on images with JavaScript

Something I’ve come to love about the JQuery JavaScript library is that, no matter what client-side scripting challenges arise these days, my first thought is “I bet there’s an easy way to do this with JQuery!”. I recently worked on a website where the client wanted rounded corners on a lot of the site’s elements, including images. A quick Google search found this jquery.corner plugin; which was, as most JQuery plugins are, extremely simple to implement and adapt for my own project.

The corner plugin has a bunch of ways to add corner effects to div elements, but unfortunately you can’t apply it directly to an image. Back to Google, I found a way to make it work with images: wrap it in a div and apply the corner code to the div element. This is good, but not best for our purposes. We still have to apply styles to make the divs match the size of their child image elements.

Objective:
To loop through all divs of a certain class, and automatically resize them to match their child image elements.

We’ll put this code inside our $(document).ready(function(){ … }) block so it runs as soon as the document is ready to run code.

We want to apply this code to all divs of a certain class, we’ll say class=”round-corners” in this example. To loop through every div of this class and run code on it, we use the wonderful JQuery.each function.

First, select images with a parent div element of this class (“round-corners”):
$(‘div.round-corners > img’)
and run the JQuery.each function to loop through:
$(‘div.round-corners > img’).each(function(id){ … })

Within this loop we want to select the image, and set the height and width of its parent div element. An easy way to do this is with the .height and .width functions. These functions are both accessors and modifiers, as is the case with many functions in JQuery.
Select the parent element of the selected image:
$(this).parent(‘div.round-corners’)
get the height of the image we’ve selected:
$(this).height()
so the whole line to set the height will look like this:
$(this).parent(‘div.round-corners’).height( $(this).height() );
Repeat for the width property with the .width function.

That should do it. Let’s take a look at the source for the whole page:

<script type="text/javascript" src="jquery.js"></script><script type="text/javascript" src="jquery.corner.js"></script><script type="text/javascript">$(document).ready(function(){ $('div.round-corners > img').each(function(id){     $(this).parent('div.round-corners').height( $(this).height() ).width( $(this).width() ); })$('.round-corners').corner();})</script><div class="round-corners"><img src="P3040004.JPG" alt="P3040004.JPG" /></div>

<div style="height: 130px; background-color: rgb(255, 0, 0);" class="round-corners">This is just a regular div with no image inside, but it will have round corners too!</div>

<div class="round-corners"><img src="bluehills.jpg" alt="bluehills.jpg" /></div>

An idea for improving this script would be to use the .wrap function to automatically wrap the images in divs

Have fun rounding corners, and please use them responsibly.

2 Comments | Tags: code, how to, web

24 February 2008 - 22:26[How to] use PHP to make form programming faster and easier

Programming forms is fairly easy, but it can certainly be one of the most tedious things to program. Anyone who’s programmed something like an application form with a decent amount of fields will tell you that it takes forever to make all of the form elements, and add the necessary code to pre-populate with values or pre-check radio buttons etc. (This is a good practice when programming forms, so a user doesn’t have to fill the form out again if it’s submitted with errors.) Whether you’re smart or just lazy, you’ll definitely want to make a simple PHP function to expedite this otherwise-boring process. Enter: the formElement() function.

/** * form elements */   function formElement($type, $name, $value = '',$extra = '')   {       global $_POST;       if ($type=='text') {           $val = (!empty($_POST[$name])) ? $_POST[$name] : $value ;           $o = '<input type="text" name="'.$name.'" value="'.$val.'"'.$extra.' />';       } elseif ($type=='radio') {           $checked = ($_POST[$name]==$value) ? 'checked="checked"' : '';           $o = '<input type="radio" name="'.$name.'" value="'.$value.'" '.$checked.' '.$extra.' />';       } elseif ($type=='checkbox') {           $checked = ($_POST[$name]==$value) ? 'checked="checked"' : '';           $o = '<input type="checkbox" name="'.$name.'" value="'.$value.'" '.$checked.' '.$extra.' />';       }

       return $o;   }

It’s pretty straight-forward, but I’ll break it down parameter-by-parameter.

$type – in my example, this is the same as the type attribute in the html input tag. You could expand the function to include textareas too, or whatever you need.

$name – same as the name attribute.

$value – For checkbox and radio types, it uses this value to check against the item in the $_POST array, and adds checked=”checked” where appropriate. For text types, it pre-populates the textbox with this value if the form hasn’t been submitted yet.

$extra – I use this for any other attributes you may need to add to your form elements. Things such as CSS inline styles, event handlers like onclick, and id or class attributes could go here.

So with our new formElement function, this;

   <input type="radio" name="fav_food" value="pizza" <?=($_POST['fav_food'] == 'pizza') ? 'checked="checked"' : '';?> /> Pizza   <input type="radio" name="fav_food" value="souvlaki" <?=($_POST['fav_food'] == 'souvlaki') ? 'checked="checked"' : '';?> /> Souvlaki

becomes this:

   <?=formElement('radio','fav_food','pizza');?> Pizza   <?=formElement('radio','fav_food','souvlaki');?> Souvlaki

1 Comment | Tags: code, how to, web

2 November 2007 - 23:04[How to] calculate daylight savings dates with PHP

This Sunday is the “fall back” date for daylight savings, which is awesome. In North America, DST time changes are on the first Sunday of November, and the first Sunday of March. On rare occasions, you might find a need for a PHP function that returns the first Sunday of a November, or the third Friday of January, or the X-th Y-day of Z.

First, let’s make a function to calculate the DST “fall back” date. Some quick planning:

  • The function will have a single, optional parameter for the year. (Default will be the current year).
  • We’ll loop through the days of the month until we get to the day we want, and return it as a UNIX timestamp.

Begin by defining our function:

function dstFallBack($year = '')

and handle the $year if none is supplied with a ternary operation:

$year = (empty($year)) ? date("Y") : $year ;

loop through the days until we get to the first Sunday (guaranteed to happen within 7 days). A simple for loop will do.

for ($x = 1; $x <= 7; $x++)

get the UNIX timestamp for the day of our iteration (November $x, $year). To do this we use the mktime function.

$date = mktime(0,0,0,11,$x,$year);

Check if the day is a Sunday using the date function. (I used "D" instead of "N" because "D" will work in older versions of PHP.

if (date("D", $date) == 'Sun')

If it's Sunday, then we're done, and we can return the $date

break; // no need to keep looping.

Here's the whole thing:

/*** Gets the date of the DST November "fallback"* In other words, the first Sunday in November** @param int $year* @return int*/function dstFallBack($year = ''){ $year = (empty($year)) ? date("Y") : $year ;

 for ($x = 1; $x <= 7; $x++)  {        $date = mktime(0,0,0,11,$x,$year);            if (date("D", $date) == 'Sun') {                $sunday = $date;                break;            }        }        return $sunday; }

Run it a few times as a test:

echo date("M jS, Y",dstFallBack()) . "\n";     // for 2007: 4-Nov-2007echo date("M jS, Y",dstFallBack(2009)) . "\n"; // 1-Nov-2009echo date("M jS, Y",dstFallBack(2016)) . "\n"; // 6-Nov-2016

The "spring forward" date is on the 2nd Sunday of a month, but the function is only slightly more complicated. Just for fun, we can put together a more reusable function (recall "X-th Y-day of Z").
To do this, just add a sort of flag as we iterate through days, like $Ydays_encountered, and increment it with each iteration.

/** * Get the Xth Y-Day of Month * "Abstract" as a double-entendre... * * @param int $xth * @param int $yday * @param int $month * @param int $year * @return int */function xthYday($xth, $yday, $month, $year = ''){ $year = (empty($year)) ? date("Y") : $year ;

 //The Xth Y-day will come up within the first X weeks, //so limit the for loop to X * 7 days. $for_limit = ($xth * 7);

 for ($i = 1; $i <= $for_limit; $i++) {  $date = mktime(0,0,0,$month,$i,$year);  if (date("D", $date) == 'Sun') {   $ydays_encountered++;    if ($ydays_encountered == $xth) {     $xthYday = $date;     break;    }  } } return $xthYday;}

echo date("M jS, Y",xthYday(1,'Sun',11,2007)); // 4-Nov-2007. Seems to work!

Hopefully this will be useful for some kind of events calendar or something.

No Comments | Tags: code, how to

17 October 2007 - 18:54[How to] force "www." on your website

I recently added some lines to my website’s .htaccess that force a “www.” on the front of my domain. If you try to go to my website by typing just dmack.ca, you’ll notice it automatically redirects you to www.dmack.ca.

It’s best practice to either always force the www. or always force no www., but not use both. There are a bunch of reasons to do this, and they vary depending on what kind of website you run:

  • Cookies. If visitors can access your site with and without the “www.” (two different URLS as far as the cookies are concerned), it could cause problems.
  • Likewise with $_SERVER['REQUEST_URI'] and such things if you’re running scripts.
  • The most cited reason I see for this is that it hurts your pagerank. Apparently you could get penalized for duplicate content.

In the interest of avoiding the above problems, and any sort of minor confusion, here’s how to set up a painless automatic redirect in your .htaccess file.
I’ll use my own domain in this example. Just add the following lines:

# redirect dmack.ca to www.dmack.caRewriteCond %{HTTP_HOST} ^dmack.ca [NC]RewriteRule ^(.*)$ http://www.dmack.ca/$1 [L,R=301]

The first line is a rewrite conditional directive that basically ensures the URL starts with “dmack.ca”. The [NC] means it’s not case sensitive. The second line is an ordinary rewrite directive that matches everything and does a 301 redirect to the properly formatted URL (complete with “http://www.”). This matches the url if it begins with dmack.ca, but it could just as easily match any url that isn’t www.dmack.ca (think: exclamation mark negates… !^www.dmack.ca)

1 Comment | Tags: how to, web

22 July 2007 - 3:54[How to] create a simple web template using PHP’s output buffer

The PHP output buffer is exactly what it sounds like: it allows us to capture output and, instead of printing it to the screen immediately, set it as a variable and deal with it later on. The “output” could include echoed lines and ordinary markup outside of php tags. For example: if you include a file, you can set all the variables and run all the functions inside it, but save the output and display it in different parts of your template, like the title, meta tags, content etc. In this tutorial, I’ll explain how to use the PHP output buffer to implement very simple template system for your website.

This guide assumes you’ve already got a nice-looking template with something like an empty div for your page content. I’m using /index.php as my template file, and i’ll pass it the same of the page to display as a GET variable.

The template index.php file should look a bit like this:

[/index.php]

<?php//get the page to display, or use home.php if nothing is passed.$page = (!empty($_REQUEST['page'])) ? $_REQUEST['page'] . '.php' : 'home.php' ; //turn on output bufferingob_start(); //get the contents of the file (/pages/home.php etc.).//do this outside of php tags because the file most likely has its own php tags, as well as ordinary html in it.eval('?> ' . file_get_contents($_SERVER['DOCUMENT_ROOT'] .'/pages/'.$page) . '<?php '); //return the contents of the output buffer, and set it as a variable.$page_content = ob_get_contents(); //turn off output buffering, and clear the output buffer so we can do it again just as easily.ob_end_clean(); ?><!DOCTYPE [...]<title><?=$page_title;?></title><meta name="description" content="<?=$meta_description;?>" /><meta name="keywords" content="<?=$meta_keywords;?>" />[...]<div id="page-content"> <?=$page_content;?></div>[...]

The variables $page_title , $meta_description , and $meta_keywords are defined in the page content file ‘/pages/whatever.php’.

A page content file would look like this:

[/pages/home.php]

<?php//define meta and title tags$page_title = 'This is the page title!';$meta_description = 'This is the meta description';$meta_keywords = 'meta keywords, hot dogs, rap music';?><h1>Welcome</h1><p>This is a test of my simple template system.</p><p>I sure hope it works!</p><p><?phpecho 'I can echo things this way too...';?></p>

Then we’d access this page with the uri /index.php?page=home and the script would start the output buffer, then get the contents of home.php and set the variables, but not echo anything until you tell it to. You can do this as many times as you want, if you wanted to include a menu from an external file as well, for example.

2 Comments | Tags: code, how to, web

11 June 2007 - 23:01[How to] host multiple offline websites with Apache

As an extension to my earlier tutorial, [How to] create an offline WAMP demo kiosk in Vista, I thought it would be worthwhile to discuss how to host more than one offline demonstration site on the same machine. These instructions will work on a Linux installation of Apache as well as our Windows installation from Apache2Triad.

This is useful if the extra sites you’re hosting have web-root-relative links (with leading slashes like "/files/document.html") that would otherwise point to http://localhost/files/document.html instead of http://localhost/site3/files/document.html, for example.

In this example, We’ll pretend you already have a site set up on localhost in your webroot. The easiest way to add new sites is with virtualhosts. To set up virtual hosts, we just need to add a bit to our httpd.conf file. Locate httpd.conf in your (apache2triad directory)/conf/ directory, and find the section for Virtualhosts. It should have a sample Virtualhost entry commented out for you already, but you need even less than that.

In your httpd.conf, add these lines:

NameVirtualHost *

<VirtualHost *>DocumentRoot C:/apache2triad/htdocsServerName localhost</Virtualhost>

<VirtualHost *>DocumentRoot C:/apache2triad/htdocs/site2/ServerName site2</Virtualhost>

<VirtualHost *>DocumentRoot C:/apache2triad/htdocs/site3/ServerName site3</Virtualhost>

…and add more blocks like this for any additional sites. Windows users, note the direction of the slashes! Don’t forget that you need to restart Apache any time you make a change to one of your conf files.

All you need to do now is add aliases to your hosts file for each virtual host you added.

In Windows, the hosts file is found in C:\Windows\system32\drivers\etc
Locate the line for 127.0.0.1 and add your “ServerName” aliases like this:

127.0.0.1    localhost    site2    site3

You can now access your sites by typing their ServerName aliases in your address bar. "site2" would take you to site2, for example, and all your root-relative links would work.

2 Comments | Tags: how to, internet, software, tech

8 May 2007 - 19:43[How to] create an offline WAMP demo kiosk in Vista

Today I was asked to put together a fully-roamable version of a demo website on a laptop with Vista basic. This way, you can demo your site with a working db to a client while you’re out on a yacht with no wifi, for example. It’s fairly simple, but there are a few stupid things you need to do to get it working in Vista.

Objective: To create an instance of an existing website with Apache and MySQL that runs completely off localhost, so demonstrations can be conducted without a network connection.

For this project, I chose to use the very-awesome Apache2Triad. Apache2Triad takes care of PHP and MySQL with remarkable ease in XP, but we’re not using XP today.

Download and install the “Edge” release, 1.5.4 from http://sourceforge.net/project/showfiles.php?group_id=93507 . This will take care of MySQL, but the Apache services won’t start properly in Vista. To fix that, you’ll have to install Apache 2.2.4 over top.
Get the MSI installer of Apache 2.2.4 from http://httpd.apache.org/download.cgi , and do a ‘custom’ install into the same path as your Apache2Triad installation. UAC will ask you for allowance, and a blank message box with nothing but an OK button came up (a cmd window came up to tell me that the Apache service was already installed, and that I could press esc to quit, but I just ignored it). Click to allow the UAC, and OK on the mystery window. When prompted, put in localhost for the host and server names. You can leave the email address blank if you want. The latter installation will add a shortcut to the Startup folder in your start menu, you can remove that.
After a reboot, Apache and MySQL should both start automatically.

Try navigating to localhost in your browser. The second time you installed Apache should have a message telling you that “It works!”. Navigate to your site root, (C:\apache2triad\htdocs by default) and paste in a copy of your demo site.

Check out http://localhost/phpmyadmin , and use the Import feature to recreate your database(s) from SQL dump files. (Oh, by the way, go make SQL dump files out of your database with Phpmyadmin’s “Export” if you haven’t already.) Click the Privileges tab in Phpmyadmin and create the accounts you need.

Navigate to http://localhost/ again and try out your site. If all went well, you should now have a working copy of your website that you can take with you and demo for clients anywhere.

1 Comment | Tags: how to, internet, software, tech