Categories
CSS Web Development

Avoid 'page jump' by forcing a scroll bar

Occasionally a web page jumps horizontally because a switch of page or change of content causes a vertical scroll bar to appear.

This can be avoided with a little CSS trick;

1
2
3
  html {
    overflow-y: scroll;
  }

This can be placed in the <head> element of a page that is prone to the problem, or in the header include file for a site.

The result of this is that when a vertical scroll bar is not needed on a site, its place is taken by an unobtrusive pseudo-scroll bar, greyed out like other inactive controls.

Categories
Web Development

Web Development Links

This is a collection of useful links for web development.  Not my content, but useful nonetheless!

Categories
PHP Web Development

Formatting dates in PHP

Formatting dates in PHP can seem a little daunting.  The best way to start is to ensure that you've stored all your dates with the slightly counter-intuitive time() function. This stores the date and time together in your database as one long number that really doesn't mean anything. This is a Unix timestamp.
The date() function is then used to reformat it. For instance 1206371089 comes out to March 24th, 2008 at 15:04:49 (GMT).

If your date/time is stored in the variable $lastupdated, you can display it in a friendlier format as;
[sourcecode language='php']
echo date("Y-m-d H:i:s",$lastupdated);
[/sourcecode]

which will give you a date display like 2008-03-24 13:04:08 (note that this format is well suited to sorting chronologically in the output if it needs to be sorted after processing.)

If you need to refomat a date stored as a string, this is the way to do it;
[sourcecode language='php']
echo date("l jS F Y",strtotime("2008-03-05"));
[/sourcecode]

You can format the dates pretty much however you want.  For a list of the format codes, it's best to go straight to the source at http://www.php.net/date.

Categories
Web Development

Software Tools for Web Developers

Here's a list of invaluable tools for web developers.

  • Notepad++
    • This open-source text editor is free to use. Very powerful, with syntax colouring.
  • grepWin
    • A try-before-you-buy global search and replace tool.
Categories
PHP Web Development

Alternate row background colouring in PHP (with only one line of code)

Alternate row background colouring in PHP is something I've seen and used often, but I was fooling with it on one of my projects today and realized it could be done with one line of code.

First, notice that I've got a while loop to cycle through the records in a dataset I've pulled from a database. You'll also see in the first and last rows that I've created the table outside the WHILE loop because the table is only created once. A row is created once for every row in the dataset so it's inside the FOR loop.

To set the background row I use a ternary operator which tests a condition, then assigns one of two values based on the evaluation of the condition. If you aren't familiar with the Ternary Operator, get to know it. It will dramatically simplifyyour code. Here's a basic example; 

1
$variable = (colour = "green") ? "The colour is green." : "The colour is not green." ;

So looking at the green line, basically what it says is this;

If the variable $bgcolour is not set, or if it is "$F0F7FF", set the value of $bgcolour to "#FFFFFF".

Otherwise, set the value to #F0F7FF". 

Note here that the condition has two parts separated by the OR operator, "||".  The first condition !isset($bgcolour) checks to see if the variable doesn't exist.  This will occur the first iteration and putting it here saves me from having to initialize the variable beforehand.

1
2
3
4
5
6
echo "<table>";
while ($row = mysql_fetch_array($dataset)) {
  $bgcolour = (!isset($bgcolour) || $bgcolour == "#F0F7FF") ? "#FFFFFF" : "#F0F7FF";
  echo "<tr style='background-color:$bgcolour;'><td> TABLE DATA GOES HERE </td></tr>";
} // end while
echo "</table>";

Keep in mind – if you use this trick twice on the same page, you'll have to initialize the variable to ensure that the first row background is the same on each table.  You want it to be consistent on all tables.  In this situation it's best to initialize the value just above the while() loop as shown here.

 

1
2
3
4
5
6
7
echo "<table>";
$bgcolour = "#FFFFFF"; // initialize it here, outside the while() loop
while ($row = mysql_fetch_array($dataset)) {
  $bgcolour = ($bgcolour == "#F0F7FF") ? "#FFFFFF" : "#F0F7FF";
  echo "<tr style='background-color:$bgcolour;'><td> TABLE DATA GOES HERE </td></tr>";
} // end while
echo "</table>";