Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Neater or improved way of datacheck?

Status
Not open for further replies.

wudz

Programmer
Mar 28, 2001
135
0
0
GB
Hi,

On my site when a customer registers on the town input they have a dropdown menu with all the towns I cover (hopefully), but this when comlete would approach around 40 - 50 towns and may miss the odd village/town, anyway I then have around 14 IF statements to check which town was choosen on average of 3 towns per IF selection (area) ie IF $timbucktoo or $happytown or $greattown then $area=1..etc.

When their town is found they are given an area number relating to the 3 towns. This number is saved with their customer details...

When another customer logs in using his area number all other customer items with the same area number are shown...

All works well, but all those If statments and town names in the script seem mighty messy and also there is no allowance for missed towns..could change town input title to Choose Nearest Town, but again messy...

Any ideas on impoving would be appreciated, but please make allowances for me being a newbie.

Cheers
John
 
I don't think 40 or 50 towns to look through is so bad. I've been to e-commerce sites where U.S. states were in a dropdown.... With D.C. and protectorates, that's more than 50.


If all you're doing is converting a town to an area number, why not use your area numbers as the values of your <option> tags? That way, there's no nested if-statements to have to deal with.



Want the best answers? Ask the best questions! TANSTAAFL!
 
Hi sleipnir 214,
Thanks for the super fast response as always...

This what I have at present pulling of the towns from a table just with the towns.


HTML

<TD WIDTH="60%"><SELECT NAME=TPL_town>
<? echo $town; ?>
</SELECT>
</TD>


PHP

if (($action == "first" && count($_POST) == 0) || ($_POST
['action'] == "first" && $TPL_err)) {
$country = "";
foreach ($towns as $key=>$name) {
$town .= "<option value=\"$name\"";
if ($name == $TPL_town) {
$town .= " selected";
} elseif ($SETTINGS['defaulttown'] == $name && !isset($TPL_err)) {
$town .= " selected";
}
$town .= ">$name</option>\n";
}
include "templates/template_register_php.html";
}

Just not sure if I should bother changing anything after what you said that if is not a problem...just seemed a bit sloppy, but is does work OK.

Cheers
John
 
First, I recommend that, if you have not done so, you restructure your table so that every town record has two more columns on it: an auto_increment column which is the unique record id of each town's record, and the area number for the town.

This gives you two options.

If you need the town name, your script can produce a <SELECT> like:

[tt]<select name="town_name">
<option value="town ID">town name</option>
.
.
.
<option value="town ID">town name</option>
</select>[/tt]

When this form is submitted, you can use a single database query to fetch the area number from the town ID value.

The other option, if you only need to know the area number, is to fetch the area number and town name and produce a <SELECT> like:

[tt]<select name="area_number">
<option value="area number>town name</option>
.
.
.
<option value="area number">town name</option>
</option>[/tt]

You don't have to do anything. You now already have the area number.





Want the best answers? Ask the best questions! TANSTAAFL!
 
Thanks again Sleipnir,
Seen the light, I always seem to get things too complext in my head and seems now such a simple answer...Great,

Many thanks

Cheers
John
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top