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
https://forums.phpfreaks.com/topic/178737-solved-arrays-help/
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
https://forums.phpfreaks.com/topic/178737-solved-arrays-help/#findComment-942846
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
https://forums.phpfreaks.com/topic/178737-solved-arrays-help/#findComment-942853
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
https://forums.phpfreaks.com/topic/178737-solved-arrays-help/#findComment-942854
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.