Today's Posts Follow Us On Twitter! TFL Members on Twitter  
Forum search: Advanced Search  
Navigation
Marketplace
  Members Login:
Lost password?
  Forum Statistics:
Forum Members: 24,254
Total Threads: 80,792
Total Posts: 566,471
There are 921 users currently browsing (tf).
 
  Our Partners:
 
  TalkFreelance     Design and Development     Programming     PHP and MySQL :

Ace Learning PHP Topic !

Thread title: Ace Learning PHP Topic !
Closed Thread  
Page 1 of 2 1 2 >
    Thread tools Search this thread Display Modes  
07-16-2005, 12:42 PM
#1
Muhammad Haris is offline Muhammad Haris
Muhammad Haris's Avatar
Status: Member
Join date: Jul 2005
Location: ****stan
Expertise:
Software:
 
Posts: 106
iTrader: 0 / 0%
 

Muhammad Haris is on a distinguished road

Send a message via MSN to Muhammad Haris

  Old  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
sketchie is offline sketchie
sketchie's Avatar
Status: Senior Member
Join date: Jul 2005
Location:
Expertise:
Software:
 
Posts: 835
iTrader: 1 / 100%
 

sketchie is on a distinguished road

  Old

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
Muhammad Haris is offline Muhammad Haris
Muhammad Haris's Avatar
Status: Member
Join date: Jul 2005
Location: ****stan
Expertise:
Software:
 
Posts: 106
iTrader: 0 / 0%
 

Muhammad Haris is on a distinguished road

Send a message via MSN to Muhammad Haris

  Old

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
opserty is offline opserty
Status: I love this place
Join date: Jan 2005
Location: UK, Birmingham
Expertise:
Software:
 
Posts: 606
iTrader: 0 / 0%
 

opserty is on a distinguished road

Send a message via MSN to opserty

  Old

(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
Muhammad Haris is offline Muhammad Haris
Muhammad Haris's Avatar
Status: Member
Join date: Jul 2005
Location: ****stan
Expertise:
Software:
 
Posts: 106
iTrader: 0 / 0%
 

Muhammad Haris is on a distinguished road

Send a message via MSN to Muhammad Haris

  Old

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
Aros is offline Aros
Aros's Avatar
Status: Lurker
Join date: Jul 2004
Location: the Netherlands
Expertise:
Software:
 
Posts: 1,074
iTrader: 2 / 100%
 

Aros is on a distinguished road

  Old

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
sketchie is offline sketchie
sketchie's Avatar
Status: Senior Member
Join date: Jul 2005
Location:
Expertise:
Software:
 
Posts: 835
iTrader: 1 / 100%
 

sketchie is on a distinguished road

  Old


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
Muhammad Haris is offline Muhammad Haris
Muhammad Haris's Avatar
Status: Member
Join date: Jul 2005
Location: ****stan
Expertise:
Software:
 
Posts: 106
iTrader: 0 / 0%
 

Muhammad Haris is on a distinguished road

Send a message via MSN to Muhammad Haris

  Old

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
sketchie is offline sketchie
sketchie's Avatar
Status: Senior Member
Join date: Jul 2005
Location:
Expertise:
Software:
 
Posts: 835
iTrader: 1 / 100%
 

sketchie is on a distinguished road

  Old

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
Koobi is offline Koobi
Koobi's Avatar
Status: Member
Join date: Apr 2005
Location:
Expertise:
Software:
 
Posts: 312
iTrader: 0 / 0%
 

Koobi is on a distinguished road

  Old

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.

Closed Thread  
Page 1 of 2 1 2 >


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

  Posting Rules  
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump:
 
  Contains New Posts Forum Contains New Posts   Contains No New Posts Forum Contains No New Posts   A Closed Forum Forum is Closed