[GRLUG] Programming efficiently

Justin Denick justin.denick at gmail.com
Tue Sep 25 10:48:06 EDT 2007


In your valued opinions, which is a better programming standard to follow

I have a maintenance page with SELECT boxes that offer content for a field.

Let's take states for instance

With ordinary html or even php printing html, the code would look like

<DEFANGED_select>
  <DEFANGED_option value=state_abrv>state_abrv</DEFANGED_option>
  {repeat for each state}
</DEFANGED_select>

Pros: No preprocessing at all Cons: HTML is for little girls

print("<DEFANGED_select>\n".
"<DEFANGED_option value=\"state_abrv\">state_abrv</DEFANGED_option>".
{repeat for each state}
"</DEFANGED_select>");

Pros: Call to print only once, No need to htmlize the content Cons: Still
html

A more dynamic approach might be
<?

$sql="SELECT st_abrv FROM schema.table-with-st_abrv ORDER BY st_abrv;";

$result = pg_query($sql) or die("You know that the table doesn't exist); //
$dbconn already established by pg_pconnect()

print("<DEFANGED_select>");

while ($rows = (pg_fetch_array($result,NULL,PGSQL_ASSOC)) {

  print("<DEFANGED_option value=\"$rows[st_abrv]\">$rows[st_abrv]</DEFANGED_option>");

}

print("</DEFANGED_select>");

pg_free_result($result);

?>
Pros: Dynamically generated content, less typing, scalable Cons: More calls
to print, More preprocessing

The first approach seemed okay for things that don't change, although it
makes for a lot of typing, which I try to avoid at all costs.
However, it doesn't provide a GUI avenue for adding more options. For
states, this probably won't be a problem, but other items may change. Making
them available to the maintenance page would require editing the actual
code.

The second approach offers more dynamics and less typing, but requires more
server preprocessing, before the users gets the page their looking for.

So I ask, which would you prefer?

?>
-- 
In vino veritas.
        [In wine there is truth.]
                -- Pliny
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://shinobu.grlug.org/pipermail/grlug/attachments/20070925/7692c511/attachment.htm 


More information about the grlug mailing list