473,387 Members | 1,579 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Reducing code clutter

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'=>'weekday', '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'=>'weekday', '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
Jul 17 '05 #1
7 1988
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.
Jul 17 '05 #2
*** 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
--
Jul 17 '05 #3
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.
Jul 17 '05 #4
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)
Jul 17 '05 #5
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
Jul 17 '05 #6
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)
Jul 17 '05 #7
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
Jul 17 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
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...
5
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)....
12
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...
7
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...
0
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. ...
6
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...
8
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...
13
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:
4
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.