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 "<option value=\"$states[$i]\" > selected>$nstates[$i]\n"; > else > echo "<option > value=\"$states[$i]\">$nstates[$i]\n"; > } > else { > if ($dstate == $states[$i]) > echo "<option value=\"$states[$i]\" > selected>$nstates[$i]\n"; > else > echo "<option > 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. <?php $states = array('mt'=>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 "<form><select name=state multiple>"; while (list ($key,$row) = each ($a) ) { echo <<<HTML <option value="$row[value]" $row[selected]>$row[display] </option><br> HTML; } echo "</select></form>"; // end test /*! @function SelectSelect @abstract send 2 arrays, 1 with just values, the other with the display/value options for your select list @param value_array array - plain array of values you want selected @param option_array array - anonymous array where each element has display/value for key @result array - anonymous array where each element has display/value plus a new key of selected where its value is "SELECTED" if you passed it as a value in your value_array array */ function &SelectSelect(&$value_array,&$option_array) { foreach($value_array as $key=>$Value) { $t[$Value] = 1; } //my %t = map {$_=>1} @{$value_array}; $select_loop = array(); while (list ($key,$row) = each ($option_array) ) { $selected = $t[$row["value"]] ? "SELECTED" : ""; $select_loop[]=array( display=>$row["display"], value=>$row["value"], selected=>$selected, ); } return $select_loop; } ?> Peter http://www.readbrazil.com/ Answering Your Questions About Brazil -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php