473,796 Members | 2,585 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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".
Mar 24 '07 #1
19 1682
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,
Mar 24 '07 #2
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ø
- ki******@NOSPAM betadome.com
(remove NOSPAM to contact me directly)
Mar 24 '07 #3
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;$cou nter++)
if (isset($c['s']['a'.$counter]))
echo $c['s']['a'.$counter]."<br>";

--
Ra*********@gma il.com
"Olemme apinoiden planeetalla."
Mar 24 '07 #4
Rami Elomaa wrote:
Let's try again, shall we.

for($counter=1; $counter<6;$cou nter++)
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".
Mar 24 '07 #5
On 24 Mar, 15:37, Mary Pegg <inva...@invali d.comwrote:
Rami Elomaa wrote:
Let's try again, shall we.
for($counter=1; $counter<6;$cou nter++)
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.
Mar 24 '07 #6
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".
Mar 25 '07 #7
On 25 Mar, 21:37, Mary Pegg <inva...@invali d.comwrote:
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.

Mar 25 '07 #8
shimmyshack wrote:
On 25 Mar, 21:37, Mary Pegg <inva...@invali d.comwrote:
>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.
js*******@attgl obal.net
=============== ===
Mar 25 '07 #9
On 25 Mar, 23:37, Jerry Stuckle <jstuck...@attg lobal.netwrote:
shimmyshack wrote:
On 25 Mar, 21:37, Mary Pegg <inva...@invali d.comwrote:
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...@attgl obal.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.

Mar 25 '07 #10

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

Similar topics

36
6406
by: Andrea Griffini | last post by:
I did it. I proposed python as the main language for our next CAD/CAM software because I think that it has all the potential needed for it. I'm not sure yet if the decision will get through, but something I'll need in this case is some experience-based set of rules about how to use python in this context. For example... is defining readonly attributes in classes worth the hassle ? Does duck-typing scale well in complex
6
1674
by: Michael | last post by:
Hi, I'm fairly new at Python, and have the following code that works but isn't very concise, is there a better way of writing it?? It seems much more lengthy than python code i have read..... :-) (takes a C++ block and extracts the namespaces from it) def ExtractNamespaces(data): print("Extracting Namespaces")
43
3428
by: Rob R. Ainscough | last post by:
I realize I'm learning web development and there is a STEEP learning curve, but so far I've had to learn: HTML XML JavaScript ASP.NET using VB.NET ..NET Framework ADO.NET SSL
3
8531
by: baibaichen | last post by:
hi i want to output strings with indent space, the code looks like: std::set<std::string> files; std::set<std::string> iterator b = files.begin(); std::set<std::string> iterator e = files.end(); for (b; b!=e b++) std::cout << '\t' << *b << std::endl;
5
2917
by: B1ackwater | last post by:
We've fooled around with Access a bit, but only using the single-user store-bought version. It seems to be a good database - versatile and infinitely programmable - and can apparently be used as a front end to SQL server if we ever needed to go that route. But - is there a client/server version of Access ? Looking on the CDW site there is a bewildering variety of packages and licences and such, but we can't figure out just which do...
0
9673
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9525
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10452
Oralloy
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...
0
10221
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 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...
0
9050
agi2029
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...
0
6785
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5440
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...
0
5569
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2924
bsmnconsultancy
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...

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.