Kenny Pollock Posted April 2, 2008 Share Posted April 2, 2008 I have a drop down with values like CALIFORNIA NORTH CALIFORNIA SOUTH GEORGIA NORTH GEORGIA SOUTH. When a user selects on and submits the form, the results page should select from the state table "CALIFORNIA" if the selected value on the previous page was "CALIFORNIA NORTH" or "CALIFORNIA SOUTH". I thought that's what I was doing here... but it doesn't seem to be working. function dropdown( $selected ) { if ( strpos($selected, 'CALIFORNIA') ) { $state = 'CALIFORNIA'; } elseif ( strpos($selected, 'GEORGIA') ) { $state = 'GEORGIA'; } elseif ( strpos($selected, 'KANSAS') ) { $state = 'KANSAS'; } elseif ( strpos($selected, 'MICHIGAN') ) { $state = 'MICHIGAN'; } elseif ( strpos($selected, 'MONTANA') ) { $state = 'MONTANA'; } elseif ( strpos($selected, 'NEVADA') ) { $state = 'NEVADA'; } elseif ( strpos($selected, 'NEBRASKA') ) { $state = 'NEBRASKA'; } elseif ( strpos($selected, 'NEW YORK') ) { $state = 'NEW YORK'; } elseif ( strpos($selected, 'NORTH DAKOTA') ) { $state = 'NORTH DAKOTA'; } elseif ( strpos($selected, 'PENNSYLVANIA') ) { $state = 'PENNSYLVANIA'; } elseif ( strpos($selected, 'SOUTH DAKOTA') ) { $state = 'SOUTH DAKOTA'; } elseif ( strpos($selected, 'TEXAS') ) { $state = 'TEXAS'; } elseif ( strpos($selected, 'MINNESOTA') ) { $state = 'MINNESOTA'; } else { $state = $selected; } $sql = mysql_query( "SELECT name FROM state_codes" ); while ( $row = mysql_fetch_array( $sql ) ) { echo '<option value="' . $row['name'] . '"'; if ( $row['name'] == $state ) { print(" selected"); } echo '>' . $row['name'] . '</option>'; } } Quote Link to comment https://forums.phpfreaks.com/topic/99249-best-way-to-do-this/ Share on other sites More sharing options...
gluck Posted April 2, 2008 Share Posted April 2, 2008 Is it returning 0 because this is the first match? Look at this >> $strpos = strpos($selected, 'CALIFORNIA'); if ($pos === false) {"not found"} else {"found"} Quote Link to comment https://forums.phpfreaks.com/topic/99249-best-way-to-do-this/#findComment-507821 Share on other sites More sharing options...
soycharliente Posted April 2, 2008 Share Posted April 2, 2008 Why are you checking to see if CALIFORNIA is contained in CALIFORNIA? Why can't you just use the value that's posted? Quote Link to comment https://forums.phpfreaks.com/topic/99249-best-way-to-do-this/#findComment-507827 Share on other sites More sharing options...
discomatt Posted April 2, 2008 Share Posted April 2, 2008 If you read the post, he says he wants CALI NORTH ro return as CALI as well... You want to use if ( strstr($selected, 'CALIFORNIA') !== FALSE ) Quote Link to comment https://forums.phpfreaks.com/topic/99249-best-way-to-do-this/#findComment-507831 Share on other sites More sharing options...
craygo Posted April 2, 2008 Share Posted April 2, 2008 he is checking trying to get the script to look in the california table if they select california south. I hope that is what I read. You can always put the string into an array. As long as you always have the state first then the area second. function dropdown($selected){ $array = explode(" ", $selected); // check just in case value is North Carolina South $count = count($array); if($count >2){ $state = $array[0]." ".$array[1]; } else { $state = $array[0]; } return $state; } Now if $selected is California North it will return California Ray Quote Link to comment https://forums.phpfreaks.com/topic/99249-best-way-to-do-this/#findComment-507833 Share on other sites More sharing options...
soycharliente Posted April 2, 2008 Share Posted April 2, 2008 If you read the post ... Oh yeah? I asked the question because I'm wondering if he could just put CALIFORNIA as the value for CALIFORNIA NORTH and then not have to worry about it. Quote Link to comment https://forums.phpfreaks.com/topic/99249-best-way-to-do-this/#findComment-507835 Share on other sites More sharing options...
discomatt Posted April 2, 2008 Share Posted April 2, 2008 He may not be able to edit the form itself. If he can though, yes, his best option is to do <select> ect.. <option value="CALI">CALI NORTH</option> <option value="CALI">CALI SOUTH</option> ect.. </select> Quote Link to comment https://forums.phpfreaks.com/topic/99249-best-way-to-do-this/#findComment-507837 Share on other sites More sharing options...
Kenny Pollock Posted April 2, 2008 Author Share Posted April 2, 2008 he is checking trying to get the script to look in the california table if they select california south. I hope that is what I read. You can always put the string into an array. As long as you always have the state first then the area second. function dropdown($selected){ $array = explode(" ", $selected); // check just in case value is North Carolina South $count = count($array); if($count >2){ $state = $array[0]." ".$array[1]; } else { $state = $array[0]; } return $state; } Now if $selected is California North it will return California Ray CALIFORNIA LOS ANGELES is returning CALIFORNIA LOS How would I do THAT? Besides that, its perfect! I cannot edit the data unfortunately. Thanks for your help! Quote Link to comment https://forums.phpfreaks.com/topic/99249-best-way-to-do-this/#findComment-507960 Share on other sites More sharing options...
Kenny Pollock Posted April 3, 2008 Author Share Posted April 3, 2008 Is this the most efficient? function dropdown( $selected ) { $array = explode(" ", $selected); // check just in case value is North Carolina South $count = count($array); if($count >3) { if($array[0] == 'NEW' && $array[1] == 'YORK') { $state = 'NEW YORK'; } else { $state = $array[0]." ".$array[1]; } } elseif($count >2) { if ( $array[2] != 'SOUTH' && $array[2] != 'NORTH' ) { $state = $array[0]; } else { $state = $array[0]." ".$array[1]; } } else { if ( $array[0] == 'WEST' ) { $state = $array[0]." ".$array[1]; } else { $state = $array[0]; } } $sql = mysql_query( "SELECT name FROM state_codes" ); while ( $row = mysql_fetch_array( $sql ) ) { echo $state; echo '<option value="' . $row['name'] . '"'; if ( $row['name'] == $state ) { echo ' selected'; } echo '>' . $row['name'] . '</option>'; } } Here's the data I have to work with: <option value="ALABAMA">ALABAMA</option> <option value="ARIZONA">ARIZONA</option> <option value="ARKANSAS">ARKANSAS</option> <option value="CALIFORNIA LOS ANGELES">CALIFORNIA LOS ANGELES</option> <option value="CALIFORNIA NORTHERN">CALIFORNIA NORTHERN</option> <option value="CALIFORNIA SAN DIEGO">CALIFORNIA SAN DIEGO</option> <option value="CALIFORNIA SANFRANCISCO">CALIFORNIA SANFRANCISCO</option> <option value="COLORADO">COLORADO</option> <option value="CONNECTICUT">CONNECTICUT</option> <option value="DELAWARE">DELAWARE</option> <option value="FLORIDA">FLORIDA</option> <option value="GEORGIA">GEORGIA</option> <option value="GEORGIA NORTH">GEORGIA NORTH</option> <option value="GEORIGA SOUTH">GEORIGA SOUTH</option> <option value="IDAHO">IDAHO</option> <option value="ILLINOIS">ILLINOIS</option> <option value="INDIANA">INDIANA</option> <option value="IOWA">IOWA</option> <option value="KANSAS">KANSAS</option> <option value="KANSAS KANSAS CITY">KANSAS KANSAS CITY</option> <option value="KENTUCKY">KENTUCKY</option> <option value="LOUISIANA">LOUISIANA</option> <option value="MAINE">MAINE</option> <option value="MARYLAND">MARYLAND</option> <option value="MASSACHUSETTS">MASSACHUSETTS</option> <option value="MICHIGAN NORTH">MICHIGAN NORTH</option> <option value="MICHIGAN SOUTH">MICHIGAN SOUTH</option> <option value="MINNESOTA">MINNESOTA</option> <option value="MINNESOTA NORTH">MINNESOTA NORTH</option> <option value="MINNESOTA SOUTH">MINNESOTA SOUTH</option> <option value="MISSISSIPPI">MISSISSIPPI</option> <option value="MISSOURI">MISSOURI</option> <option value="MONTANA">MONTANA</option> <option value="MONTANA NORTH">MONTANA NORTH</option> <option value="MONTANA SOUTH">MONTANA SOUTH</option> <option value="NEBRASKA">NEBRASKA</option> <option value="NEBRASKA WEST">NEBRASKA WEST</option> <option value="NEBRASKA OMAHA">NEBRASKA OMAHA</option> <option value="NEBRASKA WEST">NEBRASKA WEST</option> <option value="NEVADA LAS VEGAS">NEVADA LAS VEGAS</option> <option value="NEVADA RENO">NEVADA RENO</option> <option value="NEW HAMPSHIRE">NEW HAMPSHIRE</option> <option value="NEW JERSEY">NEW JERSEY</option> <option value="NEW MEXICO">NEW MEXICO</option> <option value="NEW YORK">NEW YORK</option> <option value="NEW YORK LONG ISLAND">NEW YORK LONG ISLAND</option> <option value="NEW YORK BROOKLYN">NEW YORK BROOKLYN</option> <option value="NEW YORK STATE">NEW YORK STATE</option> <option value="NORTH CAROLINA">NORTH CAROLINA</option> <option value="NORTH DAKOTA">NORTH DAKOTA</option> <option value="NORTH DAKOTA NORTH">NORTH DAKOTA NORTH</option> <option value="NORTH DAKOTA SOUTH">NORTH DAKOTA SOUTH</option> <option value="OHIO">OHIO</option> <option value="OKLAHOMA">OKLAHOMA</option> <option value="OREGON">OREGON</option> <option value="PENNSYLVANIA">PENNSYLVANIA</option> <option value="PENNSYLVANIA PHILLY">PENNSYLVANIA PHILLY</option> <option value="PENNSYLVANIA PITTSBURG">PENNSYLVANIA PITTSBURG</option> <option value="RHODE ISLAND">RHODE ISLAND</option> <option value="SOUTH CAROLINA">SOUTH CAROLINA</option> <option value="SOUTH DAKOTA">SOUTH DAKOTA</option> <option value="SOUTH DAKOTA NORTH">SOUTH DAKOTA NORTH</option> <option value="SOUTH DAKOTA SOUTH">SOUTH DAKOTA SOUTH</option> <option value="TENNESSEE">TENNESSEE</option> <option value="TEXAS ABILENE">TEXAS ABILENE</option> <option value="TEXAS AMARILLO">TEXAS AMARILLO</option> <option value="TEXAS CORPUS CHRISTI">TEXAS CORPUS CHRISTI</option> <option value="TEXAS DALLAS">TEXAS DALLAS</option> <option value="TEXAS EL PASO">TEXAS EL PASO</option> <option value="TEXAS HOUSTON">TEXAS HOUSTON</option> <option value="TEXAS MC ALLEN">TEXAS MC ALLEN</option> <option value="TEXAS SAN ANTONIO">TEXAS SAN ANTONIO</option> <option value="UTAH">UTAH</option> <option value="VERMONT">VERMONT</option> <option value="VIRGINIA">VIRGINIA</option> <option value="WASHINGTON STATE">WASHINGTON STATE</option> <option value="WEST VIRGINIA">WEST VIRGINIA</option> <option value="WISCONSIN NORTH">WISCONSIN NORTH</option> <option value="WISCONSIN SOUTH">WISCONSIN SOUTH</option> <option value="WYOMING">WYOMING</option> Quote Link to comment https://forums.phpfreaks.com/topic/99249-best-way-to-do-this/#findComment-508038 Share on other sites More sharing options...
craygo Posted April 3, 2008 Share Posted April 3, 2008 Looking at your data, this should fix it function dropdown($selected){ $array = explode(" ", $selected); // check just in case value is North Carolina South $count = count($array); if($count >2){ if($array[2] == "NORTH" || $array[2] == "SOUTH"){ $state = $array[0]." ".$array[1]; } else { $state = $array[0]; } } else { $state = $array[0]; } return $state; } Ray Quote Link to comment https://forums.phpfreaks.com/topic/99249-best-way-to-do-this/#findComment-508591 Share on other sites More sharing options...
Kenny Pollock Posted April 5, 2008 Author Share Posted April 5, 2008 NEW YORK LONG ISLAND shows up as NEW with your function Ray. This is my latest code, it does the job and works well with the data... but maybe it's not that efficient? You tell me function dropdown( $selected ) { $array = explode(" ", $selected); // check just in case value is North Carolina South $count = count($array); if($count >3) { if($array[0] == 'NEW' && $array[1] == 'YORK') { $state = 'NEW YORK'; } else { $state = $array[0]." ".$array[1]; } } elseif($count >2) { if ( $array[2] != 'SOUTH' && $array[2] != 'NORTH' ) { $state = $array[0]; } else { $state = $array[0]." ".$array[1]; } } else { if ( $array[0] == 'WEST' ) { $state = $array[0]." ".$array[1]; } else { $state = $array[0]; } } $sql = mysql_query( "SELECT name FROM state_codes" ); while ( $row = mysql_fetch_array( $sql ) ) { echo $state; echo '<option value="' . $row['name'] . '"'; if ( $row['name'] == $state ) { echo ' selected'; } echo '>' . $row['name'] . '</option>'; } } Quote Link to comment https://forums.phpfreaks.com/topic/99249-best-way-to-do-this/#findComment-509860 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.