Hi all,
Newish PHP programmer here. I wrote a form select class to help reduce code
clutter, but I don't think the effort was worth it. I was hoping to post my
attempt and get some ideas from more advanced users on how to implement a
select form using less lines of code.
First the select form class which implements a select widget. I'll post only
the relevent parts:
class frmSelectClass
{
// Attributes
var $name = '';
var $size = '';
var $multiple = '';
var $id = '';
var $class = '';
var $style = '';
// Options
var $options = array();
function frmSelectClass( $arg = '')
{
if ( gettype($arg) == 'array' )
{
foreach ( array_keys($arg ) as $key )
{
if ( $key == 'name' )
$this->name = 'name="' . $arg['name'] . '" ';
elseif ( $key == 'size' )
$this->size = 'size="' . $arg['size'] . '" ';
elseif ( $key == 'multiple' )
$this->multiple = 'multiple="' . $arg['multiple'] . '" ';
elseif ( $key == 'id' )
$this->id = 'id="' . $arg['id'] . '" ';
elseif ( $key == 'class' )
$this->class = 'class="' . $arg['class'] . '" ';
elseif ( $key == 'style' )
$this->style = 'style="' . $arg['style'] . '" ';
}
}
}
function addOption($arg = '')
{
$value = '';
$label = '';
$selec = '';
if ( gettype($arg) == 'array' )
{
foreach ( array_keys($arg ) as $key )
{
if ( $key == 'value' )
$value = 'value="' . $arg['value'] . '"';
elseif ( $key == 'label' )
$label = $arg['label'];
elseif ( $key == 'selec' && $arg['selec'] == $arg['value'] )
$selec = 'selected ';
}
}
$this->options[] = "<option $value $selec>$label</option>\n";
}
function output()
{
$attributes = $this->name . $this->size . $this->multiple .
$this->id . $this->class . $this->style;
$retval = "<select $attributes>";
foreach ( $this->options as $opt )
$retval .= $opt;
echo $retval . '</select>';
}
}
A tad more complicated than what I was hoping for. Now I'll demonstrate how to
use the class to create a select form on a webpage.
$frm = new frmSelectClass( array('id'=>'we ekday', 'name'=>'frmDow ') );
$dow = array( 'Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat' );
for ( $i = 0; $i <= 6; $i++ )
$frm->addOption(
array( 'value'=>$dow[$i], 'label'=>$dow[$i], 'selec'=>date(' D') )
);
$frm->output();
That's about it. I thought I was being really sneaky by passing in arrays
that hold attributes, so this:
$frm = new frmSelectClass( array('id'=>'we ekday', 'name'=>'frmDow ') );
would yield this:
<select id="weekday" name="frmDow" >
It works, but I think it would've taken less lines of code to just use the
straight forward non-OOP method:
$id = 'weekday';
$name = 'frmDow';
echo "<select id=\"$id\" name=\"frmDow\" >
I'm sure lots of people have tried to abstract HTML forms using PHP classes.
It seems like a natural thing to do.
Any ideas on how to make the code more compact?
Thanks for reading down this far. ;-)
Pete 7 2028
Peter Salzman wrote: Hi all,
I'm sure lots of people have tried to abstract HTML forms using PHP classes. It seems like a natural thing to do.
Any ideas on how to make the code more compact?
Thanks for reading down this far. ;-)
Pete
There are more implemtation of form element abstrations around, but I
havn't used any so I don't know exactly where they are.
However, abstration isn't always about less code, sometimes it's about
convience. For instance, in your class you eliminated the HTML from the
main script, and it's created by the output function. Thus, your main
code is less cluttered with HTML, and more direct processing.
I tried implementing the HTMLDom once using php classes, and it works
ok, but I think the effort was probably in vein as creating even the
simplest of things becomes a really large pain using the DOM structure.
I started making a few abstract methods for common items (like a
select box) but eventually decided it wasn't worth in.
Something like what you have now is useable though. It's pretty simple
and seems like it's fairly easy to use. The # of lines in the end may
be the same, but the code probably looks better.
*** Peter Salzman wrote/escribió (Fri, 24 Jun 2005 03:17:50 +0000 (UTC)): if ( $key == 'name' ) $this->name = 'name="' . $arg['name'] . '" '; elseif ( $key == 'size' ) $this->size = 'size="' . $arg['size'] . '" '; elseif ( $key == 'multiple' ) $this->multiple = 'multiple="' . $arg['multiple'] . '" '; elseif ( $key == 'id' ) $this->id = 'id="' . $arg['id'] . '" '; elseif ( $key == 'class' ) $this->class = 'class="' . $arg['class'] . '" '; elseif ( $key == 'style' ) $this->style = 'style="' . $arg['style'] . '" ';
A little suggestion: http://www.php.net/switch
--
-- Álvaro G. Vicario - Burgos, Spain
-- http://bits.demogracia.com - Mi sitio sobre programación web
-- Don't e-mail me your questions, post them to the group
--
Alvaro G Vicario wrote: *** Peter Salzman wrote/escribió (Fri, 24 Jun 2005 03:17:50 +0000 (UTC)): if ( $key == 'name' ) $this->name = 'name="' . $arg['name'] . '" '; elseif ( $key == 'size' ) $this->size = 'size="' . $arg['size'] . '" '; elseif ( $key == 'multiple' ) $this->multiple = 'multiple="' . $arg['multiple'] . '" '; elseif ( $key == 'id' ) $this->id = 'id="' . $arg['id'] . '" '; elseif ( $key == 'class' ) $this->class = 'class="' . $arg['class'] . '" '; elseif ( $key == 'style' ) $this->style = 'style="' . $arg['style'] . '" ';
A little suggestion: http://www.php.net/switch
Given the structure, *any* conditional branching is redundant:
foreach ($arg as $key=>$val) {
print " $key = '$val' ";
}
C.
Peter Salzman wrote: Hi all,
Newish PHP programmer here. I wrote a form select class to help reduce code clutter, but I don't think the effort was worth it. I was hoping to post my attempt and get some ideas from more advanced users on how to implement a select form using less lines of code.
First the select form class which implements a select widget. I'll post only the relevent parts:
Pete, IMHO the value of abstraction of this sort in a short-transaction
environment like the web is severely overrated, to the point of being an
obstruction to productive work. Did you by any chance come from a desktop
dev environment?
My own work is all around databases. If yours is also, you may benefit from
what I did. It centers around having a data dictionary specifying what the
columns are in all of the tables (it is much more than that, but that is a
good introduction). When it comes time to generate HTML widgets, you read
the dd and look at what kind of columns you have and generate appropriate
widgets (mostly textbox, textarea, and SELECT lists).
If you can give more context about the kind of site you are working on
perhaps myself or others can give more details.
--
Kenneth Downs
Secure Data Software, Inc.
(Ken)nneth@(Sec )ure(Dat)a(.com )
Kenneth Downs <kn************ **@see.sigblock > wrote: Peter Salzman wrote:
Hi all,
Newish PHP programmer here. I wrote a form select class to help reduce code clutter, but I don't think the effort was worth it. I was hoping to post my attempt and get some ideas from more advanced users on how to implement a select form using less lines of code.
First the select form class which implements a select widget. I'll post only the relevent parts:
Pete, IMHO the value of abstraction of this sort in a short-transaction environment like the web is severely overrated, to the point of being an obstruction to productive work. Did you by any chance come from a desktop dev environment?
My own work is all around databases. If yours is also, you may benefit from what I did. It centers around having a data dictionary specifying what the columns are in all of the tables (it is much more than that, but that is a good introduction). When it comes time to generate HTML widgets, you read the dd and look at what kind of columns you have and generate appropriate widgets (mostly textbox, textarea, and SELECT lists).
If you can give more context about the kind of site you are working on perhaps myself or others can give more details.
Hi Ken,
I'm not a programmer by trade. It's just something I really enjoy doing.
I'd rather program than anything else in the world. I'm actually a physicist
by trade (well, a grad student by trade, but will soon be a physicist by
trade).
I'm writing my own blog, so I fit under your "short-transaction" model. I
know there are more blogs out there than you can shake a fist at (some quite
good), but I wanted an excuse to learn sessions, SQL and using
mysql/sqlite/pgsql securely with PHP.
The blog itself has been a wonderful learning experience (I've only written 3
posts, although I've spent an awful long time writing the blog itself!). My
sole interest, really, is to become a better programmer and to have fun doing
it.
BTW, I really appreciate the other answers to my post. You guys had some
really good stuff to say!
Pete
Peter Salzman wrote: Kenneth Downs <kn************ **@see.sigblock > wrote: Peter Salzman wrote:
> Hi all, > > Newish PHP programmer here. I wrote a form select class to help reduce > code > clutter, but I don't think the effort was worth it. I was hoping to > post my attempt and get some ideas from more advanced users on how to > implement a select form using less lines of code. > > First the select form class which implements a select widget. I'll > post only the relevent parts: > > Pete, IMHO the value of abstraction of this sort in a short-transaction environment like the web is severely overrated, to the point of being an obstruction to productive work. Did you by any chance come from a desktop dev environment?
My own work is all around databases. If yours is also, you may benefit from what I did. It centers around having a data dictionary specifying what the columns are in all of the tables (it is much more than that, but that is a good introduction). When it comes time to generate HTML widgets, you read the dd and look at what kind of columns you have and generate appropriate widgets (mostly textbox, textarea, and SELECT lists).
If you can give more context about the kind of site you are working on perhaps myself or others can give more details.
Hi Ken,
I'm not a programmer by trade. It's just something I really enjoy doing. I'd rather program than anything else in the world. I'm actually a physicist by trade (well, a grad student by trade, but will soon be a physicist by trade).
Can appreciate that double life. When I was supposed to be doing Maxwell's
Equations I was usually off hacking somewhere. Hence I am programmer by
trade with a BS Physics. I'm writing my own blog, so I fit under your "short-transaction" model. I know there are more blogs out there than you can shake a fist at (some quite good), but I wanted an excuse to learn sessions, SQL and using mysql/sqlite/pgsql securely with PHP.
This is a perfect way to learn those things. The existence of those blogs
gives you a standard against which to measure your efforts. The blog itself has been a wonderful learning experience (I've only written 3 posts, although I've spent an awful long time writing the blog itself!). My sole interest, really, is to become a better programmer and to have fun doing it.
My suggestion is Ken's Umpteenth Law: Don't Abstract Against Non-existent
Cases. You are introducing a level of abstraction far above what is
required for a blog. Further, you are doing so without the intuitive feel
that comes with more years of experience, so you are risking entry into the
hall of mirrors, where you will be lost, forever lost. Therefore, commit
to writing a decidely NON-ABSTRACT blog that does what it is supposed to
do. Then gradually refactor the code to create general-purpose routines
wherever you see repetition.
Hope this helps.
--
Kenneth Downs
Secure Data Software, Inc.
(Ken)nneth@(Sec )ure(Dat)a(.com )
I suggest you take a look at MVC Design patterns (just google like I wrote
;) ), and also take a look at refactoring code techniques (which is fancy
way to say - reducing code clutter in old code base ;) )
--
Dominik Susmel | art director www.vongestern.com | vonGestern art company . Zagreb, Croatia This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Ralf W. Grosse-Kunstleve |
last post by:
******************************************************************************
This posting is also available in HTML format:
http://cci.lbl.gov/~rwgk/python/adopt_init_args_2005_07_02.html
******************************************************************************
Hi fellow Python coders,
I often find myself writing::
class grouping:
|
by: Salvador I. Ducros |
last post by:
Greetings all,
I was hoping someone might be able to point me in the right direction. I'm
currently
using std::vector to maintain several lists whose objects are of unrelated
types (classes).
I've seen mentioned here and there a technique that uses 'void *' and
something called
'template specialization' that would reduce the amount of code generated for
the
|
by: Stuart MacMartin |
last post by:
Looking for tricks for speeding up builds...
We have an application with about 970 of our own classes of various
sizes plus a fair number of routines. Over the past year the
compile/link time has been slowing down on PC (MFC dev studio 6) and on
linux. Link time on PC seems to depend on RAM - a new desktop machine
with 2Gb of RAM compiles and links significantly faster than a three
year old laptop with 1/2 Gb of RAM but same processor...
|
by: Alfonso Morra |
last post by:
I have a class that contains a nested class. The outer class is called
outer, and the nested class is called inner. When I try to compile the
following code, I get a number of errors. It is not obvious to me, where
I'm going wrong (the compiler messages do not seem to make much sense).
here is the code:
outer class declared as ff in "outer.h":
|
by: Filipe Martins |
last post by:
Hello to all.
Ever had a problem when your print some reports in a printer other the
one you use more frequently, in which the report width span more tban
one page? I did, and didn't liked it.
But there's a simple way for automatically reducing the report size to
a size that prints well in the troubling printer. Here goes a sub that
receives the following parameters:
| |
by: John Wood |
last post by:
As everybody points out, the best way to reduce the memory footprint of a
..net application is to minimize it and restore it. This can make my app go
from 40Mb of usage to about 3Mb of usage. Of course it will grow again
gradually...
My clients have requested that I somehow automate this... so that it
achieves the same result periodically without them having to minimize the
application. They claim otherwise it's difficult for them to...
|
by: Greg Merideth |
last post by:
I've written a basic windows service to provide some helper xml functions for
my web methods and even thou the service is only about 1k lines long with 1
timer, its mem usage is 10m and its vm mem usage is 14! The same code written
as a program that requires you to click on the menu options to fire off the
events takes up 4/9mb.
I've seen examples where calling SetProcessWorkingSetSize(hWnd, -1, -1); does
the same thing as minimizing a...
|
by: Skybuck Flying |
last post by:
Hello,
Why does C++ use :: for members/methods why not just : ?
For example:
long Test::TimesOne() const { return mVal; }
Alternative:
|
by: d0ugg |
last post by:
Hello everyone,
I'm creating a program that it is suppose to add, subtract, multiply and also divide fractions and after that, the result has to be reduced to the lowest terms.
However, I'm not sure how the algorithm of reducing fractions works.
Here is part of my code:
//Header File
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |