There's got to be a better way

This is a discussion on There's got to be a better way within the PHP Language forums, part of the PHP Programming Forums category; There's got to be a better way: if (isset($c['s']['a1'])) echo $c['s']['a1']."<br&...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 03-24-2007
Mary Pegg
 
Posts: n/a
Default There's got to be a better way

There's got to be a better way:
if (isset($c['s']['a1'])) echo $c['s']['a1']."<br>";
if (isset($c['s']['a2'])) echo $c['s']['a2']."<br>";
if (isset($c['s']['a3'])) echo $c['s']['a3']."<br>";
if (isset($c['s']['a4'])) echo $c['s']['a4']."<br>";
if (isset($c['s']['a5'])) echo $c['s']['a5']."<br>";

but it's late and my brain is frazzled. Any ideas, oh PHP gurus?

--
"Checking identity papers is a complete waste of time. If anyone can
be counted on to have valid papers, it will be the terrorists".
Reply With Quote
  #2 (permalink)  
Old 03-24-2007
Snahad00
 
Posts: n/a
Default Re: There's got to be a better way

Mary Pegg pravi:
> There's got to be a better way:
> if (isset($c['s']['a1'])) echo $c['s']['a1']."<br>";
> if (isset($c['s']['a2'])) echo $c['s']['a2']."<br>";
> if (isset($c['s']['a3'])) echo $c['s']['a3']."<br>";
> if (isset($c['s']['a4'])) echo $c['s']['a4']."<br>";
> if (isset($c['s']['a5'])) echo $c['s']['a5']."<br>";
>
> but it's late and my brain is frazzled. Any ideas, oh PHP gurus?
>


Dunno about php but i think theoreticaly this should work:

$counter = 1
while $count < 6 do {
if (isset($c['s']['a.$counter'])) echo $c['s']['a$counter']."<br>";

$counter++;
}



regards,
Reply With Quote
  #3 (permalink)  
Old 03-24-2007
Kim André Akerĝ
 
Posts: n/a
Default Re: There's got to be a better way

Mary Pegg wrote:

> There's got to be a better way:
> if (isset($c['s']['a1'])) echo $c['s']['a1']."<br>";
> if (isset($c['s']['a2'])) echo $c['s']['a2']."<br>";
> if (isset($c['s']['a3'])) echo $c['s']['a3']."<br>";
> if (isset($c['s']['a4'])) echo $c['s']['a4']."<br>";
> if (isset($c['s']['a5'])) echo $c['s']['a5']."<br>";
>
> but it's late and my brain is frazzled. Any ideas, oh PHP gurus?


Perhaps like this?

foreach ($c['s'] as $item) {
echo $item."<br>";
}

If you want an extra check, you could also surround it with this:
if (isset($c['s']) and is_array($c['s'])) {
}

--
Kim André Akerĝ
- kimandre@NOSPAMbetadome.com
(remove NOSPAM to contact me directly)
Reply With Quote
  #4 (permalink)  
Old 03-24-2007
Rami Elomaa
 
Posts: n/a
Default Re: There's got to be a better way

Snahad00 kirjoitti:
> Mary Pegg pravi:
>> There's got to be a better way:
>> if (isset($c['s']['a1'])) echo $c['s']['a1']."<br>";
>> if (isset($c['s']['a2'])) echo $c['s']['a2']."<br>";
>> if (isset($c['s']['a3'])) echo $c['s']['a3']."<br>";
>> if (isset($c['s']['a4'])) echo $c['s']['a4']."<br>";
>> if (isset($c['s']['a5'])) echo $c['s']['a5']."<br>";
>>
>> but it's late and my brain is frazzled. Any ideas, oh PHP gurus?
>>

>
> Dunno about php but i think theoreticaly this should work:
>
> $counter = 1
> while $count < 6 do {
> if (isset($c['s']['a.$counter'])) echo $c['s']['a$counter']."<br>";
>
> $counter++;
> }


Let's try again, shall we.

for($counter=1;$counter<6;$counter++)
if (isset($c['s']['a'.$counter]))
echo $c['s']['a'.$counter]."<br>";

--
Rami.Elomaa@gmail.com
"Olemme apinoiden planeetalla."
Reply With Quote
  #5 (permalink)  
Old 03-24-2007
Mary Pegg
 
Posts: n/a
Default Re: There's got to be a better way

Rami Elomaa wrote:

> Let's try again, shall we.
>
> for($counter=1;$counter<6;$counter++)
> if (isset($c['s']['a'.$counter]))
> echo $c['s']['a'.$counter]."<br>";


Thanks. In the clear light of day, I think I'm going to stick with
what I've got anyway. There's lots of other things coming out of
$c['s'], and the existing way is arguably more readable.

--
"Checking identity papers is a complete waste of time. If anyone can
be counted on to have valid papers, it will be the terrorists".
Reply With Quote
  #6 (permalink)  
Old 03-24-2007
shimmyshack
 
Posts: n/a
Default Re: There's got to be a better way

On 24 Mar, 15:37, Mary Pegg <inva...@invalid.com> wrote:
> Rami Elomaa wrote:
> > Let's try again, shall we.

>
> > for($counter=1;$counter<6;$counter++)
> > if (isset($c['s']['a'.$counter]))
> > echo $c['s']['a'.$counter]."<br>";

>


> what I've got anyway. There's lots of other things coming out of


which is why you shouldnt be doing it this way

> $c['s'], and the existing way is arguably more readable.


that's a daft reason, its completely unmaintainable and unextensible
no more readable to someone familiar with other basic php constructs
what if you start learning about XSS and decide you need to protect
yourself against it, then you will have to find examples such as this
and change them all to


if (isset($c['s']['a5'])) echo htmlentities($c['s']['a1'])."<br>";
..
..
..
if (isset($c['s']['a5'])) echo htmlentities($c['s']['a5'])."<br>";
that's alot of work for sake of sticking with what _you_ find more
readable


and what if you then decide you need your pages to validate to xhtml?
oh dear another trip through all your scripts:


if (isset($c['s']['a5'])) echo htmlentities($c['s']['a1'])."<br />";
..
..
..
if (isset($c['s']['a5'])) echo htmlentities($c['s']['a5'])."<br />";


what if you add more tha a5, or if you change the keys
uh oh!

there are standards in programming, designed to make code more
acceptable to others coming after you


all the chages above and more are so simple with Rani's method. If you
can't take good advice don't ask for it.


Reply With Quote
  #7 (permalink)  
Old 03-25-2007
Mary Pegg
 
Posts: n/a
Default Re: There's got to be a better way

shimmyshack wrote:

> if (isset($c['s']['a5'])) echo htmlentities($c['s']['a1'])."<br>";
> .
> .
> .
> if (isset($c['s']['a5'])) echo htmlentities($c['s']['a5'])."<br>";
> that's alot of work for sake of sticking with what _you_ find more
> readable


So what you're really arguing in favour of is wrapping it up in a
function, which is what I've done. But the question is whether it's
worth creating a for loop to run through a1 to a5 rather than simply
calling each by name.

> all the chages above and more are so simple with Rani's method. If you


No, they're simple if it's wrapped up in a function. Whether or not
a1 to a5 get generated by a for loop I've still got b, c, d, e, f (etc)
to deal with. NB I'm using these as symbols - in reality they are the
field names from a database, so no getting smart and suggesting that I
can generate b to f automagically. OTOH I could stick the field names
in an array and step through the array. This might be worth doing.

> can't take good advice don't ask for it.


I know this is Usenet but you don't *have* to be rude and abrasive.

--
"Checking identity papers is a complete waste of time. If anyone can
be counted on to have valid papers, it will be the terrorists".
Reply With Quote
  #8 (permalink)  
Old 03-25-2007
shimmyshack
 
Posts: n/a
Default Re: There's got to be a better way

On 25 Mar, 21:37, Mary Pegg <inva...@invalid.com> wrote:
> shimmyshack wrote:
> > if (isset($c['s']['a5'])) echo htmlentities($c['s']['a1'])."<br>";
> > .
> > .
> > .
> > if (isset($c['s']['a5'])) echo htmlentities($c['s']['a5'])."<br>";
> > that's alot of work for sake of sticking with what _you_ find more
> > readable

>
> So what you're really arguing in favour of is wrapping it up in a
> function, which is what I've done. But the question is whether it's
> worth creating a for loop to run through a1 to a5 rather than simply
> calling each by name.
>
> > all the chages above and more are so simple with Rani's method. If you

>
> No, they're simple if it's wrapped up in a function. Whether or not
> a1 to a5 get generated by a for loop I've still got b, c, d, e, f (etc)
> to deal with. NB I'm using these as symbols - in reality they are the
> field names from a database, so no getting smart and suggesting that I
> can generate b to f automagically. OTOH I could stick the field names
> in an array and step through the array. This might be worth doing.
>
> > can't take good advice don't ask for it.

>
> I know this is Usenet but you don't *have* to be rude and abrasive.
>
> --
> "Checking identity papers is a complete waste of time. If anyone can
> be counted on to have valid papers, it will be the terrorists".


it's not rude or abrasive, to notice you don't take advice and say so,
perhaps a little bruusque though, and I apologise if I made you cross.
FYI, I work with annoyingly complex database tables whose structure
changes as my client changes their requirements, to stop this kind of
hard coded approach which started to cost me too much time, I use
DESCRIBE `tablename`, and SHOW FULL COLUMNS FROM `tablename` which can
then be used to get the comments, fieldnames etc... then using a reg
exp on bool enum varchar() int() and so on to get metadata about the
table which is then pumped into the application.
In this way you can use general methods to print and parse data
without ever having to hard code the fieldnames, you can use it to
dynamically generate forms etc...
Instead of repeatedly writing code to format output from the DB, you
just need a vlid link, and some form of instructions what your db
connection is to "get" and "output" and finally the output format -
preventing too much or too little from being drawn from the DB. You
only have to code this once, and after than it can be used everywhere
you need output. It might seem a little extreme to folks, I don't
know, but I find this approach saves time. The class which serves html
markup can end up being very complex but you can control things with a
couple of calls and an array.

Reply With Quote
  #9 (permalink)  
Old 03-26-2007
shimmyshack
 
Posts: n/a
Default Re: There's got to be a better way

On 25 Mar, 23:37, Jerry Stuckle <jstuck...@attglobal.net> wrote:
> shimmyshack wrote:
> > On 25 Mar, 21:37, Mary Pegg <inva...@invalid.com> wrote:
> >> shimmyshack wrote:
> >>> if (isset($c['s']['a5'])) echo htmlentities($c['s']['a1'])."<br>";
> >>> .
> >>> .
> >>> .
> >>> if (isset($c['s']['a5'])) echo htmlentities($c['s']['a5'])."<br>";
> >>> that's alot of work for sake of sticking with what _you_ find more
> >>> readable
> >> So what you're really arguing in favour of is wrapping it up in a
> >> function, which is what I've done. But the question is whether it's
> >> worth creating a for loop to run through a1 to a5 rather than simply
> >> calling each by name.

>
> >>> all the chages above and more are so simple with Rani's method. If you
> >> No, they're simple if it's wrapped up in a function. Whether or not
> >> a1 to a5 get generated by a for loop I've still got b, c, d, e, f (etc)
> >> to deal with. NB I'm using these as symbols - in reality they are the
> >> field names from a database, so no getting smart and suggesting that I
> >> can generate b to f automagically. OTOH I could stick the field names
> >> in an array and step through the array. This might be worth doing.

>
> >>> can't take good advice don't ask for it.
> >> I know this is Usenet but you don't *have* to be rude and abrasive.

>
> >> --
> >> "Checking identity papers is a complete waste of time. If anyone can
> >> be counted on to have valid papers, it will be the terrorists".

>
> > it's not rude or abrasive, to notice you don't take advice and say so,
> > perhaps a little bruusque though, and I apologise if I made you cross.
> > FYI, I work with annoyingly complex database tables whose structure
> > changes as my client changes their requirements, to stop this kind of
> > hard coded approach which started to cost me too much time, I use
> > DESCRIBE `tablename`, and SHOW FULL COLUMNS FROM `tablename` which can
> > then be used to get the comments, fieldnames etc... then using a reg
> > exp on bool enum varchar() int() and so on to get metadata about the
> > table which is then pumped into the application.
> > In this way you can use general methods to print and parse data
> > without ever having to hard code the fieldnames, you can use it to
> > dynamically generate forms etc...
> > Instead of repeatedly writing code to format output from the DB, you
> > just need a vlid link, and some form of instructions what your db
> > connection is to "get" and "output" and finally the output format -
> > preventing too much or too little from being drawn from the DB. You
> > only have to code this once, and after than it can be used everywhere
> > you need output. It might seem a little extreme to folks, I don't
> > know, but I find this approach saves time. The class which serves html
> > markup can end up being very complex but you can control things with a
> > couple of calls and an array.

>
> PMJI, but then you aren't much of a programmer.
>
> I've been doing SQL Database work for over 20 years now. I'll bet some
> of the databases I've designed would make yours look puny - over 100
> tables, over 1,500 columns, for instance. And mostly 3rd normal form.
>
> And yes, these databases do change as customer requirements change. But
> I deal with them.
>
> Your problem is that the user actually *cares* about the contents of the
> database. They don't. What they do care about is the *data* -
> including the relationships. Whether data is contained in one table or
> ten is not important.
>
> You can give all the excuses you want for not taking good advice. But
> the bottom line is - you haven't given any excuses we haven't heard
> hundreds of times. And you aren't explaining a situation most of us
> haven't run into multiple times. And we deal with it properly.
>
> --
> ==================
> Remove the "x" from my email address
> Jerry Stuckle
> JDS Computer Training Corp.
> jstuck...@attglobal.net
> ==================


I'm not sure what to make of that Jerry, but I wonder if my point was
clear enough. The idea is to write php classes which auto discover DB
structure, as does say phpmyadmin. The idea is to let the DB structure
be _independent_ of the php code, responding to changes within the DB
such as character encoding, field types, number of columns etc... so
that little or no hard coding and adjusting of the logic is needed,
just an adjustment - if any - to the "display" if one can call it that
- to config that prescribes the way the code interacts with the DB
tables.
I don't then mind how often changes are made to the DB structure,
which as you say always changes as the project grows and the
relationships become clearer, nor do I mind how often the tables are
split (pi$$ing contest avoided), provided a there exists a
data<~>query map.
ALways with programming it's bread and butter, reinventing the wheel
for each new app (even with OO)
forming the query, running it, getting the data, persisting it,
parsing, filtering, displaying and so on...
I wanted something a bit more like a fluent interface, more readable
and "semantic", in that it moves things on from having to write step
by step, and makes things a bit more fluid. I would be nice to get
eventually to a form where anyone could write in words what they
required; perhaps when chicken foot's reg exp parser comes up with
this ;)
I personally hate to see the type of hard coded stuff that gets banged
out, but it exists in the real world - of course.

Reply With Quote
  #10 (permalink)  
Old 03-26-2007
Jerry Stuckle
 
Posts: n/a
Default Re: There's got to be a better way

shimmyshack wrote:
> On 25 Mar, 21:37, Mary Pegg <inva...@invalid.com> wrote:
>> shimmyshack wrote:
>>> if (isset($c['s']['a5'])) echo htmlentities($c['s']['a1'])."<br>";
>>> .
>>> .
>>> .
>>> if (isset($c['s']['a5'])) echo htmlentities($c['s']['a5'])."<br>";
>>> that's alot of work for sake of sticking with what _you_ find more
>>> readable

>> So what you're really arguing in favour of is wrapping it up in a
>> function, which is what I've done. But the question is whether it's
>> worth creating a for loop to run through a1 to a5 rather than simply
>> calling each by name.
>>
>>> all the chages above and more are so simple with Rani's method. If you

>> No, they're simple if it's wrapped up in a function. Whether or not
>> a1 to a5 get generated by a for loop I've still got b, c, d, e, f (etc)
>> to deal with. NB I'm using these as symbols - in reality they are the
>> field names from a database, so no getting smart and suggesting that I
>> can generate b to f automagically. OTOH I could stick the field names
>> in an array and step through the array. This might be worth doing.
>>
>>> can't take good advice don't ask for it.

>> I know this is Usenet but you don't *have* to be rude and abrasive.
>>
>> --
>> "Checking identity papers is a complete waste of time. If anyone can
>> be counted on to have valid papers, it will be the terrorists".

>
> it's not rude or abrasive, to notice you don't take advice and say so,
> perhaps a little bruusque though, and I apologise if I made you cross.
> FYI, I work with annoyingly complex database tables whose structure
> changes as my client changes their requirements, to stop this kind of
> hard coded approach which started to cost me too much time, I use
> DESCRIBE `tablename`, and SHOW FULL COLUMNS FROM `tablename` which can
> then be used to get the comments, fieldnames etc... then using a reg
> exp on bool enum varchar() int() and so on to get metadata about the
> table which is then pumped into the application.
> In this way you can use general methods to print and parse data
> without ever having to hard code the fieldnames, you can use it to
> dynamically generate forms etc...
> Instead of repeatedly writing code to format output from the DB, you
> just need a vlid link, and some form of instructions what your db
> connection is to "get" and "output" and finally the output format -
> preventing too much or too little from being drawn from the DB. You
> only have to code this once, and after than it can be used everywhere
> you need output. It might seem a little extreme to folks, I don't
> know, but I find this approach saves time. The class which serves html
> markup can end up being very complex but you can control things with a
> couple of calls and an array.
>


PMJI, but then you aren't much of a programmer.

I've been doing SQL Database work for over 20 years now. I'll bet some
of the databases I've designed would make yours look puny - over 100
tables, over 1,500 columns, for instance. And mostly 3rd normal form.

And yes, these databases do change as customer requirements change. But
I deal with them.

Your problem is that the user actually *cares* about the contents of the
database. They don't. What they do care about is the *data* -
including the relationships. Whether data is contained in one table or
ten is not important.

You can give all the excuses you want for not taking good advice. But
the bottom line is - you haven't given any excuses we haven't heard
hundreds of times. And you aren't explaining a situation most of us
haven't run into multiple times. And we deal with it properly.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 07:37 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0