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

Friday 16 March 2007

MySQL: limiting the results of your query and show the total number of results

In an SQL query you can easily limit the results that are returned by using 'LIMIT start, records' at the end of the query.

$query = "SELECT id, name, city
FROM tbl_names
WHERE city="AMSTERDAM"
LIMIT 0,10";

$result = mysql_query($query);

Which will return the first ten names (LIMIT first=0, number of names=10).

In this case I am interested in the LIMIT option to reduce the results that I show but I also want to use the result of the query a little bit later on in the page to show the total number of results. In the above code I do not know the total number of results I would have gotten if I had not included the LIMIT statement.


To get the following result with one SQL query you can use the code below.
name 1
name 2
....
name 9
name 10

total number of names in Amsterdam: 34


The trick is to set the LIMIT in the $result = mysql_query($query); statement and not in the query itself.

$query = "SELECT id, name, city
FROM tbl_names
WHERE city="AMSTERDAM";

$result = mysql_query($query, " LIMIT 0,10");
$totalresult = mysql_query($query);
$totalnumberofnames= mysql_num_rows($totalresult);

$result is then used to show the 10 names and $totalnumberofnames is used to show '34' at the end. The query runs twice against your database but you only have to write the query once in your PHP.

Just make sure you do not forget the space between the " and LIMIT!