Jump to content

Recommended Posts

My for loop is simply printing out the array. I'm trying to make it produce option inputs. Anyone see what I'm doing wrong?

 

$state_arr = explode("|", $state);

for ($i=0; $i < (count($state_arr)); $i++)

{

echo "<option value='" . $state_arr[$i] . "'>" . $state_arr[$i] . "</option>";

}

 

$state = '\

UNITED STATES|\

Alaska|\

Alabama|\

Arkansas|\

American Samoa|\

Arizona|\

California|\

Colorado|\

Connecticut|\

D.C.|\

Delaware|\

Florida|\

Georgia|\

Hawaii|\

Iowa|\

Idaho|\

Illinois|\

Indiana|\

Kansas|\

Kentucky|\

Louisiana|\

Massachusetts|\

Maryland|\

Maine|\

Marshall Islands|\

Michigan|\

Minnesota|\

Missouri|\

Marianas|\

Mississippi|\

Montana|\

North Carolina|\

North Dakota|\

Nebraska|\

New Hampshire|\

New Jersey|\

New Mexico|\

Nevada|\

New York|\

Ohio|\

Oklahoma|\

Oregon|\

Pennsylvania|\

Puerto Rico|\

Rhode Island|\

South Carolina|\

South Dakota|\

Tennessee|\

Texas|\

Utah|\

Virginia|\

Virgin Islands|\

Vermont|\

Washington|\

Wisconsin|\

West Virginia|\

Wyoming|\

CANADA|\

Alberta|\

Manitoba|\

Alberta|\

British Columbia|\

Manitoba|\

New Brunswick|\

Newfoundland and Labrador|\

Nova Scotia|\

Northwest Territories|\

Nunavut|\

Ontario|\

Prince Edward Island|\

Quebec|\

Saskatchewan|\

Yukon Territory|\

UNITED KINGDOM|\

Avon|\

Bedfordshire|\

Berkshire|\

Buckinghamshire|\

Cambridgeshire|\

Cheshire|\

Cleveland|\

Cornwall|\

Cumbria|\

Derbyshire|\

Devon|\

Dorset|\

Durham|\

Essex|\

Gloucestershire|\

Greater London|\

Greater Manchester|\

Hampshire|\

Hereford & Worcestershire|\

Hertfordshire|\

Humberside|\

Isle of Man|\

Isle of Wight|\

Kent|\

Lancashire|\

Leicestershire|\

Lincolnshire|\

Merseyside|\

Norfolk|\

Northamptonshire|\

Northumberland|\

Nottinghamshire|\

Oxfordshire|\

Shropshire|\

Somerset|\

Staffordshire|\

Suffolk|\

Surrey|\

Sussex|\

Warwickshire|\

West Midlands|\

Wiltshire|\

Yorkshire|\

';

Link to comment
https://forums.phpfreaks.com/topic/62738-trouble-with-a-for-loop/
Share on other sites

This works.

You didn't include <select> perhaps? And possibly even <form>?

<?php

$state = '\
UNITED STATES|\
Alaska|\
Alabama|\
Arkansas|\
American Samoa|\
Arizona|\
California|\
Colorado|\
Connecticut|\
D.C.|\
Delaware|\
Florida|\
Georgia|\
Hawaii|\
Iowa|\
Idaho|\
Illinois|\
Indiana|\
Kansas|\
Kentucky|\
Louisiana|\
Massachusetts|\
Maryland|\
Maine|\
Marshall Islands|\
Michigan|\
Minnesota|\
Missouri|\
Marianas|\
Mississippi|\
Montana|\
North Carolina|\
North Dakota|\
Nebraska|\
New Hampshire|\
New Jersey|\
New Mexico|\
Nevada|\
New York|\
Ohio|\
Oklahoma|\
Oregon|\
Pennsylvania|\
Puerto Rico|\
Rhode Island|\
South Carolina|\
South Dakota|\
Tennessee|\
Texas|\
Utah|\
Virginia|\
Virgin Islands|\
Vermont|\
Washington|\
Wisconsin|\
West Virginia|\
Wyoming|\
CANADA|\
Alberta|\
Manitoba|\
Alberta|\
British Columbia|\
Manitoba|\
New Brunswick|\
Newfoundland and Labrador|\
Nova Scotia|\
Northwest Territories|\
Nunavut|\
Ontario|\
Prince Edward Island|\
Quebec|\
Saskatchewan|\
Yukon Territory|\
UNITED KINGDOM|\
Avon|\
Bedfordshire|\
Berkshire|\
Buckinghamshire|\
Cambridgeshire|\
Cheshire|\
Cleveland|\
Cornwall|\
Cumbria|\
Derbyshire|\
Devon|\
Dorset|\
Durham|\
Essex|\
Gloucestershire|\
Greater London|\
Greater Manchester|\
Hampshire|\
Hereford & Worcestershire|\
Hertfordshire|\
Humberside|\
Isle of Man|\
Isle of Wight|\
Kent|\
Lancashire|\
Leicestershire|\
Lincolnshire|\
Merseyside|\
Norfolk|\
Northamptonshire|\
Northumberland|\
Nottinghamshire|\
Oxfordshire|\
Shropshire|\
Somerset|\
Staffordshire|\
Suffolk|\
Surrey|\
Sussex|\
Warwickshire|\
West Midlands|\
Wiltshire|\
Yorkshire|\
';
?>
<form method="post" action="<?php $_SERVER['PHP_SELF']; ?>">
<select name="state">
<?php

$state_arr = explode("|", $state);
for ($i=0; $i < (count($state_arr)); $i++)
{
echo "<option value='" . $state_arr[$i] . "'>" . $state_arr[$i] . "</option>";
}

?>

</select>

</form>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.