Subject Re: [firebird-php] Jumping in the wagon
Author Jochem Maas
I couldn't resist commenting on the example. sorry if the critique seems harsh.

so you have a chance to bash my stuff also I offer an example of one of my files from
a smaller project (i.e. one that I didn't feel required any kind of framework because the
ramp up associated withm and complexity of a framework was overkill) at the bottom of this
post ... although there is no framework as such I do pretty much always hide away some of
the mundane stuff (like outputting consistent docuemtn and page headers) in a 'page'
class or something similiar and I have a number of simple output related functions, that help
keep the page scripts clean/simple, which I generally copy from one of these type's of
project to another (the selectTag() function is one example, the simpleTable() function is
another example, but that one is a bit of a weird one - it outputs a not so simple table - it kind
of took on a life of it's own!)


Alan McDonald wrote:
>
>

...

> 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.

using DA and the 'raw functions' intermittently inside a single project seems
inconsequential to say the least.

> Alan
>
> <? include "secureredir.php" ?>
> <html>
> <head>

you generally output HTML before running logic/doing queries/etc?
that is yuck imho.

> <?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">

this is more correct, also there should be html comments (or better
yet an HTML comments CDATA declaration) to hide the JS from old/shite
browsers:

<script type="text/javascript">
/* <![CDATA[ */

function bla() {}

/* ]]> */
</script>
> 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

...

> $db_table_where ="WHERE FK_SECCODE=".$_COOKIE['FK_SECCODE'];
> $alphacode = "";

all the above can be done in a single string which would make it more readable.

> 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) {

I would avoid using such 'descriptive' variable names - it's a maintainance nightmare imho.

> $id = $C1;
> if ($C4>1) {
> $C2 = "<a href='brcontact.php?id=".$C1."'><b>".$C2."</b> - <font

...

> .$C8."' target=new>".$C5."</a><br>";
> // $C5 = $C5.'<br>';
> }

defining functions halfway down a script rather than at the top/bottom or preferably
in a seperate file is maintainable?

> 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);
>

...

> ?>
> <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>

I guess XHTML is not your thing ;-)

>
> </html>


<?php
/**
* list.php :: list all employees!
*
* @author Jochem Maas <jochem@...>
* @copyright Copyright 2006, beeldspraak
* @link http://beeldspraak.com
*/

// ordering
$oSessVar = array('employee', 'o');
$dSessVar = array('employee', 'd');

MSess::initVar($oSessVar, 'login');
MSess::initVar($dSessVar, 'ASC');

$oCur = MSess::getVar($oSessVar);
$oNew = getG('o');
$oChk = $oNew ? $oNew : $oCur;
if ($oNew == $oCur) {
$dCur = MSess::getVar($dSessVar);
$oDir = ($dCur == 'ASC') ? 'DESC' : 'ASC';
} else {
$oDir = $oNew ? 'ASC' : MSess::getVar($dSessVar);
}

$oOpts= array('id' => 'id','name' => 'full name','active' => 'active','login' => 'login');
switch ($oChk) {
case 'id':
$orderby = 'id '.$oDir;
break;
case 'name':
$orderby = 'fullname '.$oDir;
break;
case 'active':
$orderby = 'active DESC, name '.$oDir;
break;
case 'login':
default:
$orderby = 'name '.$oDir;
break;

}
MSess::setVar($oSessVar, $oChk);
MSess::setVar($dSessVar, $oDir);

// filtering?

// do the query
$qry = "SELECT e.id, e.name, e.fullname, e.empcode, e.email, e.active, e.level, e.is_admin,
( SELECT (
SUM(h.monday) +
SUM(h.tuesday) +
SUM(h.wednesday) +
SUM(h.thursday) +
SUM(h.friday) +
SUM(h.saturday) +
SUM(h.sunday)
) FROM hours h WHERE h.employee_id=e.id
) AS hours_total
FROM employees e
ORDER BY $orderby";

$res = $MDB->query($qry);
$rows = $MDB->fetchrowset($res);

// create funky hour percentage field
$totalhours = 0;
foreach ($rows as $row) $totalhours += floatval($row['hours_total']);
$hours_1pc = $totalhours / 100;
foreach ($rows as $k => $row) $rows[$k]['hours_pc'] = $hours_1pc ? number_format(($row['hours_total'] / $hours_1pc), 2, '.',
'').' %' : '0 %';

// define column names for output
$cols = array('id',
'login',
'full name',
'trigram',
'email address',
'active',
'auth level',
'intranet admin',
'hours total',
'hours %');

// header
$Page->showHeader("Employees List");

echo '
<p>
<form method="get">
Order by: ',selectTag($oOpts, $oChk, 'o', 0, 'this.form.submit();'),'
  <input value="'.($oDir == 'ASC'?'▼':'▲').'" type="submit"/>
<form>
<br /><br />
</p>
';

if (!$res) {
echo "<p>query failure!</p>";
} else if ($MDB->numrows($res) == 0) {
echo "<p>no employees found! (adjust your filter if necessary.)</p>";
} else {
simpleTable($rows, $cols, true,
array('field' => 'id', 'urltpl' => '/employees/edit.php?'.POST_ID_URL.'=%s'),
array('field' => 'id', 'urltpl' => '/employees/delete.php?'.POST_ID_URL.'=%s'),
array('active', 'is_admin'),
false,
array('level', 'hours_total', 'hours_pc'));
}

// footer
$Page->showFooter();