Re: [PHP] Looking for optimal coding
On 14 Feb 2002, at 22:18, Phillip S. Baker wrote: > This is not a big thing. > But I am looking at this thinking there is a way to make the code take > up even less lines. Just want to stick this in my cap for future > reference. > > for($i=01;$i<=50;$i++) { > if (!empty($content)) { > if ($row[$content]==$states[$i]) > echo " selected>$nstates[$i]\n"; > else > echo " value=\"$states[$i]\">$nstates[$i]\n"; > } > else{ > if ($dstate == $states[$i]) > echo " selected>$nstates[$i]\n"; > else > echo " value=\"$states[$i]\">$nstates[$i]\n"; > } > } > > Basically I want to check for two possible conditions to make an item > selected. If the first one is valid then do not check for the other. Hi, I'm not sure about the "two possible conditions". Seems arbitrary but you could do it with my suggestion by just passing an array with one value rather than more than one. Warning, I've only programmed in PHP for about a month now. What I would do if you, what I do when programming, is ask myself if I can abstract the code I'm writing. Might I reach out of the particular instance and see if there is something common in what I'm doing here. It can take a little longer initially but you can then use what you've created over and over again. What you wanted to do sounds like something I've done in Perl so I just re-wrote it in PHP. I have a module of common routines I nearly always use in forms and I have the function below in that module (in Perl). I put it all in a php contstruct so you could test it in your browser. Pardon all the data. I would not have used the values as the "key" for the $states array but it did not work unless I put something there. You can use this for any 2 sets of arrays (states, cities, colors, sizes, etc). I have common sets of arrays in my module as well. array(display=>'Montana',value=>'mt',),'mi'=>array(displ ay=>'Michigan',value=>'mi',),'md'=>array(display=>'Maryland',value= >'md',),'tx'=>array(display=>'Texas',value=>'tx',),'nm'=>array(display =>'New Mexico',value=>'nm',),'ut'=>array(display=>'Utah',value=>'ut',),'pa'=> array(display=>'Pennsylvania',value=>'pa',),'ne'=>array(display=>'Ne braska',value=>'ne',),'az'=>array(display=>'Arizona',value=>'az',),'mp '=>array(display=>'Mariana Islands',value=>'mp',),'wy'=>array(display=>'Wyoming',value=>'wy',), 'ia'=>array(display=>'Iowa',value=>'ia',),'ar'=>array(display=>'Arkans as',value=>'ar',),'la'=>array(display=>'Louisiana',value=>'la',),'id'=>arr ay(display=>'Idaho',value=>'id',),'al'=>array(display=>'Alabama',valu e=>'al',),'co'=>array(display=>'Colorado',value=>'co',),'ma'=>array(di splay=>'Massachusetts',value=>'ma',),'nc'=>array(display=>'North Carolina',value=>'nc',),'ny'=>array(display=>'New York',value=>'ny',),'dc'=>array(display=>'District Of Columbia',value=>'dc',),'ca'=>array(display=>'California',value=>'ca',) ,'vt'=>array(display=>'Vermont',value=>'vt',),'mo'=>array(display=>'M issouri',value=>'mo',),'tn'=>array(display=>'Tennessee',value=>'tn',),' or'=>array(display=>'Oregon',value=>'or',),'sc'=>array(display=>'Sou th Carolina',value=>'sc',),'wa'=>array(display=>'Washington',value=>'w a',),'in'=>array(display=>'Indiana',value=>'in',),'ks'=>array(display=>' Kansas',value=>'ks',),'oh'=>array(display=>'Ohio',value=>'oh',),'hi'=> array(display=>'Hawaii',value=>'hi',),'nv'=>array(display=>'Nevada',va lue=>'nv',),'nh'=>array(display=>'New Hampshire',value=>'nh',),'wv'=>array(display=>'West Virginia',value=>'wv',),'wi'=>array(display=>'Wisconsin',value=>'wi',),' nj'=>array(display=>'New Jersey',value=>'nj',),'ak'=>array(display=>'Alaska',value=>'ak',),'mh'= >array(display=>'Marshall Islands',value=>'mh',),'fm'=>array(display=>'Micronesia',value=>'fm',) ,'as'=>array(display=>'American Samoa',value=>'as',),'mn'=>array(display=>'Minnesota',value=>'mn', ),'ct'=>array(display=>'Connecticut',value=>'ct',),'me'=>array(display =>'Maine',value=>'me',),'de'=>array(display=>'Delaware',value=>'de', ),'sd'=>array(display=>'South Dakota',value=>'sd',),'ok'=>array(display=>'Oklahoma',value=>'ok',),' il'=>array(display=>'Illinois',value=>'il',),'gu'=>array(display=>'Guam', value=>'gu',),'nd'=>array(display=>'North Dakota',value=>'nd',),'va'=>array(display=>'Virginia',value=>'va',),'ky' =>array(display=>'Kentucky',value=>'ky',),'ri'=>array(display=>'Rho de Island',value=>'ri',),'ms'=>array(display=>'Mississippi',value=>'ms',),' pw'=>array(display=>'Palau',value=>'pw',),'ga'=>array(display=>'Geo rgia',value=>'ga',),'fl'=>array(display=>'Florida',value=>'fl',),); // what you would get from the form or use as a default $value_array = array("al","az"); $a =& SelectSelect($value_array,$states); sort($a); // let's test it echo ""; while (list ($key,$row) = e
RE: [PHP] Looking for optimal coding
> -Original Message- > From: Joffrey van Wageningen [mailto:[EMAIL PROTECTED]] > Sent: 15 February 2002 09:00 > > i would try: > > for($i=01;$i<=50;$i++) { > if(!empty($content) && $row[$content] == $states[$i]) > $selected = " selected"; > elseif($dstate == $states[$i]) > $selected = " selected"; > else > $selected = ""; > echo " value=\"".$states[$i]."\"".$selected.">".$nstates[$i].""; > } Well, all the boolean operators use lazy evaluation, so you can safely collapse your tests into a single one. Also, using the ?: operator, you can turn the whole thing into a single echo statement: for($i=01;$i<=50;$i++): echo "${nstates[$i]}"; endfor; Mind you, I wouldn't particularly claim this to be particularly readable, but it's about as compact as you can get! Cheers! Mike - Mike Ford, Electronic Information Services Adviser, Learning Support Services, Learning & Information Services, JG125, James Graham Building, Leeds Metropolitan University, Beckett Park, LEEDS, LS6 3QS, United Kingdom Email: [EMAIL PROTECTED] Tel: +44 113 283 2600 extn 4730 Fax: +44 113 283 3211 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Looking for optimal coding
for($i=1;$i<=50;$i++) { if ((!empty($content) && $row[$content] == $states[$i]) || ($dstate == $states[$i])) $selected="SELECTED"; else $selected=""; printf("%s", $states[$i], $selected, $nstates[$i]); } I always prefer to use printf to mix content and data. And why not use the || your reaching a single solution Jerry Verhoef >-Original Message- >From: Joffrey van Wageningen [mailto:[EMAIL PROTECTED]] >Sent: Friday, February 15, 2002 10:00 AM >To: PHP Email List >Subject: Re: [PHP] Looking for optimal coding > > > > > This is not a big thing. > > But I am looking at this thinking there is a way to make the code take >up > > even less lines. > > > for($i=01;$i<=50;$i++) { > > if (!empty($content)) { > > if ($row[$content]==$states[$i]) > > echo " > selected>$nstates[$i]\n"; > > else > > echo "value=\"$states[$i]\">$nstates[$i]\n"; > > } > > else{ > > if ($dstate == $states[$i]) > > echo " > selected>$nstates[$i]\n"; > > else > > echo "value=\"$states[$i]\">$nstates[$i]\n"; > > } > > } > >i would try: > >for($i=01;$i<=50;$i++) { > if(!empty($content) && $row[$content] == $states[$i]) > $selected = " selected"; > elseif($dstate == $states[$i]) > $selected = " selected"; > else > $selected = ""; > echo "value=\"".$states[$i]."\"".$selected.">".$nstates[$i].""; >} > >trading four echo's for a one echo and a extra var and nest the ifs > > > Basically I want to check for two possible conditions to make an item >selected. > > If the first one is valid then do not check for the other. > > > > Get what I mean? > > Any expert programmers out there with the way to chop this even further? > >test it :) > >mvgr, >Joffrey van Wageningen > >-- >.-[ Joffrey van Wageningen | WoLFjuh | [EMAIL PROTECTED] ]-- >| Networking Event 2000 - www.ne2000.nl - IRCnet:#ne2000, Undernet:#clue >| PGP:1024D/C6BA5863 - 3B93 52D3 CB91 9CB7 C50D FA79 865F 628A C6BA 5863 >| * We demand guaranteed rigidly defined areas of doubt and uncertainty. >| -- Douglas Adams > > >-- >PHP General Mailing List (http://www.php.net/) >To unsubscribe, visit: http://www.php.net/unsub.php > _ Chat on line met vrienden en probeer MSN Messenger uit: http://messenger.msn.nl -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Looking for optimal coding
> This is not a big thing. > But I am looking at this thinking there is a way to make the code take up > even less lines. > for($i=01;$i<=50;$i++) { > if (!empty($content)) { > if ($row[$content]==$states[$i]) > echo " selected>$nstates[$i]\n"; > else > echo "$nstates[$i]\n"; > } > else{ > if ($dstate == $states[$i]) > echo " selected>$nstates[$i]\n"; > else > echo "$nstates[$i]\n"; > } > } i would try: for($i=01;$i<=50;$i++) { if(!empty($content) && $row[$content] == $states[$i]) $selected = " selected"; elseif($dstate == $states[$i]) $selected = " selected"; else $selected = ""; echo "".$nstates[$i].""; } trading four echo's for a one echo and a extra var and nest the ifs > Basically I want to check for two possible conditions to make an item selected. > If the first one is valid then do not check for the other. > > Get what I mean? > Any expert programmers out there with the way to chop this even further? test it :) mvgr, Joffrey van Wageningen -- .-[ Joffrey van Wageningen | WoLFjuh | [EMAIL PROTECTED] ]-- | Networking Event 2000 - www.ne2000.nl - IRCnet:#ne2000, Undernet:#clue | PGP:1024D/C6BA5863 - 3B93 52D3 CB91 9CB7 C50D FA79 865F 628A C6BA 5863 | * We demand guaranteed rigidly defined areas of doubt and uncertainty. | -- Douglas Adams -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Looking for optimal coding
> Any expert programmers out there with the way to chop this > even further? I would suspect that chopping this further would make it even harder to understand/maintain in the future... J -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Looking for optimal coding
This is not a big thing. But I am looking at this thinking there is a way to make the code take up even less lines. Just want to stick this in my cap for future reference. for($i=01;$i<=50;$i++) { if (!empty($content)) { if ($row[$content]==$states[$i]) echo "$nstates[$i]\n"; else echo "$nstates[$i]\n"; } else{ if ($dstate == $states[$i]) echo "$nstates[$i]\n"; else echo "$nstates[$i]\n"; } } Basically I want to check for two possible conditions to make an item selected. If the first one is valid then do not check for the other. Get what I mean? Any expert programmers out there with the way to chop this even further? Thanks Phillip +++ IMPORTANT: This email message (including attachments, if any) is intended for the use of the individual addressee(s) named above and may contain information that is confidential, privileged or unsuitable for overly sensitive persons with low self-esteem, no sense of humor or irrational religious beliefs. If you are not the intended recipient, any dissemination, distribution or copying of this email is not authorized (either explicitly or implicitly) and constitutes an irritating social faux pas. Unless the word absquatulation has been used in its correct context somewhere other than in this warning, it does not have any legal or no grammatical use and may be ignored. No animals were harmed in the transmission of this email, although the barking dachshund next door is living on borrowed time, let me tell you. Those of you with an overwhelming fear of the unknown will be gratified to learn that there is no hidden message revealed by reading this warning backwards, so just ignore that Alert Notice from Microsoft. However, by pouring a complete circle of salt around yourself and your computer you can ensure that no harm befalls you, your family or your pets. If you have received this email in error, please add some nutmeg and egg whites, whisk and place in a warm oven for 40 minutes. +++ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php