Please help the drop down list!

08-18-04, 07:42 PM
|
|
Newbie Coder
|
|
Join Date: Aug 2004
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
|
Please help the drop down list!
Hi All,
I want to creat phone card rate drop down list.
database table name: rates
CREATE TABLE `rates` (
`country_name` varchar(20) default NULL,
`country_code` varchar(20) default NULL,
`area/city_code` varchar(20) default NULL,
`us_dollars` decimal(15,4) default NULL,
`toll_free` decimal(15,4) default NULL
) TYPE=MyISAM;
( i will manully insert data into database )
Basicly, i want people select country name from drop down list, and it will display a phone rates like this or just display us_dollars and toll free will be fine:
Country name country_code area/city_code us_dollars toll_free
Albania 355 N/A 0.2250 0.2550
Currently, i am a newbe for PHP, could you please anybody help me with it?
if the database is wrong, please help me correct it.
Thank you so much.
John
|

08-18-04, 10:33 PM
|
 |
Junior Code Guru
|
|
Join Date: Dec 2003
Location: Vancouver, BC, Canada
Posts: 550
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hello,
I am not too sure exactly what you want to accomplish here - would you like to display the detailed information right away in a textarea or something on the same page with or without reloading? or would you rather display the detailed information in another page?
As for the former, depending on how much data you will have in your rates table, you can 1) load every data in the page on the first page load and place a JavaScript onChange event to the combobox and change the data according to the user's selection on the fly in another textarea on the same page or something or 2) place an onChange event like the other one, but pass the ID via GET (e.g. myscript.php?rateID=2) to the same page, and only when this GET parameter is set, will you display the corresponding data from the database. As for the latter, you can do exactly the same as 2), but pass the ID to another page, instead of the same one.
As for your database table definition, you need a unique identifier (ID) called Primary Key. This is the rateID I refered to above. It can be any of the already existing fields, but it (or they) has to be unique throughout your table, like DNA for humans. It is very important that you understand Primary Key and what it's for. It is explained in most, if not all, SQL tutorials, so search for terms like 'primary key', 'rdbms', etc for more details.
I don't know how new you are to PHP, so I will stop here. Hope this gives you some idea.
__________________
Blavv =|
|

08-19-04, 04:17 PM
|
|
Newbie Coder
|
|
Join Date: Nov 2003
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hope this helps:
<?php
if (!$country){ ?>
<form id="GET RATES" action="<?=$PHP_SELF?>" method="post" name="FormName">
<select name="country" size="1">
<?php
$sql = "SELECT * from rates";
$result = mysql_query($sql)or die("Connect Error: ".mysql_error());
while ($row = mysql_fetch_array($result)){
?><option value="<?=$row[country_code]"><?=$row[country_name]?></option>
<?php } ?>
</select><input type="submit" name="" value="GO">
</form>
<?php } else {
<?php
$sql = "SELECT * from rates where country_code=('$country')";
$result = mysql_query($sql)or die("Connect Error: ".mysql_error());
$row = mysql_fetch_array($result);
?><?=$row[country_name]?> - <?=$row[us_dollars]?> - <?=$row[toll_free]?>
<?php } ?>
|

08-19-04, 04:18 PM
|
|
Newbie Coder
|
|
Join Date: Nov 2003
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Sorry, line nine should read:
?><option value="<?=$row[country_code]?>"><?=$row[country_name]?></option>
|

08-23-04, 09:47 PM
|
|
Newbie Coder
|
|
Join Date: Aug 2004
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Thank for helping me with the code.
However, i need a little more help from you again.
This is the information:
database i created:
CREATE TABLE `rates` (
`country_name` varchar(20) NOT NULL default '',
`country_code` varchar(20) default NULL,
`area/city_code` varchar(20) default NULL,
`us_dollars` decimal(15,4) default NULL,
`toll_free` decimal(15,4) default NULL,
PRIMARY KEY (`country_name`)
) TYPE=MyISAM;
#
# Dumping data for table `rates`
#
INSERT INTO `rates` VALUES ('Albania', '355', '', '0.2150', '0.2450');
and the code you provided and i added something:
--------------------------------------------
<?php
$location="localhost";
$username="xxx";
$password="yyy";
$database="zzz";
$result=mysql_connect("$location","$username","$pa ssword","$database");
mysql_select_db("rates",$result);
?>
<?php
if (!$country_name){ ?>
<form id="GET RATES" action="<?=$PHP_SELF?>" method="post" name="FormName">
<select name="country" size="1">
<?php
$sql = "SELECT * from rates";
$result = mysql_query($sql)or die("Connect Error: ".mysql_error());
while ($row = mysql_fetch_array($result)){
?><option value="<?=$row[country_code]?>"><?=$row[country_name]?></option>
<?php } ?>
</select><input type="submit" name="" value="GO">
</form>
<?php } else { ?>
<?php
$sql = "SELECT * from rates where country_code=('$country')";
$result = mysql_query($sql)or die("Connect Error: ".mysql_error());
$row = mysql_fetch_array($result);
?><?=$row[country_name]?> - <?=$row[us_dollars]?> - <?=$row[toll_free]?>
<?php } ?>
--------------------------------------------
but i can't see the Albania from drop down list instead is a blank:
Anybody can help me plz?
sean
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|