|
|
|
|
Thread title: Ace Learning PHP Topic ! |
|
|
|
|
|
Thread tools
Search this thread
Display Modes
|
|
07-16-2005, 12:42 PM
|
#1
|
Status: Member
Join date: Jul 2005
Location: ****stan
Expertise:
Software:
Posts: 106
|
Ace Learning PHP Topic !
Hello ,
I am learning PHP so I made this thread for my help.i dont want to keep making threads and threads so i will make one topic for only my help.
Currently i am reading book by ATKINSON named as php core programming.it's great ! I am on chapter #2,Figure 2-3.
I am planning to create a script like where the users selects the country from the drop down box and then the form process and the country capital is displayed ? anybody can help me make this ?
|
|
07-16-2005, 01:31 PM
|
#2
|
Status: Senior Member
Join date: Jul 2005
Location:
Expertise:
Software:
Posts: 835
|
First you'll need a list of all the countries and their capital.
PHP Code:
<?php
$country = $_POST['country'];
$capital=array(
"England" => "London",
"France" => "Paris"
);
$capital = str_replace(array_keys($capital),array_values($capital),$country);
echo $capital;
?>
That's one way that wouldn't involve endless if and elseifs etc.
By the way, it's better to make more threads because others that are searching for help, will find it better if it was in a seperate thread.
Edit:seems to be a gap in the code for no reason, can't change it :s
|
|
07-16-2005, 01:35 PM
|
#3
|
Status: Member
Join date: Jul 2005
Location: ****stan
Expertise:
Software:
Posts: 106
|
Originally Posted by sketchie
First you'll need a list of all the countries and their capital.
PHP Code:
<?php
$country = $_POST['country'];
$capital=array(
"England" => "London",
"France" => "Paris"
);
$capital = str_replace(array_keys($capital),array_values($capital),$country);
echo $capital;
?>
That's one way that wouldn't involve endless if and elseifs etc.
By the way, it's better to make more threads because others that are searching for help, will find it better if it was in a seperate thread.
Edit:seems to be a gap in the code for no reason, can't change it :s
|
explain the code .. i told you i am new
|
|
07-16-2005, 04:08 PM
|
#4
|
Status: I love this place
Join date: Jan 2005
Location: UK, Birmingham
Expertise:
Software:
Posts: 606
|
(sketchie) Could you not just use for(each) and an if statement to check? rather then all that code you've put there?
PHP Code:
<?php
$country = $_POST['country'];
$capital=array(
"UK" => "London",
"France" => "Paris",
"Germany" => "Berlin",
"Spain" => "Madrid"
"Etc" => "Etc"
);
foreach ($capital as $k => $v)
{
if($country == $k)
{
echo "The Capital of ".$k." is ".$v;
}
}
?>
For Muhammad: This basically cycles through each of the countries finds a match and the echos the capital.
P.S. (sketchie) You don't know your countries and capitals London is the capital of the UK
|
|
07-16-2005, 05:20 PM
|
#5
|
Status: Member
Join date: Jul 2005
Location: ****stan
Expertise:
Software:
Posts: 106
|
Can't understand this bit of code
PHP Code:
foreach ($capital as $k => $v)
{
if($country == $k)
{
echo "The Capital of ".$k." is ".$v;
}
}
explain more !
|
|
07-16-2005, 05:37 PM
|
#6
|
Status: Lurker
Join date: Jul 2004
Location: the Netherlands
Expertise:
Software:
Posts: 1,074
|
Originally Posted by Muhammad Haris
Can't understand this bit of code
PHP Code:
foreach ($capital as $k => $v)
{
if($country == $k)
{
echo "The Capital of ".$k." is ".$v;
}
}
explain more !
|
www.php.net should answer your questions about functions.
|
|
07-16-2005, 06:36 PM
|
#7
|
Status: Senior Member
Join date: Jul 2005
Location:
Expertise:
Software:
Posts: 835
|
P.S. (sketchie) You don't know your countries and capitals London is the capital of the UK
|
I was referring to England not UK, if I was reffering to UK I would of stated so.
UK:
Scotland: Edinburgh
England: London
Wales: Cardiff
Ireland: Dublin
Oh and the question asked wasn't to cycle through all of them and echo them, it was to echo the user's choice, so there is no point in looping it.
Here is an explanation of my code:
PHP Code:
<?php
//Country is defined from a form (notice the orginal $_POST[])
$country = $_POST['country'];
//array is being created.
$capital=array(
//goes "Country => "Capital" seperated by commas
"England" => "London",
"France" => "Paris"
);
//string replaced is used so the country that's chosen selects from the array where it is the same. Then becomes $capital variable
$capital = str_replace(array_keys($capital),array_values($cap ital),$country);
echo $capital;
?>
|
|
07-16-2005, 07:08 PM
|
#8
|
Status: Member
Join date: Jul 2005
Location: ****stan
Expertise:
Software:
Posts: 106
|
and like if I want the user selects the country from the drop down box and then the capital is displayed ? can you tell me how to do that ?
|
|
07-16-2005, 09:28 PM
|
#9
|
Status: Senior Member
Join date: Jul 2005
Location:
Expertise:
Software:
Posts: 835
|
PHP Code:
if (isset($_POST['submit'])) {
//Country is defined from a form (notice the orginal $_POST[])
$country = $_POST['country'];
//array is being created.
$capital=array(
//goes "Country => "Capital" seperated by commas
"England" => "London",
"France" => "Paris"
);
//string replaced is used so the country that's chosen selects from the array where it is the same. Then becomes $capital variable
$capital = str_replace(array_keys($capital), array_values($capital),$country);
echo $capital;
} else {
echo '<form action="'.$_SERVER['PHP_SELF'].'" method="POST">';
echo '<select name="country">';
echo '<option>England</option>';
echo '<option>France</option>';
echo '</select>';
echo '<input type="submit" name="submit" value="go!">';
echo '</form>';
}
|
|
07-17-2005, 03:30 PM
|
#10
|
Status: Member
Join date: Apr 2005
Location:
Expertise:
Software:
Posts: 312
|
i would replace this:
PHP Code:
if(isset($_POST['submit']))
with this:
PHP Code:
if('go!' == $_POST['submit'])
otherwise your form will be submitted for any $_POST['submit'] value which bots can do by sending headers.
|
|
|
|
|
|
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
|