Subject RE: [firebird-php] Jumping in the wagon
Author Alan McDonald
> Alan, there is nothing to be sorry, this is the kind of response I'd
> like to have, different opinions and different approaches. I
> am trying
> to learn from your experiences. By the way, I could not disagree with
> you for two reasons:
> 1.) You are right about your point (use the right tool for
> the job, and
> the right tool is the one that the coder is more comfortable with)
> 2.) I have no experience in WEB development to disagree with
> anyone :-)
>
> thanks again !
>

If you look at what asp.net introduced a couple of years ago, they separate
logic and presentation to the tune of actually making 2 pages for each page,
one has presenataion and one has logic. I had a good long look at this and
only for the most mundane tasks could this separation make sense to me and
be maintained. As soon as you start doing something "real-world", the
separation falls in a heap, it's a joke. Logic code unavoidably creeps into
the presentation layer.
My approach has been always been, instead, don't "kick against the pricks"
(to use a biblical phrase) but to keep my pages always laid out the same way
so I always know where in the page I need to go to change something. The
same holds for PHP and ASP (which I also do a lot of still).
E.g. some of this is obvious
First part of Head section has all javascript... Client side validation, etc
and the main includes which are security re-diverts, data connections etc
Second part of Head section has function used as code blocks by later parts.
First part of body section has the form submission actions as well as the
form construction/declaration.
Second part of body section is the data extraction layer
Third part of body section has the data/format/layout scripting
Fourth part of page has the presentation code, sometimes provided by an
include to the more pure html, but which also includes logic which relies on
the previous parts (unavoidable).
Last part of the body/form has the hidden form inputs.

Here's a typical (short) example. It takes full advantage of the data
abstraction but I'm also not shy on performing direct data actions via the
base functions of php/ibase.
Alan


<? include "secureredir.php" ?>
<html>
<head>
<?php
include "version.php";
include_once "ezfirebird/ez_sql.php";
include_once "ez_resultsmcs.php";
include "metatags.php";
?>
<title>Account Browser</title>
<script language="JavaScript">
function toggledns(id, dns) {
if (dns) {
if (confirm('This action will allow solicitation\nfor all contacts of
this account if they individually allow it.\nConfirm allow solicitation?
')) {
document.braccount.toggledns.value= "ok";
document.braccount.accountid.value= id;
document.braccount.submit();
}
} else {
if (confirm('This action will prevent automated solicitation\nfor
this account and all related contacts.\nConfirm prevent solicitation?
')) {
document.braccount.toggledns.value= "ok";
document.braccount.accountid.value= id;
document.braccount.submit();
}
}
}
</script>
</head>
<body>
<form name="braccount" id="braccount" action="braccount.php" method="post">
<input type="hidden" name="accountid">
<input type="hidden" name="toggledns">
<input type="hidden" name="BRSR"
value=<?=(($_GET['BRSR']!=null)?$_GET['BRSR']:(($_POST['BRSR']!=null)?$_POST
['BRSR']:''))?>>
<input type="hidden" name="f"
value=<?=(($_GET['f']!=null)?$_GET['f']:(($_POST['f']!=null)?$_POST['f']:'')
)?>>
<input type="hidden" name="alpha"
value=<?=(($_GET['alpha']!=null)?$_GET['alpha']:(($_POST['alpha']!=null)?$_P
OST['alpha']:''))?>>

<?php
// Submission Actions
----------------------------------------------------------------------------
if (($_POST['toggledns']=="ok")&&($_POST['accountid']!=null)) {
$toggleid = $db->get_var("SELECT O_UPDATE FROM
TGL_DNS(".$_POST['accountid'].",'T')");
$_GET['BRSR']=$_POST['BRSR'];
$_GET['f']=$_POST['f'];
$_GET['alpha']=$_POST['alpha'];
}
// Data extraction
----------------------------------------------------------------------------
---
$db_table_name ="ACCOUNT";
$db_table_fields ="ID, ACCOUNT, DESCRIPTION, ";
$db_table_fields .="(SELECT COUNT(*) FROM CONTACT WHERE
CONTACT.FK_ACCOUNT=ACCOUNT.ID) AS CONTS, ";
$db_table_fields .="(SELECT ADDRESS1 FROM ADDRESS WHERE
ACCOUNT.FK_ADDRESS=ADDRESS.ID) AS ADDRESS1, ";
$db_table_fields .="(SELECT ADDRESS2 FROM ADDRESS WHERE
ACCOUNT.FK_ADDRESS=ADDRESS.ID) AS ADDRESS2, ";
$db_table_fields .="(SELECT CITY FROM ADDRESS WHERE
ACCOUNT.FK_ADDRESS=ADDRESS.ID) AS CITY, ";
$db_table_fields .="(SELECT STATE FROM ADDRESS WHERE
ACCOUNT.FK_ADDRESS=ADDRESS.ID) AS STATE, ";
$db_table_fields .="(SELECT POSTALCODE FROM ADDRESS WHERE
ACCOUNT.FK_ADDRESS=ADDRESS.ID) AS POSTALCODE, ";
$db_table_fields .="(SELECT ISPRIMARY FROM ADDRESS WHERE
ACCOUNT.FK_ADDRESS=ADDRESS.ID) AS ISPRIMARY, ";
$db_table_fields .="(SELECT ISMAILING FROM ADDRESS WHERE
ACCOUNT.FK_ADDRESS=ADDRESS.ID) AS ISMAILING, ";
$db_table_fields .="MAINPHONE, ";
$db_table_fields .="(SELECT COUNT(*) FROM C_LOAN_ACCOUNT WHERE
C_LOAN_ACCOUNT.FK_ACCOUNT=ACCOUNT.ID) AS LOANS, ";
$db_table_fields .="(SELECT COUNT(*) FROM C_LOAN_CONTACT WHERE
C_LOAN_CONTACT.FK_CONTACT IN (SELECT ID FROM CONTACT WHERE
FK_ACCOUNT=ACCOUNT.ID)) AS CLOANS, ";
$db_table_fields .="FK_REFERRER, (SELECT SECCODEDESC FROM SECCODE WHERE
ID=ACCOUNT.FK_REFERRER) AS REFERRER, FAX, DONOTSOLICIT, ";
$db_table_fields .="(SELECT COUNT(*) FROM FILENOTES WHERE
FK_ACCOUNT=ACCOUNT.ID) AS NOTECOUNT ";
$db_table_order ="ORDER BY CREATEDATE DESC, ID DESC";
$db_table_where ="WHERE FK_SECCODE=".$_COOKIE['FK_SECCODE'];
$alphacode = "";
if ( $_GET['alpha']!=null ) {
$db_table_where .=" AND ACCOUNT LIKE '".$_GET['alpha']."%' ";
$db_table_order ="ORDER BY UPPER(ACCOUNT)";
$ezr->set_qs_val("alpha",$_GET['alpha']);
$alphacode = ". Accounts starting with ".$_GET['alpha'].", name
order.";
}
// Visual Layout
----------------------------------------------------------------------------
------
$tblwidth = 770;
$ezr->num_results_per_page = 12;
$ezr->query_firebird("$db_table_fields FROM $db_table_name $db_table_where
$db_table_order");
$ezr->register_function('tweak_address');
function tweak_address($C1, $C2, $C3, $C4, $C5, $C6, $C7, $C8, $C9, $C10,
$C11, $C12, $C13, $C14, $C15, $C16, $C17, $C18, $C19) {
$id = $C1;
if ($C4>1) {
$C2 = "<a href='brcontact.php?id=".$C1."'><b>".$C2."</b> - <font
size=1>(<img src='images/users.gif' WIDTH='16' HEIGHT='16' BORDER=0
ALIGN='MIDDLE' ALT='Multiple Contacts'> ".$C4.")</font></a>";
} else {
$C2 = "<a href='brcontact.php?id=".$C1."'><b>".$C2."</b> - <font
size=1>(<img src='images/user.gif' WIDTH='16' HEIGHT='16' BORDER=0
ALIGN='MIDDLE' ALT='One Contact'> ".$C4.")</font></a>";
}
$C1 = "<a href='editaccount.php?id=".$C1."'><img src='images/edit.gif'
WIDTH='16' HEIGHT='16' BORDER=0 ALIGN='MIDDLE' ALT='Edit Account
Details'></a> <a href='brcontact.php?id=".$C1."'><img
src='images/useradd.gif' WIDTH='16' HEIGHT='16' BORDER=0 ALIGN='MIDDLE'
ALT='Add Contact'></a> <a
href='filenote.php?accountid=".$C1."&type=1&id=".$C1."'><img
src='images/pushpin.gif' WIDTH='16' HEIGHT='16' BORDER=".($C19>0?"1":"0")."
ALIGN='MIDDLE' ALT='Add File Note'></a> <a
href='brloan.php?id=".$C1."'><img src='images/currency.gif' WIDTH='16'
HEIGHT='16' BORDER=0 ALIGN='MIDDLE' ALT='Loans'></a>";
if ( !empty($C5) ) { // Address Line 1
$addrline1 = $C5;
$streetarray = split(' ', $addrline1, 3);
$C5 = "<a
href='http://www.whereis.com.au/whereis/mapping/geocodeAddress.do?streetNumb
er=".$streetarray[0]."&streetName=".$streetarray[1]."&suburb=".$C7."&state="
.$C8."' target=new>".$C5."</a><br>";
// $C5 = $C5.'<br>';
}
if ( !empty($C6) ) { // Address Line 2
$C6 = $C6.'<br>';
}
if ($C4>1) {
$C4 = "<img src='images/users.gif' WIDTH='16' HEIGHT='16' BORDER=0
ALIGN='MIDDLE' ALT='Multiple Contacts'> ".$C4;
} else {
$C4 = "<img src='images/user.gif' WIDTH='16' HEIGHT='16' BORDER=0
ALIGN='MIDDLE' ALT='1 Contact'> ".$C4;
}
$C13 = $C13+$C14;
$C12 = "<b>P:</b> ".$C12."<br><b>F:</b> ".$C17;
$C18 = ($C18=='T'?"<a href=javascript:toggledns(".$id.",true);><img
src='images/error.gif' WIDTH='16' HEIGHT='16' BORDER=0 ALIGN='MIDDLE'
ALT='DNS\r\nToggle:Do Not Solicit'></a>":"<a
href=javascript:toggledns(".$id.",false);><img src='images/tick.gif'
WIDTH='16' HEIGHT='16' BORDER=0 ALIGN='MIDDLE' ALT='Active\r\nToggle:Do Not
Solicit'></a>");
}
// $db->vardump($ezr);

$results_heading = "<tr bgcolor=cacad2>";
$results_heading .= " <td width=80 class='collabel' style='text-align:
center'><b>Actions</b></td>";
$results_heading .= " <td width=270 class='collabel'><b>Name</b></td>";
$results_heading .= " <td width=40 class='collabel' style='text-align:
center'><b>Loans</b></td>";
$results_heading .= " <td width=225
class='collabel'><b>Address</b></td>";
$results_heading .= " <td width=25 class='collabel'><b>DNS</b></td>";
$results_heading .= " <td width=100 class='collabel'><b>Phone</b></td>";
$results_heading .= "</tr>";
$results_row = " <tr bgcolor=ALTCOLOR2><td bgcolor=ALTCOLOR2
align=center><font size=1>COL1</font></td>";
$results_row .= " <td bgcolor=ALTCOLOR2>COL2</td>";
$results_row .= " <td bgcolor=ALTCOLOR2 align=center><font
color='red'><b>COL13</b></font></td>";
$results_row .= " <td bgcolor=ALTCOLOR3>COL5COL6COL7 COL8 COL9</td>";
$results_row .= " <td bgcolor=ALTCOLOR2>COL18</td>";
$results_row .= " <td bgcolor=ALTCOLOR1>COL12</td>";
$results_row .= "</tr>";
$ezr->results_open = "<table style='font-family: Tahoma; font-size:
8pt; color: 000066;' cellpadding=1 cellspacing=1 bgcolor=ffffff
width=".$tblwidth.">";
$ezr->results_heading = "<tr bgcolor=EFEFEF>$results_heading</tr>";
$ezr->results_row = "<tr bgcolor=FFFFFF>$results_row</tr>";
$ezr->show_num_pages = false;

echo "<LINK REL='stylesheet' HREF='browser.css' TYPE='text/css'>";
echo "<table BORDER=0 CELLPADDING=2 CELLSPACING=0 CLASS='content'
width=".$tblwidth."><tr>";
echo "<td CLASS='caption'><img src='images/blocks.gif' WIDTH='24'
HEIGHT='24' BORDER=0 ALIGN='MIDDLE' ALT='Accounts'> ACCOUNTS ordered by
date".$alphacode;
echo "</td>";
echo "<td STYLE='BACKGROUND-COLOR: #a5cbef' align='right'><BUTTON
style='BORDER-RIGHT: #000000 2px solid; BORDER-BOTTOM: #000000 2px solid;
BORDER-LEFT: #FFFFFF 2px solid; BORDER-TOP: #FFFFFF 2px solid;
FONT-SIZE:11px; FONT-FAMILY: Tahoma; FONT-WEIGHT: bold; TEXT-ALIGN: top;
width:85; height:22;'
onClick='javascript:helpinput(\"help/hlpaccounts.php\");'><IMG
SRC='images/help-2.gif' ALT='Help contents'
ALIGN=ABSMIDDLE>  Help</BUTTON></td>";
echo "</tr></table>\n";
echo "<table BORDER=0 CELLPADDING=2 CELLSPACING=0
CLASS='content'>\n<tr>";
echo "<td CLASS='caption' style='text-align:right'><img
src='images/spacer.gif'width=".(string)((int)$tblwidth-7)." height=0
border=0><br>";
echo "<BUTTON style='BORDER-RIGHT: #000000 2px solid; BORDER-BOTTOM:
#000000 2px solid; BORDER-LEFT: #FFFFFF 2px solid; BORDER-TOP: #FFFFFF 2px
solid; FONT-SIZE:11px; FONT-FAMILY: Tahoma; FONT-WEIGHT: bold; TEXT-ALIGN:
top; width:85; height:22;'
onClick='javascript:location.href=\"exportaccounts.php?id=".$ezr->results[0]
[12]."\"; return false;'><IMG SRC='images/filter-2.gif' ALT='Export contacts
list\nto CSV for import to Outlook' ALIGN=ABSMIDDLE> Export</BUTTON>";
echo "</td>\n</tr></table>\n";
include "alphaaccount.php";
$ezr->display();

echo "<p><hr align=left size=1 noshade width=".$tblwidth.">";

?>
<p><font face="tahoma" size="1"><b>Notes:</b>
<br>Click Account Name to edit/add contacts for the account.
<br>Numbers in brackets are contacts for this account.
<br><a href="accountnew.php">Click here</a> to add new account.
</font></p></br>
</form>
</body>

</html>