Thursday, 3 May 2007

PHP: line of code only meant for internet explorer

Looking at my site in Firefox and in IE I saw a difference in the layout where I had text followed by a <DIV>.

Internet Explorer does not have a standard margin between the text and the DIV. So I had to find a way of adding space between them only in IE, not in the other browsers like Firefox.

The solution was that I added this line between the text and the <DIV>:



If IE reads this it will include the <p> tag, the other browsers will see it as comment and not display it.

You can also use this trick to hide comments (see older post). Just make sure you do not put dashes (like '-----') as the comment, this will invalidate the next line where you use this trick. So I used '****' instead in my comments to clearly mark pieces of coding.

HTML: HTML code validation

A handy tool that I found while trying to fix a bugging bug was the W3C markup validation service.

This will check your HTML pages for errors. As my website is made with PHP I have URL's which does not seem to be understood by the validation tool, however it did help me to fix the problem by popinting me to tags that were trying to close something that was not open (e.g. DIV, P).

I could have prevented these errors by working more precisely but as it is my first site I had to do quite a bit of reprogramming and experimenting.

The validation service can be found at: validator.w3.org

My website is: www.dejongfotografie.nl

PHP: blanc white page in IE and empty source page

I had problems with viewing my website in IE 6. Every now and then it displayed randomly a white page, without any source code but the notice 'done'. Searching on internet I found some comments on session_start that IE 6 does not handle this correctly (related to cookies and mod_gzip) which results in blank pages. However I do not use session_start so this was not hte case.

Reviewing my coding carefully I found some </p> tags and a </div> tag while there was no DIV to close.

Fixing these seems to have solved the problem. So basically a question of neat programming.

Saturday, 21 April 2007

PHP: putting string and a number with calculation together

In a form I created a loop to show a list of files and next to each file a check box. To give each check biox a unique name, I wanted to add the number of the file to it. So first I did:

<input type="checkbox" name="<?php echo 'upload' . $a-1; ?>">

I made the calculation because I already increased $a before the HTML code came. The result was:

<input type="checkbox" name="-1">

The trick is to put the calculation between brackets, like this:

<input type="checkbox" name="<?php echo 'upload' . ($a-1;) ?>">

And this results in the 'name' being: "upload1", "upload2" etc.

My website: www.dejongfotografie.nl

Wednesday, 21 March 2007

PHP: making a neat list of items

I have spend quite a few hours trying to get a list of links in my navigation that looked neat and tidy in both Mozilla based browsers and in IE. I tried to use UL and LI with all sorts of CSS but never got a result I liked. So I went back to using a TABLE. It might be regarded as not sophisticated but it gives the best list in my view.

I wanted to get a list of links (taken from a DB) and show them all under each other slightly indented to the right and a small bullet in front of each item.

I wrote the small piece of PHP to do this efficiently and neatly with a TABLE.

echo '<table border="0">';

while ($row = mysql_fetch_assoc($result)) {

echo '<tr><td><img src="buloranje.png"> <a href="index.php?page=link' .
$row['album_id'] . '">' . $row['al_name'] . '</a>' .
'</td></tr>';
}

echo '</table>';


The image of the bullet is 6px high and 4px wide. The bottom two lines of the image are transparent so that the bullet stands in the middle of the text.

Each link is stored in a row of $result by a SQL query.
The WHILE loop writes a line of the table <TR><TD> as long as there are results in $result. The link and display text is also build up out of data from the database.

When there are no more links to show, the table is closed.

Sunday, 18 March 2007

PHP: comparison of integer with string

PHP does not have explicit declarations of variable types. Whatever you put in a variable or array defines what the variable's type is. String, integer, boolean, float etc.

I have two variables, both containing a value. However somehow one of the variables is seen as a string, so when doing a comparison between the two strings it did not give a result. The variable was not explicitely loaded as a string, a value from the url is put in there using the GET method.

$var1=33 (integer)
$var2="33" (string)

if($var1 == $var2) {echo 'equal';}

This does not give a match. A quick although not very neat resolution is to make sure that var2 is seen as an integer. This can be done by using an arithmatic operator on the variable.

$var2++; //var2=34
$var2--; //var2=33

if($var1 == $var2) {echo 'equal';}

Results in the if-statement being true.

PHP: escaping double quotes

In PHP you sometimes have double quotes (this one: " ) in a string. Because double quotes also indicate the start and end of the string you have to escape them when they are within the string. Today I had the following example. I needed to put a piece of HTML to show an image into a string. So I tried:

$prev = "<img src="leftgr.GIF" width="13" height="13" border="0" >";

ofcourse this did notwork as PHP thinks the string ends with the second double quote "<img src=". It should be:

$prev = "<img src=\"leftgr.GIF\" width=\"13\" height=\"13\" border=\"0\" >";

The backslash (this one: \) escapes characters. PHP knows then then the character does not belong to the PHP code but is just a character in a string.

My website: www.dejongfotografie.nl