Jump to content

[SOLVED] Arrays Help


stublackett

Recommended Posts

Hi Guys,

 

Just working through WROX's Beginning PHP 5 Book and go to producing an web page that uses Arrays to differentiate countries and pair them to their capital. The form seems to function but doesnt echo the countries capital at all... Why would it do this? Pretty sure I've followed it word for word too..

 

Code is as follows.....

<html>
<head><title>Capitals</title></head>
<body>
<?php 
if (isset($_POST['posted'])) {
$country_capital = array(0 => "London", "Paris", "Moscow", "Lisbon", "Madrid", "Stockholm", "Copenhagen", "Washington", "Montreal", "Brisbane", "Tokyo", "Rome", "Mexico City", "Prague", "Berlin", "Dublin");
for ($counter=0; $counter<16; $counter++)
{
	if($_POST['hiddencountry'][$counter]==$_POST['country'])
	{
		echo "The capital of $_POST[country] is <b>$country_capital[$counter]</b><hr> ";
	}
}
}
?>
<form action="capitals.php" method="POST">
<input name="posted" type="hidden" value="true">
What Country do you want to know the capital of?
<select name="country">
<?php 
$capitals_of_countries = array(1 => "England", "France", "Russia", "Portugal", "Spain", "Sweden", "Denmark", "USA", "Canada", "Australia", "Japan", "Italy", "Mexico", "Czech Republic", "Germany", "Ireland");
for ($counter = 1; $counter <16; $counter++) {
echo "<option>$capitals_of_countries[$counter]</option>";
}
echo "</select><br><br>";
for ($counter = 1; $counter <16; $counter++) {
echo "<input type='hidden' name'hiddencountry[]' value='$capitals_of_countries[$counter]'>";
}
?>
<input type="submit" value="Find Capital">
</form>
</body>
</html>

 

Thanks in advance :)

Link to comment
Share on other sites

Your submit button needs a name.  In this case, it should be 'posted' (without the single quotes) because that's what you're checking to see is set before echoing out the result.  You also need to ensure that all non-variable array keys are in quotes.

 

Also, when echoing arrays in a string, I find it useful to use curly brackets, like so:

 

echo "The capital of {$_POST['country']} is <b>{$country_capital[$counter]}</b>";

 

When you get into more advanced code, like retrieving values from a database, you'll save yourself some headaches doing it that way.

Link to comment
Share on other sites

Hi stublackett,

 

While what Nightslyr has said is true, the reason the form isn't working is because you have just missed the = on the name attribute for the hidden input field.  Change it to:

 

echo "<input type='hidden' name='hiddencountry[]' value='$capitals_of_countries[$counter]'>";

 

Once you make this change your form will work as expected ;)

 

As an additional point, change every instance of $counter<16 to read $counter<17 instead, otherwise "Ireland" does not appear in the list.

 

Hope this helps.

Link to comment
Share on other sites

Not being funny, but that method doesn't make much sense to me. I'd do it like this:

<html>
<head><title>Capitals</title></head>
<body>
<?php 
$capitals_of_countries = array("England", "France", "Russia", "Portugal", "Spain", "Sweden", "Denmark", "USA", "Canada", "Australia", "Japan", "Italy", "Mexico", "Czech Republic", "Germany", "Ireland");
$country_capital = array("London", "Paris", "Moscow", "Lisbon", "Madrid", "Stockholm", "Copenhagen", "Washington", "Montreal", "Brisbane", "Tokyo", "Rome", "Mexico City", "Prague", "Berlin", "Dublin");

if (isset($_POST['posted'])) {
  echo "The capital of " . $capitals_of_countries[$_POST['country']] . " is <b>" 
      . $country_capital[$_POST['country']] . "</b><hr />";
}
?>
<form action="test.php" method="POST" />
<input name="posted" type="hidden" value="true" />
What Country do you want to know the capital of?
<select name="country">
<?php 
$tot = sizeof($capitals_of_countries);
for ($counter = 0; $counter < $tot; $counter++) {
echo '<option value="' . $counter . '">' . $capitals_of_countries[$counter] . '</option>';
}
?>
</select><br /><br />
<input type="submit" value="Find Capital" />
</form>
</body>
</html>

Link to comment
Share on other sites

An even better solution would be to simply crate an associative array of capitols to country:

 

$capsToCountries = array("London" => "England", "Paris" => "France", /* etc */);

foreach($capsToCountries as $cap => $country)
{
   echo "The capitol of $country is $cap";
}

Link to comment
Share on other sites

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.