PDA

View Full Version : Debug my PHP script



nickmahon
July 10th, 2002, 16:56
I'm learning PHP right now, but this one has me stumped.

provinces.php:


<form method="post" action="provinces2.php">
Select a province to learn a city in it.
<select name="theprovinces">
<?php
$provinces = array(
1=> "Newfoundland and Labrador",
"Nova Scotia",
"New Brunswick",
"Prince Edward Island",
"Quebec",
"Ontario",
"Manitoba",
"Saskatchewan",
"Alberta",
"British Columbia",
"Nunavut",
"Northwest Territories",
"Yukon Territory"
);
for($counter=1; $counter<14; $counter++) {
print("<option>$provinces[$counter]</option>\n");
}
print("</select>\n");
for($counter=1; $counter<14; $counter++) {
echo("<INPUT TYPE=HIDDEN NAME='hiddenprovince[]' VALUE='$provinces[$counter]'>\n");
}
?>
<br>
<input type="submit">
</form>


provinces2.php


<?php

$cities = array(
0=> "Uhhhh...",
"Halifax",
"St. Johns",
"Charlottetown",
"Montreal",
"Toronto",
"Winnepeg",
"Regina",
"Calgary",
"Vancouver",
"Iqualit",
"Yellowknife",
"Whitehorse"
);

for ($counter=0; $counter<13; $counter++)
{
if($hiddenprovince[$counter]==$theprovince)
{
echo"The State capital is $cities[$counter]";
}
}

?>


It's basically supposed to be a dropdown menu, you select a province, press go, and it displays a city in that province... but instead it displays a blank page. :confused2

Canuckkev
July 10th, 2002, 17:08
I don't get the whole hiddenprovince thing?

nickmahon
July 10th, 2002, 17:16
It passes the province[] array to the next page.

Canuckkev
July 10th, 2002, 17:19
I think this will give you what you want...you should be able to select different cities in each province though, cause there is usually more than one.
provinces.php



<form method="post" action="provinces2.php">
Select a province to learn a city in it.
<select name="theprovinces">
<?php
$provinces = array(
0=> "Newfoundland and Labrador",
"Nova Scotia",
"New Brunswick",
"Prince Edward Island",
"Quebec",
"Ontario",
"Manitoba",
"Saskatchewan",
"Alberta",
"British Columbia",
"Nunavut",
"Northwest Territories",
"Yukon Territory"
);
for($counter=0; $counter<13; $counter++) {
echo"<option value=\"$counter\">$provinces[$counter]</option>\n";
}
echo "</select>\n";
?>
<br>
<input type="submit" value="Go!">
</form>


provinces2.php


<?


$cities = array(
0=> "St. Johns",
"Halifax",
"Frederiction",
"Charlottetown",
"Quebec City",
"Toronto",
"Winnipeg",
"Regina",
"Edmonton",
"Victoria",
"Iqualuit",
"Yellowknife",
"Whitehorse"
);


echo "The Provincial Capital is $cities[$theprovinces]<br>\n";

?>


Hehe...I changed the cities.

nickmahon
July 10th, 2002, 17:25
Thanks.

This is just an introduction lesson (Buy the book "Beginning PHP4", it's great) to the for(){} loop.

The only reason my first one didn't work was because I was telling it to print $theprovince instead of $theprovinces.

I spent a half hour looking over this. I'm stupid. :biggrin2: