[thelist] PHP Mysql problem

Simon Willison cs1spw at bath.ac.uk
Tue Oct 7 05:09:52 CDT 2003

Simon Perry wrote:
> http://www.charterflights.co.uk/testcode/code.txt

No idea about your database problem I'm afraid, but I've got a tip for 
improving the readability of your code:

You use the following idiom for constructing a string of HTML:

$page_content="<h1>Charter flights to $fields[3] in $area_name ...";
$page_content.="<p>Charter flights from the UK to $destination ...";
$page_content.="... etc ...";

A tidier way of doing this is to use HEREDOC syntax:

$page_content = <<<EOD
<h1>Charter flights to {$fields[3]} in $area_name from the UK</h1>

<p>Charter flights from the UK to $destination are run mainly to provide 
transport for package holidays, excess seats are sold off on a flight 
only basis. The cost of these seats is usually cheap when compared to 
scheduled flights. Charter airlines can offer these cheap flights by 
offereing more restricted services than the major scheduled or low cost 

<div id="div1"><span id="searchTitle"><a 
href="javascript:displayIframe();"><span id="span1"></span> Search for 
and book online flights to {$fields[3]}</a></span></div>

<div id="search"><iframe id="searchFrame" 
marginwidth="0" marginheight="0" width="550" height="850" 

<h1>{$fields[3]} information</h1>


This has a number of advantages: you can neatly run the definition over 
multiple lines, you no longer have to \"escape your quotes\", you still 
get the benefits of variable interpolation (I prefer to wrap array 
indexes in curly braces {$like_this[0]} for clarity). I find this format 
much easier to read and maintain.

Another tip you might find useful: mysql_fetch_row() returns an array 
that is indexed by the name of the columns as well as by number. This 
can make code much clearer - instead of saying {$field[3]} you can have 
{$field['DESTINATION_NAME']}, which has a more obvious meaning.

I hope you find those tips useful, and I'm sorry I couldn't help with 
the databaser error.

Best regards,

Simon Willison

More information about the thelist mailing list