开发者

Can't parse (then loop) a variable that has multiple values separated by commas

I have database of companies and each can ship to only a certain number of states. I manage them with a form that sends to a MySQL field for all the states (seperating each state with a comma).

I'm having trouble parsing the string so that it will check the boxes attributed with the states when the page loads

$check ="NY,CA,FL";

function state_list($check) { 
$arr = array('AL'=>"Alabama",'AK'=>"Alaska",'AZ'=>"Arizona",'AR'=>"Arkansas",'CA'=>"California",'CO'=>"Colorado",'CT'=>"Connecticut",'DE'=>"Delaware",'DC'=>"DC",'FL'=>"Florida",'GA'=>"Georgia",'HI'=>"Hawaii",'ID'=>"Idaho",'IL'=>"Illinois", 'IN'=>"Indiana", 'IA'=>"Iowa",  'KS'=>"Kansas",'KY'=>"Kentucky",'LA'=>"Louisiana",'ME'=>"Maine",'MD'=>"Maryland", 'MA'=>"Massachusetts",'MI'=>"Michigan",'MN'=>"Minnesota",'MS'=>"Mississippi",'MO'=>"Missouri",'MT'=>"Montana",'NE'=>"Nebraska",'NV'=>"Nevada",'NH'=>"New Hampshire",'NJ'=>"New Jersey",'NM'=>"New Mexico",'NY'=>"New York",'NC'=>"North Carolina",'ND'=>"North Dakota",'OH'=>"Ohio",'OK'=>"Oklahoma", 'OR'=>"Oregon",'PA'=>"Pennsylvania",'PR'=>"Puerto Rico ",'RI'=>"Rhode Island",'SC'=>"South Carolina",'SD'=>"South Dakota",'TN'=>"Tennessee",'TX'=>"Texas",'UT'=>"Utah",'VT'=>"Vermont",'VA'=>"Virginia",'WA'=>"Washington",'WV'=>"West Virginia",'WI'=>"Wisconsin",'WY'=>"Wyoming");
foreach($arr as $k => $v){ 
    $checked = ($check == $k) ? ' checked="yes"' : '';
    echo '<li><input class="checkStates" name="states" type="checkb开发者_C百科ox" value="'.$k.'"'.$checked.'>'.$v.'</li>';
}
}


If $check is comma delimited you can explode the string and convert it into an array, then in the for loop, simply check if the state is in the array.

$check ="NY,CA,FL";
$arr = array('AL'=>"Alabama",'AK'=>"Alaska",'AZ'=>"Arizona",'AR'=>"Arkansas",'CA'=>"California",'CO'=>"Colorado",'CT'=>"Connecticut",'DE'=>"Delaware",'DC'=>"DC",'FL'=>"Florida",'GA'=>"Georgia",'HI'=>"Hawaii",'ID'=>"Idaho",'IL'=>"Illinois", 'IN'=>"Indiana", 'IA'=>"Iowa",  'KS'=>"Kansas",'KY'=>"Kentucky",'LA'=>"Louisiana",'ME'=>"Maine",'MD'=>"Maryland", 'MA'=>"Massachusetts",'MI'=>"Michigan",'MN'=>"Minnesota",'MS'=>"Mississippi",'MO'=>"Missouri",'MT'=>"Montana",'NE'=>"Nebraska",'NV'=>"Nevada",'NH'=>"New Hampshire",'NJ'=>"New Jersey",'NM'=>"New Mexico",'NY'=>"New York",'NC'=>"North Carolina",'ND'=>"North Dakota",'OH'=>"Ohio",'OK'=>"Oklahoma", 'OR'=>"Oregon",'PA'=>"Pennsylvania",'PR'=>"Puerto Rico ",'RI'=>"Rhode Island",'SC'=>"South Carolina",'SD'=>"South Dakota",'TN'=>"Tennessee",'TX'=>"Texas",'UT'=>"Utah",'VT'=>"Vermont",'VA'=>"Virginia",'WA'=>"Washington",'WV'=>"West Virginia",'WI'=>"Wisconsin",'WY'=>"Wyoming");

$states = explode(',', $check); 

foreach($arr as $k => $v){ 
    $checked = in_array($k, $states) ? ' checked="yes"' : '';
    echo '<li><input class="checkStates" name="states" type="checkbox" value="'.$k.'"'.$checked.'>'.$v.'</li>';
}


I'm thinking it has something to do with your comparison. Be sure that $check is uppercase. And if that passes then it must be something do with the value of $check being passed in.

EDIT: What you will need to do is this...

$ary = explode(",", $check);

foreach($arr as $k => $v) {
    $checked = (in_array($k, $ary)) ? ' checked="yes"' : '';
    echo '<li><input class="checkStates" name="states" type="checkbox" value="'.$k.'"'.$checked.'>'.$v.'</li>';
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜