Dropdown (selectbox) values


#1

Hi

I have a dropdown with countries in them. I have language files to replace them with the correct countries:

Language = English, it shows:

  1. Netherlands 2. Germany etc

And when the selected language is German:

  1. Niederlande 2. Deutschland etc

But I need the values to be the same in both languages, so no matter what language is selected, the value for germany should be a preset value like ‘duitsland’ and the same for Netherlands (it would be ‘nederland’ then).

I need this so when posting and searching for countries is based on the same values.

the code I have up to this point:

[code]<?
$countries = array($_LANG[‘C_NETHERLANDS’], $_LANG[‘C_GERMANY’);

$SelectCountry = “ntnt”;

while(list(,$v) = each($countries))
$SelectCountry .= “$vnt”;
}

$SelectCountry .= “”;
?>[/code]

This shows the dropdown with the translated countries, but I wonderinghow to get corresponding values in there aswell.

Thanks!


#2

So, what you want is:

When English is set as the default language, it should show:

  1. Netherlands
  2. Germany

When German is set as the default language, it should show:

  1. Niederlande
  2. Deutschland

When Dutch is set as the default language, it should show:

  1. Nederland
  2. Duitsland

I’d say, make an array containing these values:

[php]
$english = (
“nl” => “Netherlands”,
“de” => “Germany”
);

$german = (
“nl” => “Niederlande”,
“de” => “Deutschland”
);

$dutch = (
“nl” => “Nederland”,
“de” => “Duitsland”
);
[/php]

Then, set the correct array to the preferred language:

[php]
$myLanguage = $english;
if ($setLanguage == “German”) {
$myLanguage = $german;
} elseif ($setLanguage = “Dutch”) {
$myLanguage = $dutch;
}
[/php]

After that, you could easily use the $myLanguage array for your values. For example, in a loop:

[php]
$selectCountry = “”;
while (list($key, $value) = each($myLanguage)) {
$selectCountry .= “<option value=”".$key."">".$value."";
}
$selectCountry .= “”;
[/php]


#3

Its exactly what I want, but…

The Array to build would become pretty timeconsuming, as there are alot of countries and at least 6 languages to work with. The DB holds all data concerning user entered data (country - city - street etc).

I understand (for once :D ) what you’re doing in your script, but wouldn’t it be more efficient if I translate on the fly by using language files (as I do now with the rest of the page)?

The: $_LANG['C_NETHERLANDS'] textfile is way easier to edit and maintain, if I’m not mistaken.

I assume you had a good reason for doing it like you showed here, but I rather have something that’s more flxible.

Thank ALOT!

JP


#4

True, the above solution is mostly suited for a small number of languages (in a small number of translations). If you want it to be more scalable, you could always create a table in your database, which stores the name of the country in any respective language. That’s about as scalable and maintainable (PHPMyAdmin?) as it gets, and as far as I know, even more flexible than any text file.


#5

Table

id             english                german           deutch
-------       -------------------  ----------------  -------------
nl             Netherlands         Niederlande      Nederland
de             Germany             Deutschland      Duitsland

Then you only need to add a new id for the appropriate country in the table when it comes up.

Then you could run a query and in the drop down, use the id as the value and the language value for the text.

[php]
$sql = ‘SELECT id, ‘.$language.’ FROM table ‘;
$data = mysql_query ($sql);
while ($result = mysql_fetch_assoc($data)) {
echo ‘’.$result[$language].’’;
}
[/php]


#6

thanks

I did fix it by passing the values with the same name: when dutch is selected it shows:

ENGLAND - Engeland
GERMANY - Duitsland

and when German is selected it will do

ENGLAND - England
GERMANY - Deutschland

keeping the values the same…

But now I have another problem… the first searchresults (based on country) show the correct content, but I want to refine the search by adding another small form (containing the options) as a header on the resultpage. So on top of the results for dutch theres another form to refine it. You still with me? The sumit button will execute a script where button ID=S2. It does all that EXCEPT that it will not take the country with it, returning a result with only the options. How do I pass the current result (country) into a new search. I’ve tried the $_post but it didn’t work.

Any help?

thanks!


#7

Not sure what you’re trying to achieve, but for a $_POST variable to work, there will have to be a form element of the same name on the submitted form. Say, if a form holds the following elements:

type:      name:       value:
---------  -------     --------
text       mytext      Zyppora
password   mypassword  MyPaSs
checkbox   mycheckbox  Checked!
select     myselect    Selected!
submit     mysubmit    Submit

And the form is submitted, then the next page’s $_POST will look like this:

[php]
$_POST[‘mytext’] = “Zyppora”;
$_POST[‘mypassword’] = “MyPaSs”;
$_POST[‘mycheckbox’] = “Checked!”; // But ONLY if the checkbox was checked!
$_POST[‘myselect’] = “Selected!”;
$_POST[‘mysubmit’] = “Submit”;
[/php]

Notice the comment for the checkbox: this index would not exist if the checkbox was not checked, just like, for example, ‘mywebsite’ does not exist (simply because it wasn’t an element in the form).

So if you want to carry a variable over to the next page, preferrably use a form element. For non-form pages, it’s possible to use a link as in page.php?variable=value (but this requires the $_GET array, rather than the $_POST).

Hope this cleared some things up and you can advance your script.


#8

thanks zyppora

I’ll try to explain what should happen…

searchform - users must choose a country from the list
–> results are passed to ‘advanced.php’ and displayed using ‘search.php’.

The above works fine; I now have the results where country == selectedcountry, the URL = ‘http://www.domain.name/search.php?c=&s=&search_country=Netherlands

In the resultpage ‘search.php’ I want to add checkboxes (with value ‘y’).
After submitting the form it produces a list with the selected option, but without the country like this:
http://www.domain.name/search.php?c=&s= … mingpool=y

Also, when the results return (with the country missing but options selected) in the search.php file, it sets all checkboxes back to unchecked, this will cause page two of the results to be without options (and country for now)…

hope you get what i mean :confused:

thx!


#9

I’m not getting the problem actually. Could you try posting some code, or a visual of what’s going on (or better yet, show us the pages).


#10

sure no problem

the ‘working files’ are on http://www.heymans.name (images are distorded on this one, the proper one is on another domain, but files are identical).

On that page just click the search button (as the countrylist only holds Netherlands and Germany at the moment, and I have no listings for Germany yet).

The resultpage has a ‘header’ where the refining options should go…

thx

JP


#11

Okay … but what options do you want there to be? I assume they’re going to be search-result-narrowing options? So correct me if I’m wrong, but you’ll want the current selection to be narrowed down by adding more options, the currently selected options being retained, right? On the ‘refine search’ page, you could include your current selection like this:

<input type="hidden" name="mypreviousselection" value="<?php echo $mypreviousselection; ?>">

Hopefully I’m completely off.


#12

hi zyppora

it’s still not working, I’m not sure it is transfering anything. It does find the country (as you could see) but whatever I put in the small searchform to refine it fails. The ‘input type = hidden’ suggestion you made I’ve tried before but didn’t do anything.

I indeed want to narrow down th e search, by adding more options (like fireplace, swimmingpool etc).

heh, I wonder why the seemingly ‘simple’ things are so hard :)

thanks!

If I give you the pages needed for the search, would that help you in any way to maybe figure this out?

Thx


#13

Something like this should work:

search.html:

<form method="post" action="search.php">
<select name="country">
<option value="nl">Netherlands</option>
<option value="de">Germany</option>
</select>
<input type="submit" value="submit">
</form>

search.php:
[php]

<?php $country = $_POST['country']; // Don't forget to check the value for SQL injection! $sql = "SELECT * FROM your_table WHERE country = '".$country."'"; $rslt = mysql_query($sql); while ($result = $rslt) { $myPresentation = displayResult($result); // This is where you handle the presentation of the data } ?> [/php]

refinedsearch.php:
[php]
$country = $_POST[‘country’];
$fireplace = $_POST[‘fireplace’];
$swimmingpool = $_POST[‘swimmingpool’];
// Check ALL of these for SQL injection!

$sql = “SELECT * FROM your_table WHERE country = '”.$country."’";
if ($fireplace == 1) { $sql .= " AND fireplace = ‘yes’"; }
if ($swimmingpool == 1) { $sql .= " AND swimmingpool = ‘yes’"; }

// Do SQL and Presentation stuff here
[/php]

Not sure what goes on in your scripts, but this should work just fine.


#14

thanks zyppora!

But I probably start allover again since I’m not getting this to work :confused:
The current search pages have alot more code in it and I have no clue where to add yours… I did try all sorts of things but they all fail - sigh -

I’m gonna fiddle some more with it and if I don’t succeed throw in the blanket on this one :expressionless:

Thanks!